diff options
author | Grzegorz Wielgosinski <g.wielgosins@samsung.com> | 2022-03-08 13:29:10 +0100 |
---|---|---|
committer | Grzegorz Wielgosinski <g.wielgosins@samsung.com> | 2022-03-08 14:33:08 +0100 |
commit | 7ad88bf6ca841e76d6c6d8fff161ff7e86cc1830 (patch) | |
tree | 8601e18e483ac1f2c4a89add9f829a469f0ea383 /so-cnf-adapter-application/src | |
parent | dddae6e9115e5434b2fb7531ea955a823effe280 (diff) |
Add new unit test and move AaiRequest to aai package
Issue-ID: SO-3845
Change-Id: Ieca3f79236e24d01fef505d31717e971e9ced394
Signed-off-by: Grzegorz Wielgosinski <g.wielgosins@samsung.com>
Diffstat (limited to 'so-cnf-adapter-application/src')
21 files changed, 276 insertions, 127 deletions
diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudApiUrl.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudApiUrl.java new file mode 100644 index 0000000..d21b97a --- /dev/null +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudApiUrl.java @@ -0,0 +1,19 @@ +package org.onap.so.adapters.cnf.client; + +import org.onap.so.adapters.cnf.MulticloudConfiguration; +import org.springframework.stereotype.Component; + +@Component +class MulticloudApiUrl { + + private final MulticloudConfiguration multicloudConfiguration; + + MulticloudApiUrl(MulticloudConfiguration multicloudConfiguration1) { + this.multicloudConfiguration = multicloudConfiguration1; + } + + String apiUrl(String instanceId) { + return multicloudConfiguration.getMulticloudUrl() + "/v1/instance/" + instanceId; + } + +}
\ No newline at end of file diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudClient.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudClient.java index 605806b..5edb34c 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudClient.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/client/MulticloudClient.java @@ -52,19 +52,17 @@ public class MulticloudClient { private static final Logger log = LoggerFactory.getLogger(MulticloudClient.class); private final RestTemplate restTemplate; - private final MulticloudConfiguration multicloudConfiguration; + private final MulticloudApiUrl multicloudApiUrl; private final ObjectMapper objectMapper; - public MulticloudClient(RestTemplate restTemplate, MulticloudConfiguration multicloudConfiguration) { + public MulticloudClient(RestTemplate restTemplate, MulticloudApiUrl multicloudApiUrl) { this.restTemplate = restTemplate; - this.multicloudConfiguration = multicloudConfiguration; + this.multicloudApiUrl = multicloudApiUrl; this.objectMapper = new ObjectMapper(); } public String upgradeInstance(String instanceId, MulticloudInstanceRequest upgradeRequest) throws BadResponseException { - MulticloudApiUrl multicloudApiUrl = new MulticloudApiUrl(multicloudConfiguration); - multicloudApiUrl.setInstanceId(instanceId); - String endpoint = multicloudApiUrl.apiUrl() + "/upgrade"; + String endpoint = multicloudApiUrl.apiUrl(instanceId) + "/upgrade"; ResponseEntity<String> result = restTemplate.exchange(endpoint, POST, getHttpEntity(upgradeRequest), String.class); checkResponseStatusCode(result); log.info("upgradeInstance response status: {}", result.getStatusCode()); @@ -74,9 +72,7 @@ public class MulticloudClient { } public K8sRbInstanceStatus getInstanceStatus(String instanceId) throws BadResponseException { - MulticloudApiUrl multicloudApiUrl = new MulticloudApiUrl(multicloudConfiguration); - multicloudApiUrl.setInstanceId(instanceId); - String endpoint = multicloudApiUrl.apiUrl() + "/status"; + String endpoint = multicloudApiUrl.apiUrl(instanceId) + "/status"; ResponseEntity<String> result = restTemplate.exchange(endpoint, GET, getHttpEntity(), String.class); checkResponseStatusCode(result); log.info("getInstanceStatus response status: {}", result.getStatusCode()); @@ -91,9 +87,7 @@ public class MulticloudClient { } public K8sRbInstanceHealthCheckSimple startInstanceHealthCheck(String instanceId) throws BadResponseException { - MulticloudApiUrl multicloudApiUrl = new MulticloudApiUrl(multicloudConfiguration); - multicloudApiUrl.setInstanceId(instanceId); - String endpoint = multicloudApiUrl.apiUrl() + "/healthcheck"; + String endpoint = multicloudApiUrl.apiUrl(instanceId) + "/healthcheck"; ResponseEntity<String> result = restTemplate.exchange(endpoint, POST, getHttpEntity(), String.class); checkResponseStatusCode(result); log.info("startInstanceHealthCheck response status: {}", result.getStatusCode()); @@ -108,9 +102,7 @@ public class MulticloudClient { } public K8sRbInstanceHealthCheck getInstanceHealthCheck(String instanceId, String healthCheckInstance) throws BadResponseException { - MulticloudApiUrl multicloudApiUrl = new MulticloudApiUrl(multicloudConfiguration); - multicloudApiUrl.setInstanceId(instanceId); - String endpoint = multicloudApiUrl.apiUrl() + "/healthcheck/" + healthCheckInstance; + String endpoint = multicloudApiUrl.apiUrl(instanceId) + "/healthcheck/" + healthCheckInstance; ResponseEntity<String> result = restTemplate.exchange(endpoint, GET, getHttpEntity(), String.class); checkResponseStatusCode(result); log.info("getInstanceHealthCheck response status: {}", result.getStatusCode()); @@ -125,9 +117,7 @@ public class MulticloudClient { } public void deleteInstanceHealthCheck(String instanceId, String healthCheckInstance) throws BadResponseException { - MulticloudApiUrl multicloudApiUrl = new MulticloudApiUrl(multicloudConfiguration); - multicloudApiUrl.setInstanceId(instanceId); - String endpoint = multicloudApiUrl.apiUrl() + "/healthcheck/" + healthCheckInstance; + String endpoint = multicloudApiUrl.apiUrl(instanceId) + "/healthcheck/" + healthCheckInstance; ResponseEntity<String> result = restTemplate.exchange(endpoint, DELETE, getHttpEntity(), String.class); checkResponseStatusCode(result); log.info("deleteInstanceHealthCheck response status: {}", result.getStatusCode()); @@ -166,23 +156,4 @@ public class MulticloudClient { throw new BadResponseException("Multicloud response status error", String.valueOf(statusCode.value())); } } - - private class MulticloudApiUrl { - - private String instanceId; - private final MulticloudConfiguration multicloudConfiguration; - - MulticloudApiUrl(MulticloudConfiguration multicloudConfiguration1) { - this.multicloudConfiguration = multicloudConfiguration1; - } - - String apiUrl() { - String instanceUri = multicloudConfiguration.getMulticloudUrl() + "/v1/instance/"; - return instanceId.equals("") ? instanceUri : instanceUri + instanceId; - } - - void setInstanceId(String instanceId) { - this.instanceId = instanceId; - } - } } diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/exceptions/ApplicationException.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/exceptions/ApplicationException.java index 926bfec..40d0933 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/exceptions/ApplicationException.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/exceptions/ApplicationException.java @@ -20,7 +20,6 @@ package org.onap.so.adapters.cnf.exceptions; -import static org.onap.so.adapters.cnf.util.CnfAdapterUtil.marshal; import org.onap.so.adapters.cnf.model.ErrorResponse; import org.springframework.http.ResponseEntity; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/model/instantiation/AaiRequest.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/model/aai/AaiRequest.java index 80a2cf7..21165b1 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/model/instantiation/AaiRequest.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/model/aai/AaiRequest.java @@ -17,7 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.so.adapters.cnf.model.instantiation; +package org.onap.so.adapters.cnf.model.aai; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/rest/CnfAdapterRest.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/rest/CnfAdapterRest.java index bc85dc4..3272ad9 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/rest/CnfAdapterRest.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/rest/CnfAdapterRest.java @@ -52,7 +52,7 @@ import org.onap.so.adapters.cnf.model.ResourceBundleEntity; import org.onap.so.adapters.cnf.model.Tag; import org.onap.so.adapters.cnf.model.aai.AaiCallbackResponse; import org.onap.so.adapters.cnf.model.healthcheck.HealthCheckResponse; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.StatusCheckResponse; import org.onap.so.adapters.cnf.model.upgrade.InstanceUpgradeRequest; import org.onap.so.adapters.cnf.service.CnfAdapterService; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorService.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorService.java index f8e24f9..d7ee2d1 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorService.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorService.java @@ -22,7 +22,7 @@ package org.onap.so.adapters.cnf.service.aai; import com.google.common.hash.Hashing; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceGvk; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sStatusMetadata; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParser.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParser.java index ee6fd51..0b300c4 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParser.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParser.java @@ -23,7 +23,7 @@ package org.onap.so.adapters.cnf.service.aai; import org.apache.http.client.utils.URIBuilder; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceGvk; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sStatus; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiService.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiService.java index 9c80bf0..ff454b8 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiService.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/aai/AaiService.java @@ -23,7 +23,7 @@ package org.onap.so.adapters.cnf.service.aai; import org.onap.so.adapters.cnf.AaiConfiguration; import org.onap.so.adapters.cnf.client.MulticloudClient; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceStatus; import org.onap.so.adapters.cnf.util.IAaiRepository; @@ -32,7 +32,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java index 6c03757..3f89c53 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java @@ -23,8 +23,6 @@ package org.onap.so.adapters.cnf.util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import org.onap.aaiclient.client.aai.AAIResourcesClient; import org.onap.aaiclient.client.aai.AAITransactionalClient; import org.onap.aaiclient.client.aai.AAIVersion; @@ -34,21 +32,18 @@ import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder; import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types; import org.onap.aaiclient.client.generated.fluentbuilders.K8sResource; import org.onap.aaiclient.client.graphinventory.exceptions.BulkProcessFailed; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.service.aai.KubernetesResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.ws.rs.NotFoundException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; public class AaiRepository implements IAaiRepository { private final static Logger logger = LoggerFactory.getLogger(IAaiRepository.class); - private final static Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private final AAIResourcesClient aaiClient; private final ObjectMapper objectMapper; @@ -98,7 +93,7 @@ public class AaiRepository implements IAaiRepository { } } if (updateK8sResource) { - String payload = null; + String payload; try { payload = objectMapper.writeValueAsString(resource); } catch (JsonProcessingException e) { @@ -185,9 +180,9 @@ public class AaiRepository implements IAaiRepository { private static final int TRANSACTION_LIMIT = 30; - public AAITransactionHelper(AAIResourcesClient aaiClient) { + AAITransactionHelper(AAIResourcesClient aaiClient) { this.aaiClient = aaiClient; - transactions = new ArrayList<AAITransactionalClient>(); + transactions = new ArrayList<>(); transactionCount = TRANSACTION_LIMIT; } @@ -199,7 +194,7 @@ public class AaiRepository implements IAaiRepository { return transactions.get(transactions.size() - 1); } - public void execute(boolean dryRun) { + void execute(boolean dryRun) { if (transactions.size() > 0) { transactions.forEach(transaction -> { try { diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/CnfAdapterUtil.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/CnfAdapterUtil.java index ef2453a..030849f 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/CnfAdapterUtil.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/CnfAdapterUtil.java @@ -36,54 +36,13 @@ public class CnfAdapterUtil { private static final Logger LOGGER = LoggerFactory.getLogger(CnfAdapterUtil.class); - public static final int BAD_REQUEST = 400; - - private static final String UNMARSHAL_FAIL_MSG = "Failed to unmarshal json"; + private static final int BAD_REQUEST = 400; private static final String MARSHAL_FAIL_MSG = "Failed to marshal object"; private static final ObjectMapper MAPPER = new ObjectMapper(); - public static class StatusDesc { - - public static final String ALLOCATE_NSS_SUCCESS = "Allocating nss is " + "successful"; - - public static final String CREATE_NSS_SUCCESS = "Creating nss is " + "successful"; - - public static final String DEALLOCATE_NSS_SUCCESS = "Deallocate nss " + "is successful"; - - public static final String ACTIVATE_NSS_SUCCESS = "Activate nss " + "is successful"; - - public static final String DEACTIVATE_NSS_SUCCESS = "Deactivate nss " + "is successful"; - - public static final String QUERY_JOB_STATUS_FAILED = "Query job " + "status failed"; - - public static final String QUERY_JOB_STATUS_SUCCESS = "Query job " + "status is successful"; - - private StatusDesc() { - - } - } - - private CnfAdapterUtil() { - - } - - public static void assertObjectNotNull(Object object) throws ApplicationException { - if (null == object) { - LOGGER.error("Object is null."); - throw new ApplicationException(BAD_REQUEST, "An object is null."); - } - } - - public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException { - try { - return MAPPER.readValue(jsonstr, type); - } catch (IOException e) { - LOGGER.error(THREE, RA_NS_EXC.toString(), ErrorCode.BusinessProcessError.getValue(), UNMARSHAL_FAIL_MSG, e); - throw new ApplicationException(BAD_REQUEST, UNMARSHAL_FAIL_MSG); - } - } + private CnfAdapterUtil() { } public static String marshal(Object srcObj) throws ApplicationException { try { diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/IAaiRepository.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/IAaiRepository.java index 9bdf1b7..8a865d7 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/IAaiRepository.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/IAaiRepository.java @@ -21,7 +21,7 @@ package org.onap.so.adapters.cnf.util; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.service.aai.KubernetesResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/AaiConfigurationTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/AaiConfigurationTest.java new file mode 100644 index 0000000..93ab257 --- /dev/null +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/AaiConfigurationTest.java @@ -0,0 +1,16 @@ +package org.onap.so.adapters.cnf; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AaiConfigurationTest { + + private AaiConfiguration tested = new AaiConfiguration(); + + @Test + public void shouldReturnDefaultFalse() { + boolean actual = tested.isEnabled(); + assertFalse(actual); + } +}
\ No newline at end of file diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudApiUrlTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudApiUrlTest.java new file mode 100644 index 0000000..f67c239 --- /dev/null +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudApiUrlTest.java @@ -0,0 +1,37 @@ +package org.onap.so.adapters.cnf.client; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.so.adapters.cnf.MulticloudConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +public class MulticloudApiUrlTest { + + private static final String BASE_URL = "http://test-multicloud.com:8080"; + + @InjectMocks + private MulticloudApiUrl tested; + + @Mock + private MulticloudConfiguration multicloudConfiguration; + + @Test + public void shouldPresentInstanceIdInPath() { + // given + String instanceId = "instanceId"; + // when + when(multicloudConfiguration.getMulticloudUrl()).thenReturn(BASE_URL); + + // then + String actual = tested.apiUrl(instanceId); + + assertEquals(BASE_URL + "/v1/instance/instanceId", actual); + } + +}
\ No newline at end of file diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudClientTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudClientTest.java new file mode 100644 index 0000000..738c184 --- /dev/null +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/client/MulticloudClientTest.java @@ -0,0 +1,177 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved. + * Modifications Copyright (C) 2021 Samsung Technologies Co. + * Modifications Copyright (C) 2021 Orange. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.adapters.cnf.client; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.adapters.cnf.model.MulticloudInstanceRequest; +import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheck; +import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheckSimple; +import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceStatus; +import org.onap.so.client.exception.BadResponseException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.http.HttpMethod.DELETE; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; + +@RunWith(SpringRunner.class) +public class MulticloudClientTest { + + private static final String instanceId = "INSTANCE_ID"; + private static final String endpoint = "API_URL"; + + @InjectMocks + private MulticloudClient tested; + + @Mock + private RestTemplate restTemplate; + @Mock + private MulticloudApiUrl multicloudApiUrl; + @Captor + private ArgumentCaptor<String> instanceIdCaptor; + + + @Test + public void shouldUpgradeInstance() throws BadResponseException { + // given + MulticloudInstanceRequest upgradeRequest = mock(MulticloudInstanceRequest.class); + ResponseEntity result = mock(ResponseEntity.class); + String body = "body"; + + // when + when(multicloudApiUrl.apiUrl(instanceId)).thenReturn(endpoint); + when(restTemplate.exchange(eq(getUrl("/upgrade")), eq(POST), any(), eq(String.class))).thenReturn(result); + when(result.getStatusCode()).thenReturn(HttpStatus.OK); + when(result.getBody()).thenReturn(body); + + // then + String actual = tested.upgradeInstance(instanceId, upgradeRequest); + + assertEquals(body, actual); + verify(multicloudApiUrl).apiUrl(instanceIdCaptor.capture()); + assertInstanceIdCaperedValue(instanceIdCaptor.getValue()); + } + + @Test + public void shouldGetInstanceStatus() throws BadResponseException { + // given + ResponseEntity result = mock(ResponseEntity.class); + String body = "{\"ready\":true}"; + + // when + when(multicloudApiUrl.apiUrl(instanceId)).thenReturn(endpoint); + when(restTemplate.exchange(eq(getUrl("/status")), eq(GET), any(), eq(String.class))).thenReturn(result); + when(result.getStatusCode()).thenReturn(HttpStatus.OK); + when(result.getBody()).thenReturn(body); + + // then + K8sRbInstanceStatus actual = tested.getInstanceStatus(instanceId); + + assertEquals(true, actual.isReady()); + verify(multicloudApiUrl).apiUrl(instanceIdCaptor.capture()); + assertInstanceIdCaperedValue(instanceIdCaptor.getValue()); + } + + @Test + public void shouldStartInstanceHealthCheck() throws BadResponseException { + // given + ResponseEntity result = mock(ResponseEntity.class); + String body = "{\"status\":\"SUCCEED\"}"; + + // when + when(multicloudApiUrl.apiUrl(instanceId)).thenReturn(endpoint); + when(restTemplate.exchange(eq(getUrl("/healthcheck")), eq(POST), any(), eq(String.class))).thenReturn(result); + when(result.getStatusCode()).thenReturn(HttpStatus.OK); + when(result.getBody()).thenReturn(body); + + // then + K8sRbInstanceHealthCheckSimple actual = tested.startInstanceHealthCheck(instanceId); + + assertEquals("SUCCEED", actual.getStatus()); + verify(multicloudApiUrl).apiUrl(instanceIdCaptor.capture()); + assertInstanceIdCaperedValue(instanceIdCaptor.getValue()); + } + + @Test + public void shouldGetInstanceHealthCheck() throws BadResponseException { + // given + ResponseEntity result = mock(ResponseEntity.class); + String healthCheckInstance = "healthCheckInstance"; + String body = "{\"status\":\"SUCCEED\"}"; + + // when + when(multicloudApiUrl.apiUrl(instanceId)).thenReturn(endpoint); + when(restTemplate.exchange(eq(getUrl("/healthcheck/"+healthCheckInstance)), eq(GET), any(), eq(String.class))).thenReturn(result); + when(result.getStatusCode()).thenReturn(HttpStatus.OK); + when(result.getBody()).thenReturn(body); + + // then + K8sRbInstanceHealthCheck actual = tested.getInstanceHealthCheck(instanceId, healthCheckInstance); + + assertEquals("SUCCEED", actual.getStatus()); + verify(multicloudApiUrl).apiUrl(instanceIdCaptor.capture()); + assertInstanceIdCaperedValue(instanceIdCaptor.getValue()); + } + + @Test + public void shouldDeleteInstanceHealthCheck() throws BadResponseException { + // given + ResponseEntity result = mock(ResponseEntity.class); + String healthCheckInstance = "healthCheckInstance"; + String body = "body"; + + // when + when(multicloudApiUrl.apiUrl(instanceId)).thenReturn(endpoint); + when(restTemplate.exchange(eq(getUrl("/healthcheck/"+healthCheckInstance)), eq(DELETE), any(), eq(String.class))).thenReturn(result); + when(result.getStatusCode()).thenReturn(HttpStatus.OK); + when(result.getBody()).thenReturn(body); + + // then + tested.deleteInstanceHealthCheck(instanceId, healthCheckInstance); + + verify(multicloudApiUrl).apiUrl(instanceIdCaptor.capture()); + assertInstanceIdCaperedValue(instanceIdCaptor.getValue()); + } + + private void assertInstanceIdCaperedValue(String instanceIdCapturedValue) { + assertEquals(instanceId, instanceIdCapturedValue); + } + + private String getUrl(String path) { + return endpoint + path; + } +}
\ No newline at end of file diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfAdapterRestTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfAdapterRestTest.java index b5f9818..1b16580 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfAdapterRestTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfAdapterRestTest.java @@ -44,8 +44,8 @@ import org.onap.so.adapters.cnf.model.Resource; import org.onap.so.adapters.cnf.model.ResourceBundleEntity; import org.onap.so.adapters.cnf.model.Tag; import org.onap.so.adapters.cnf.model.aai.AaiCallbackResponse; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.healthcheck.HealthCheckResponse; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.StatusCheckResponse; import org.onap.so.adapters.cnf.model.upgrade.InstanceUpgradeRequest; import org.onap.so.adapters.cnf.service.CnfAdapterService; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfRestTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfRestTest.java index 4251f90..77c8e50 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfRestTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfRestTest.java @@ -28,24 +28,12 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.onap.so.adapters.cnf.MulticloudConfiguration; import org.onap.so.adapters.cnf.client.SoCallbackClient; -import org.onap.so.adapters.cnf.model.BpmnInstanceRequest; import org.onap.so.adapters.cnf.model.CheckInstanceRequest; -import org.onap.so.adapters.cnf.model.ConfigTemplateEntity; -import org.onap.so.adapters.cnf.model.ConfigurationEntity; -import org.onap.so.adapters.cnf.model.ConfigurationRollbackEntity; -import org.onap.so.adapters.cnf.model.ConnectivityInfo; -import org.onap.so.adapters.cnf.model.InstanceMiniResponse; import org.onap.so.adapters.cnf.model.InstanceMiniResponseList; -import org.onap.so.adapters.cnf.model.InstanceResponse; import org.onap.so.adapters.cnf.model.InstanceStatusResponse; -import org.onap.so.adapters.cnf.model.MulticloudInstanceRequest; -import org.onap.so.adapters.cnf.model.ProfileEntity; -import org.onap.so.adapters.cnf.model.Resource; -import org.onap.so.adapters.cnf.model.ResourceBundleEntity; -import org.onap.so.adapters.cnf.model.Tag; import org.onap.so.adapters.cnf.model.aai.AaiCallbackResponse; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.healthcheck.HealthCheckResponse; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.StatusCheckResponse; import org.onap.so.adapters.cnf.model.upgrade.InstanceUpgradeRequest; import org.onap.so.adapters.cnf.service.CnfAdapterService; @@ -57,14 +45,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.context.request.async.DeferredResult; -import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -import static org.assertj.core.api.Java6Assertions.assertThat; import static org.junit.Assert.assertEquals; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorServiceTest.java index aad28c9..c79e29f 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorServiceTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiIdGeneratorServiceTest.java @@ -3,7 +3,7 @@ package org.onap.so.adapters.cnf.service.aai; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceGvk; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sStatus; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParserTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParserTest.java index 40e2e4e..ea7510a 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParserTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiResponseParserTest.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceGvk; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sStatus; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiServiceTest.java index 9a5dbc6..651b433 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiServiceTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/aai/AaiServiceTest.java @@ -6,7 +6,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.onap.so.adapters.cnf.AaiConfiguration; import org.onap.so.adapters.cnf.client.MulticloudClient; -import org.onap.so.adapters.cnf.model.instantiation.AaiRequest; +import org.onap.so.adapters.cnf.model.aai.AaiRequest; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus; import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceStatus; import org.onap.so.client.exception.BadResponseException; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java index 8803521..18dc656 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java @@ -1,19 +1,17 @@ package org.onap.so.adapters.cnf.service.healthcheck; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.junit.Test; import org.junit.Rule; +import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.adapters.cnf.client.MulticloudClient; import org.onap.so.adapters.cnf.model.CheckInstanceRequest; import org.onap.so.adapters.cnf.model.InstanceRequest; import org.onap.so.adapters.cnf.model.healthcheck.HealthCheckResponse; import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheck; import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheckSimple; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.UUID; @@ -25,7 +23,6 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -@SpringBootTest @RunWith(SpringRunner.class) public class HealthCheckServiceTest { private final int portNumber = 8443; diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/upgrade/InstanceUpgradeServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/upgrade/InstanceUpgradeServiceTest.java index a4e7983..e291538 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/upgrade/InstanceUpgradeServiceTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/upgrade/InstanceUpgradeServiceTest.java @@ -28,13 +28,10 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.adapters.cnf.client.MulticloudClient; import org.onap.so.adapters.cnf.model.MulticloudInstanceRequest; import org.onap.so.adapters.cnf.model.upgrade.InstanceUpgradeRequest; import org.onap.so.client.exception.BadResponseException; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; @@ -46,7 +43,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@SpringBootTest @RunWith(SpringRunner.class) public class InstanceUpgradeServiceTest { |