diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-11-27 10:46:02 +0100 |
---|---|---|
committer | Zebek Bogumil <bogumil.zebek@nokia.com> | 2020-11-30 08:44:57 +0100 |
commit | 0e7c7822f5586ee95eff8c1b529bd94d3399c069 (patch) | |
tree | 6a17f4c5eb5d8a9ff899c6fa6665c7dbdfd14577 | |
parent | 8c262c47f3c090c702ef124345241bd084dfc958 (diff) |
Fix problem with closing http connection
- Resolve problem with http connection closing
- Improve code quality
- Make SdcConnectorClient testable
Issue-ID: SDC-3392
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: Icf082bebdc828245bdb3fa330c753298dbff5b34
4 files changed, 95 insertions, 147 deletions
diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/http/SdcConnectorClient.java b/sdc-distribution-client/src/main/java/org/onap/sdc/http/SdcConnectorClient.java index 33fedc7..ee13944 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/http/SdcConnectorClient.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/http/SdcConnectorClient.java @@ -3,7 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Modifications copyright (C) 2019 Nokia. All rights reserved. + * Modifications copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.UUID; import org.apache.commons.io.IOUtils; @@ -41,9 +42,7 @@ import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.impl.DistributionClientResultImpl; import org.onap.sdc.utils.DistributionActionResultEnum; -import org.onap.sdc.utils.GeneralUtils; import org.onap.sdc.api.asdc.RegistrationRequest; -import org.onap.sdc.api.asdc.ServerListResponse; import org.onap.sdc.api.consumer.IConfiguration; import org.onap.sdc.impl.DistributionClientDownloadResultImpl; import org.onap.sdc.utils.DistributionClientConstants; @@ -59,38 +58,22 @@ import com.google.gson.reflect.TypeToken; import fj.data.Either; public class SdcConnectorClient { - String contentDispositionHeader = "Content-Disposition"; - private static Logger log = LoggerFactory.getLogger(SdcConnectorClient.class.getName()); - private IConfiguration configuration; - private HttpAsdcClient httpClient = null; - - public void init(IConfiguration configuraion) { - this.configuration = configuraion; - httpClient = new HttpAsdcClient(configuration); - } + private final static Logger log = LoggerFactory.getLogger(SdcConnectorClient.class.getName()); + static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition"; - public void close() { - if (httpClient != null) { - httpClient.closeHttpClient(); - } - } + private final IConfiguration configuration; + private final HttpAsdcClient httpClient; - public IConfiguration getConfiguration() { - return configuration; - } - - public void setConfiguration(IConfiguration configuration) { + public SdcConnectorClient(IConfiguration configuration, HttpAsdcClient httpClient) { + Objects.requireNonNull(configuration); + Objects.requireNonNull(httpClient); this.configuration = configuration; - } - - public HttpAsdcClient getHttpClient() { - return httpClient; - } - - public void setHttpClient(HttpAsdcClient httpClient) { this.httpClient = httpClient; } + public void close() { + httpClient.closeHttpClient(); + } public Either<List<String>, IDistributionClientResult> getValidArtifactTypesList() { Pair<HttpAsdcResponse, CloseableHttpResponse> getServersResponsePair = performAsdcServerRequest(AsdcUrls.GET_VALID_ARTIFACT_TYPES); @@ -122,18 +105,17 @@ public class SdcConnectorClient { } private Pair<HttpAsdcResponse, CloseableHttpResponse> performAsdcServerRequest(final String url) { - String requestId = UUID.randomUUID().toString(); + String requestId = generateRequestId(); Map<String, String> requestHeaders = addHeadersToHttpRequest(requestId); log.debug("about to perform getServerList. requestId= {} url= {}", requestId, url); - Pair<HttpAsdcResponse, CloseableHttpResponse> getServersResponsePair = httpClient.getRequest(url, requestHeaders, false); - return getServersResponsePair; + return httpClient.getRequest(url, requestHeaders, false); } public Either<TopicRegistrationResponse, DistributionClientResultImpl> registerAsdcTopics(ApiCredential credential) { - Either<TopicRegistrationResponse, DistributionClientResultImpl> response = null; + Either<TopicRegistrationResponse, DistributionClientResultImpl> response; - String requestId = UUID.randomUUID().toString(); + String requestId = generateRequestId(); Map<String, String> requestHeaders = addHeadersToHttpRequest(requestId); RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName(), configuration.isConsumeProduceStatusTopic(), configuration.getMsgBusAddress()); @@ -160,12 +142,15 @@ public class SdcConnectorClient { } + private String generateRequestId() { + return UUID.randomUUID().toString(); + } + public IDistributionClientResult unregisterTopics(ApiCredential credential) { - DistributionClientResultImpl response = null; + DistributionClientResultImpl response; - String requestId = UUID.randomUUID().toString(); - HttpAsdcClient httpClient = createNewHttpClient(); + String requestId = generateRequestId(); Map<String, String> requestHeaders = addHeadersToHttpRequest(requestId); RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName(), configuration.isConsumeProduceStatusTopic(), configuration.getMsgBusAddress()); @@ -192,15 +177,11 @@ public class SdcConnectorClient { } - HttpAsdcClient createNewHttpClient() { - return new HttpAsdcClient(configuration); - } - - public DistributionClientDownloadResultImpl dowloadArtifact(IArtifactInfo artifactInfo) { - DistributionClientDownloadResultImpl response = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to download artifact from ASDC"); + public DistributionClientDownloadResultImpl downloadArtifact(IArtifactInfo artifactInfo) { + DistributionClientDownloadResultImpl response; - String requestId = UUID.randomUUID().toString(); - Map<String, String> requestHeaders = new HashMap<String, String>(); + String requestId = generateRequestId(); + Map<String, String> requestHeaders = new HashMap<>(); requestHeaders.put(DistributionClientConstants.HEADER_REQUEST_ID, requestId); requestHeaders.put(DistributionClientConstants.HEADER_INSTANCE_ID, configuration.getConsumerID()); requestHeaders.put(HttpHeaders.ACCEPT, ContentType.APPLICATION_OCTET_STREAM.toString()); @@ -222,23 +203,6 @@ public class SdcConnectorClient { /* **************************** private methods ********************************************/ - private Either<List<String>, IDistributionClientResult> parseGetServersResponse(HttpAsdcResponse getServersResponse) { - Either<List<String>, IDistributionClientResult> result; - try { - String jsonMessage = IOUtils.toString(getServersResponse.getMessage().getContent()); - - Gson gson = new GsonBuilder().create(); - ServerListResponse serverListResponse = gson.fromJson(jsonMessage, ServerListResponse.class); - List<String> serverList = serverListResponse.getUebServerList(); - result = Either.left(serverList); - - } catch (UnsupportedOperationException | IOException e) { - result = handleParsingError(e); - } - - return result; - } - private Either<List<String>, IDistributionClientResult> parseGetValidArtifactTypesResponse(HttpAsdcResponse getArtifactTypesResponse) { Either<List<String>, IDistributionClientResult> result; try { @@ -362,8 +326,8 @@ public class SdcConnectorClient { try { is = entity.getContent(); String artifactName = ""; - if (getServersResponse.getHeadersMap().containsKey(contentDispositionHeader)) { - artifactName = getServersResponse.getHeadersMap().get(contentDispositionHeader); + if (getServersResponse.getHeadersMap().containsKey(CONTENT_DISPOSITION_HEADER)) { + artifactName = getServersResponse.getHeadersMap().get(CONTENT_DISPOSITION_HEADER); } byte[] payload = IOUtils.toByteArray(is); @@ -371,25 +335,11 @@ public class SdcConnectorClient { return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.DATA_INTEGRITY_PROBLEM, "failed to get artifact from ASDC. Empty checksum"); } - DistributionClientDownloadResultImpl resResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); - return resResponse; - - + return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); } catch (UnsupportedOperationException | IOException e) { log.error("failed to get artifact from response "); return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "UnsupportedOperationException "); } } - - private boolean validateChecksum(IArtifactInfo artifactInfo, byte[] payload) { - boolean bRes = false; - String calculatedMD5 = GeneralUtils.calculateMD5(payload); - if (artifactInfo.getArtifactChecksum().equals(calculatedMD5)) { - bRes = true; - } - - return bRes; - } - } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java index 5640540..136d43e 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,11 +49,13 @@ import org.onap.sdc.api.consumer.IStatusCallback; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.http.HttpAsdcClient; import org.onap.sdc.http.SdcConnectorClient; import org.onap.sdc.http.TopicRegistrationResponse; import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.DistributionClientConstants; import org.onap.sdc.utils.GeneralUtils; +import org.onap.sdc.utils.Pair; import org.onap.sdc.utils.Wrapper; import org.onap.sdc.api.notification.IVfModuleMetadata; import org.slf4j.Logger; @@ -83,7 +86,7 @@ public class DistributionClientImpl implements IDistributionClient { public static final int TERMINATION_TIMEOUT = 60; private static Logger log = LoggerFactory.getLogger(DistributionClientImpl.class.getName()); - protected SdcConnectorClient asdcConnector = new SdcConnectorClient(); + private SdcConnectorClient asdcConnector; private ScheduledExecutorService executorPool = null; protected CambriaIdentityManager cambriaIdentityManager = null; private List<String> brokerServers; @@ -135,7 +138,9 @@ public class DistributionClientImpl implements IDistributionClient { isConsumerGroupGenerated = false; needToUpdateCambriaConsumer = true; } else if (!isConsumerGroupGenerated) { - generateConsumerGroup(); + String generatedConsumerGroup = UUID.randomUUID().toString(); + configuration.setConsumerGroup(generatedConsumerGroup); + isConsumerGroupGenerated = true; } if (needToUpdateCambriaConsumer) { @@ -249,7 +254,7 @@ public class DistributionClientImpl implements IDistributionClient { IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(result.getDistributionActionResult(), result.getDistributionMessageResult()); return downloadResult; } - return asdcConnector.dowloadArtifact(artifactInfo); + return asdcConnector.downloadArtifact(artifactInfo); } @Override @@ -282,7 +287,8 @@ public class DistributionClientImpl implements IDistributionClient { validateNotTerminated(errorWrapper); } if (errorWrapper.isEmpty()) { - validateAndInitConfiguration(errorWrapper, conf); + this.configuration = validateAndInitConfiguration(errorWrapper, conf).getSecond(); + this.asdcConnector = createAsdcConnector(this.configuration); } // 1. get ueb server list from configuration if (errorWrapper.isEmpty()) { @@ -313,6 +319,10 @@ public class DistributionClientImpl implements IDistributionClient { return result; } + SdcConnectorClient createAsdcConnector(Configuration configuration) { + return new SdcConnectorClient(configuration, new HttpAsdcClient(configuration)); + } + private void registerForTopics(Wrapper<IDistributionClientResult> errorWrapper) { Either<TopicRegistrationResponse, DistributionClientResultImpl> registerAsdcTopics = asdcConnector.registerAsdcTopics(credential); if (registerAsdcTopics.isRight()) { @@ -343,7 +353,6 @@ public class DistributionClientImpl implements IDistributionClient { } private void validateArtifactTypesWithAsdcServer(IConfiguration conf, Wrapper<IDistributionClientResult> errorWrapper) { - asdcConnector.init(configuration); Either<List<String>, IDistributionClientResult> eitherValidArtifactTypesList = asdcConnector.getValidArtifactTypesList(); if (eitherValidArtifactTypesList.isRight()) { DistributionActionResultEnum errorType = eitherValidArtifactTypesList.right().value().getDistributionActionResult(); @@ -494,9 +503,9 @@ public class DistributionClientImpl implements IDistributionClient { return start(); } - protected DistributionActionResultEnum validateAndInitConfiguration(Wrapper<IDistributionClientResult> errorWrapper, IConfiguration conf) { + protected Pair<DistributionActionResultEnum, Configuration> validateAndInitConfiguration(Wrapper<IDistributionClientResult> errorWrapper, IConfiguration conf) { DistributionActionResultEnum result = DistributionActionResultEnum.SUCCESS; - + Configuration configuration = null; if (conf == null) { result = DistributionActionResultEnum.CONFIGURATION_IS_MISSING; } else if (conf.getConsumerID() == null || conf.getConsumerID().isEmpty()) { @@ -520,7 +529,7 @@ public class DistributionClientImpl implements IDistributionClient { } else if (conf.isConsumeProduceStatusTopic() && Objects.isNull(statusCallback)) { result = DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG; } else { // DistributionActionResultEnum.SUCCESS - handleValidConf(conf); + configuration = createConfiguration(conf); } if (result != DistributionActionResultEnum.SUCCESS) { @@ -530,11 +539,11 @@ public class DistributionClientImpl implements IDistributionClient { log.error(initResult.toString()); errorWrapper.setInnerElement(initResult); } - return result; + return new Pair<>(result, configuration); } - private void handleValidConf(IConfiguration conf) { - this.configuration = new Configuration(conf); + private Configuration createConfiguration(IConfiguration conf) { + Configuration configuration = new Configuration(conf); if (!isPollingIntervalValid(conf.getPollingInterval())) { configuration.setPollingInterval(DistributionClientConstants.MIN_POLLING_INTERVAL_SEC); } @@ -542,7 +551,9 @@ public class DistributionClientImpl implements IDistributionClient { configuration.setPollingTimeout(DistributionClientConstants.POLLING_TIMEOUT_SEC); } if (conf.getConsumerGroup() == null) { - generateConsumerGroup(); + String generatedConsumerGroup = UUID.randomUUID().toString(); + configuration.setConsumerGroup(generatedConsumerGroup); + isConsumerGroupGenerated = true; } //Default use HTTPS with SDC @@ -554,12 +565,8 @@ public class DistributionClientImpl implements IDistributionClient { if (conf.isUseHttpsWithDmaap() == null) { configuration.setUseHttpsWithDmaap(true); } - } - private void generateConsumerGroup() { - String generatedConsumerGroup = UUID.randomUUID().toString(); - configuration.setConsumerGroup(generatedConsumerGroup); - isConsumerGroupGenerated = true; + return configuration; } protected boolean isValidFqdn(String fqdn) { diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java index d3a6ffb..c52910d 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java @@ -3,7 +3,7 @@ * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications copyright (C) 2019 Nokia. All rights reserved. + * Modifications copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,7 +85,7 @@ public class SdcConnectorClientTest { Pair<HttpAsdcResponse, CloseableHttpResponse> mockPair = new Pair<>(httpAsdcResponse, null); private HttpEntity lastHttpEntity = null; - private static SdcConnectorClient asdcClient = Mockito.spy(new SdcConnectorClient()); + private static SdcConnectorClient asdcClient; private static final String ARTIFACT_URL = "http://127.0.0.1/artifact/url"; private static final String IT_JUST_DIDN_T_WORK = "It just didn't work"; @@ -97,8 +97,7 @@ public class SdcConnectorClientTest { @BeforeClass public static void beforeClass() { - asdcClient.setConfiguration(configuration); - asdcClient.setHttpClient(httpClient); + asdcClient = Mockito.spy(new SdcConnectorClient(configuration, httpClient)); when(apiCredential.getApiKey()).thenReturn(MOCK_API_KEY); when(httpAsdcResponse.getStatus()).thenReturn(HttpStatus.SC_OK); @@ -131,26 +130,12 @@ public class SdcConnectorClientTest { when(conf.isUseHttpsWithSDC()).thenReturn(true); when(conf.activateServerTLSAuth()).thenReturn(false); - SdcConnectorClient client = new SdcConnectorClient(); - client.init(conf); - assertNotNull(client.getHttpClient()); + final HttpAsdcClient httpClient = new HttpAsdcClient(conf); + SdcConnectorClient client = new SdcConnectorClient(conf, httpClient); client.close(); //check if client is really closed - client.getHttpClient().getRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, new HashMap<>()); - } - - - @Test - public void getConfigurationTest() { - IConfiguration conf = asdcClient.getConfiguration(); - assertEquals(configuration, conf); - } - - @Test - public void getHttpClientTest() { - HttpAsdcClient httpAsdcClient = asdcClient.getHttpClient(); - assertEquals(httpClient, httpAsdcClient); + httpClient.getRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, new HashMap<>()); } @Test @@ -277,8 +262,6 @@ public class SdcConnectorClientTest { when(configuration.getMsgBusAddress()) .thenReturn(Arrays.asList("http://127.0.0.1:45321/dmaap", "http://127.0.0.1:45321/dmaap")); - doReturn(httpClient).when(asdcClient).createNewHttpClient(); - String failMessage = "It just didn't work"; HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); HttpEntity messageMock = mock(HttpEntity.class); @@ -299,8 +282,6 @@ public class SdcConnectorClientTest { when(configuration.getAsdcAddress()).thenReturn("127.0.0.1" + PORT); when(configuration.isConsumeProduceStatusTopic()).thenReturn(false); - doReturn(httpClient).when(asdcClient).createNewHttpClient(); - String failMessage = ""; HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); HttpEntity messageMock = mock(HttpEntity.class); @@ -319,7 +300,7 @@ public class SdcConnectorClientTest { @Test public void downloadArtifactHappyScenarioTest() throws IOException { Map<String, String> headers = new HashMap<>(); - headers.put(asdcClient.contentDispositionHeader, "SomeHeader"); + headers.put(asdcClient.CONTENT_DISPOSITION_HEADER, "SomeHeader"); IArtifactInfo artifactInfo = mock(IArtifactInfo.class); when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL); @@ -335,7 +316,7 @@ public class SdcConnectorClientTest { when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(BYTES)); doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false)); - IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo); + IDistributionClientResult result = asdcClient.downloadArtifact(artifactInfo); assertEquals(DistributionActionResultEnum.SUCCESS, result.getDistributionActionResult()); } @@ -353,7 +334,7 @@ public class SdcConnectorClientTest { when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(BYTES)); doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false)); - IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo); + IDistributionClientResult result = asdcClient.downloadArtifact(artifactInfo); assertEquals(DistributionActionResultEnum.DATA_INTEGRITY_PROBLEM, result.getDistributionActionResult()); } @@ -371,7 +352,7 @@ public class SdcConnectorClientTest { when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting()); doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false)); - IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo); + IDistributionClientResult result = asdcClient.downloadArtifact(artifactInfo); assertEquals(DistributionActionResultEnum.GENERAL_ERROR, result.getDistributionActionResult()); } @@ -389,7 +370,7 @@ public class SdcConnectorClientTest { when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting()); doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false)); - IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo); + IDistributionClientResult result = asdcClient.downloadArtifact(artifactInfo); assertEquals(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, result.getDistributionActionResult()); } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java index bb3c7bf..bc4f78e 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +23,7 @@ package org.onap.sdc.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; import java.io.IOException; @@ -32,6 +34,8 @@ import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; +import org.onap.sdc.http.HttpAsdcClient; +import org.onap.sdc.utils.Pair; import org.onap.sdc.utils.TestConfiguration; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.api.consumer.IConfiguration; @@ -68,10 +72,12 @@ public class DistributionClientTest { @Test public void validateConfigurationTest() { - DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), testConfiguration); + final Pair<DistributionActionResultEnum, Configuration> distributionActionResultEnumConfigurationPair = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), testConfiguration); + DistributionActionResultEnum validationResult = distributionActionResultEnumConfigurationPair.getFirst(); + Configuration configuration = distributionActionResultEnumConfigurationPair.getSecond(); Assert.assertEquals(DistributionActionResultEnum.SUCCESS, validationResult); - Assert.assertEquals(testConfiguration.getPollingInterval(), client.configuration.getPollingInterval()); - Assert.assertEquals(testConfiguration.getPollingTimeout(), client.configuration.getPollingTimeout()); + Assert.assertEquals(testConfiguration.getPollingInterval(), configuration.getPollingInterval()); + Assert.assertEquals(testConfiguration.getPollingTimeout(), configuration.getPollingTimeout()); } @Test @@ -79,10 +85,12 @@ public class DistributionClientTest { TestConfiguration userConfig = new TestConfiguration(); userConfig.setPollingInterval(1); userConfig.setPollingTimeout(2); - DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), userConfig); + final Pair<DistributionActionResultEnum, Configuration> distributionActionResultEnumConfigurationPair = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), userConfig); + DistributionActionResultEnum validationResult = distributionActionResultEnumConfigurationPair.getFirst(); + Configuration configuration = distributionActionResultEnumConfigurationPair.getSecond(); Assert.assertEquals(DistributionActionResultEnum.SUCCESS, validationResult); - assertEquals(15, client.configuration.getPollingInterval()); - assertEquals(15, client.configuration.getPollingTimeout()); + assertEquals(15, configuration.getPollingInterval()); + assertEquals(15, configuration.getPollingTimeout()); } @Test @@ -111,11 +119,11 @@ public class DistributionClientTest { Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); TestConfiguration testPassword = new TestConfiguration(); testPassword.setPassword(null); - DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword); + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword).getFirst(); Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult); testPassword.setPassword(""); - validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword); + validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword).getFirst(); Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult); } @@ -125,11 +133,11 @@ public class DistributionClientTest { Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); TestConfiguration testUser = new TestConfiguration(); testUser.setUser(null); - DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testUser); + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testUser).getFirst(); Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult); testUser.setUser(""); - validationResult = client.validateAndInitConfiguration(errorWrapper, testUser); + validationResult = client.validateAndInitConfiguration(errorWrapper, testUser).getFirst(); Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult); } @@ -144,7 +152,7 @@ public class DistributionClientTest { Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -211,6 +219,10 @@ public class DistributionClientTest { Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); } + private void reconfigureAsdcConnector(SdcConnectorClient connector, DistributionClientImpl client) { + doReturn(connector).when(client).createAsdcConnector(any()); + } + @Test public void initFailedConnectAsdcTest() throws HttpException, CambriaApiException, IOException { // cambriaMock @@ -274,7 +286,7 @@ public class DistributionClientTest { IDistributionClientResult success = initSuccesResult(); Mockito.when(connector.unregisterTopics(Mockito.any(ApiCredential.class))).thenReturn(success); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -321,7 +333,7 @@ public class DistributionClientTest { IDistributionClientResult success = initSuccesResult(); Mockito.when(connector.unregisterTopics(Mockito.any(ApiCredential.class))).thenReturn(success); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -368,7 +380,7 @@ public class DistributionClientTest { Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -398,7 +410,7 @@ public class DistributionClientTest { Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -424,7 +436,7 @@ public class DistributionClientTest { Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.right(failureResult); Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); - client.asdcConnector = connector; + reconfigureAsdcConnector(connector, client); // cambriaMock @@ -513,8 +525,7 @@ public class DistributionClientTest { public void connectorRegisterCI() { - SdcConnectorClient connector = new SdcConnectorClient(); - connector.init(testConfiguration); + SdcConnectorClient connector = new SdcConnectorClient(testConfiguration, new HttpAsdcClient(testConfiguration)); ApiCredential creds = new ApiCredential("publicKey", "secretKey"); Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsFromAsdc = connector.registerAsdcTopics(creds); @@ -523,10 +534,9 @@ public class DistributionClientTest { } public void downloadArtifactTestCI() { - SdcConnectorClient connector = new SdcConnectorClient(); - connector.init(testConfiguration); + SdcConnectorClient connector = new SdcConnectorClient(testConfiguration, new HttpAsdcClient(testConfiguration)); IArtifactInfo artifactInfo = initArtifactInfo(); - connector.dowloadArtifact(artifactInfo); + connector.downloadArtifact(artifactInfo); } // ########### TESTS TO ADD TO CI END ########### |