Fixed nullpointer exception and optimized the code here and there.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
kaizimmerm
2016-06-23 09:02:05 +02:00
parent 717a23671e
commit fa2477f5cb
5 changed files with 42 additions and 37 deletions

View File

@@ -42,6 +42,8 @@ import org.springframework.beans.factory.annotation.Qualifier;
*/
public class ConfigurableScenario {
private static final int PAGE_SIZE = 100;
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurableScenario.class);
private final MgmtDistributionSetClientResource distributionSetResource;
@@ -109,25 +111,25 @@ public class ConfigurableScenario {
private void deleteSoftwareModules() {
PagedList<MgmtSoftwareModule> modules;
do {
modules = softwareModuleResource.getSoftwareModules(0, 100, null, null).getBody();
modules = softwareModuleResource.getSoftwareModules(0, PAGE_SIZE, null, null).getBody();
modules.getContent().forEach(module -> softwareModuleResource.deleteSoftwareModule(module.getModuleId()));
} while (modules.getTotal() > 100);
} while (modules.getTotal() > PAGE_SIZE);
}
private void deleteDistributionSets() {
PagedList<MgmtDistributionSet> distributionSets;
do {
distributionSets = distributionSetResource.getDistributionSets(0, 100, null, null).getBody();
distributionSets = distributionSetResource.getDistributionSets(0, PAGE_SIZE, null, null).getBody();
distributionSets.getContent().forEach(set -> distributionSetResource.deleteDistributionSet(set.getDsId()));
} while (distributionSets.getTotal() > 100);
} while (distributionSets.getTotal() > PAGE_SIZE);
}
private void deleteTargets() {
PagedList<MgmtTarget> targets;
do {
targets = targetResource.getTargets(0, 100, null, null).getBody();
targets = targetResource.getTargets(0, PAGE_SIZE, null, null).getBody();
targets.getContent().forEach(target -> targetResource.deleteTarget(target.getControllerId()));
} while (targets.getTotal() > 100);
} while (targets.getTotal() > PAGE_SIZE);
}
private void runRollouts(final Scenario scenario) {
@@ -217,10 +219,11 @@ public class ConfigurableScenario {
private void createTargets(final Scenario scenario) {
LOGGER.info("Creating {} targets", scenario.getTargets());
for (int i = 0; i < scenario.getTargets() / 100; i++) {
targetResource.createTargets(new TargetBuilder().controllerId(scenario.getTargetName())
.address(scenario.getTargetAddress()).buildAsList(i * 100,
(i + 1) * 100 > scenario.getTargets() ? (scenario.getTargets() - (i * 100)) : 100));
for (int i = 0; i < (scenario.getTargets() / PAGE_SIZE); i++) {
targetResource.createTargets(
new TargetBuilder().controllerId(scenario.getTargetName()).address(scenario.getTargetAddress())
.buildAsList(i * PAGE_SIZE, (i + 1) * PAGE_SIZE > scenario.getTargets()
? (scenario.getTargets() - (i * PAGE_SIZE)) : PAGE_SIZE));
}
LOGGER.info("Creating {} targets -> Done", scenario.getTargets());

View File

@@ -12,11 +12,16 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* Implementation for {@link MultipartFile} for hawkBit artifact upload.
*
*/
public class ArtifactFile implements MultipartFile {
private final String name;
@@ -55,9 +60,9 @@ public class ArtifactFile implements MultipartFile {
final byte[] content) {
Assert.hasLength(name, "Name must not be null");
this.name = name;
this.originalFilename = originalFilename != null ? originalFilename : "";
this.originalFilename = Optional.ofNullable(originalFilename).orElse("");
this.contentType = contentType;
this.content = content != null ? content : new byte[0];
this.content = Optional.ofNullable(content).orElse(new byte[0]);
}
@Override

View File

@@ -40,7 +40,7 @@ public class FeignMultipartEncoder implements Encoder {
private final HttpHeaders multipartHeaders = new HttpHeaders();
private final HttpHeaders jsonHeaders = new HttpHeaders();
public static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
public FeignMultipartEncoder() {
multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
@@ -48,11 +48,8 @@ public class FeignMultipartEncoder implements Encoder {
}
@Override
public void encode(final Object object, final Type bodyType, final RequestTemplate template)
throws EncodeException {
public void encode(final Object object, final Type bodyType, final RequestTemplate template) {
encodeMultipartFormRequest(object, template);
}
private void encodeMultipartFormRequest(final Object value, final RequestTemplate template) {
@@ -66,8 +63,7 @@ public class FeignMultipartEncoder implements Encoder {
}
@SuppressWarnings("unchecked")
private void encodeRequest(final Object value, final HttpHeaders requestHeaders, final RequestTemplate template)
throws EncodeException {
private void encodeRequest(final Object value, final HttpHeaders requestHeaders, final RequestTemplate template) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
try {
@@ -150,12 +146,6 @@ public class FeignMultipartEncoder implements Encoder {
return this.filename;
}
@Override
public InputStream getInputStream() throws IOException, IllegalStateException {
return super.getInputStream(); // To change body of generated
// methods, choose Tools | Templates.
}
@Override
public long contentLength() throws IOException {
return size;