diff options
author | Wojciech Sliwka <wojciech.sliwka@nokia.com> | 2019-06-14 11:34:27 +0200 |
---|---|---|
committer | Wojciech Sliwka <wojciech.sliwka@nokia.com> | 2019-06-18 14:53:37 +0200 |
commit | 72b3fb69e548cf665204ec025d2778dcf3ce0216 (patch) | |
tree | ac7126066bbcd2986052da905e9d359d9f949328 /vid-app-common/src/test/java/org/onap | |
parent | dd3ba9800ec14898f50858ecd67db0f9245ffc01 (diff) |
Extend probe mechanism
added methods to:
- new aai client
- sdc client
- so client
Change-Id: Ib7beb5a5ac58c2ac9767f5e8dc4f988ac0a44c3e
Issue-ID: VID-490
Signed-off-by: Wojciech Sliwka <wojciech.sliwka@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap')
4 files changed, 152 insertions, 18 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java index 43fb5a3bb..da937cade 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java @@ -21,21 +21,32 @@ package org.onap.vid.aai; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import io.joshworks.restclient.http.HttpResponse; +import org.apache.commons.io.IOUtils; import org.mockito.Answers; import org.mockito.Mock; -import org.mockito.Mockito; import org.onap.vid.aai.model.ResourceType; import org.onap.vid.client.SyncRestClient; +import org.onap.vid.model.Subscriber; import org.onap.vid.model.SubscriberList; +import org.onap.vid.model.probes.ExternalComponentStatus; +import org.springframework.http.HttpStatus; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.io.InputStream; import java.util.Collections; +import java.util.List; import java.util.Map; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; public class AaiOverTLSClientTest { @@ -49,6 +60,9 @@ public class AaiOverTLSClientTest { @Mock private AaiOverTLSPropertySupplier propertySupplier; + @Mock + private HttpResponse<SubscriberList> response; + @BeforeMethod public void setUp() { initMocks(this); @@ -60,7 +74,7 @@ public class AaiOverTLSClientTest { mockPropertyReader(); aaiRestClient.isNodeTypeExistsByName("name", ResourceType.GENERIC_VNF); - Mockito.verify(syncRestClient).get(contains(SEARCH_NODES_QUERY_SEARCH_NODE_TYPE), + verify(syncRestClient).get(contains(SEARCH_NODES_QUERY_SEARCH_NODE_TYPE), eq(getHeaders()), eq(Collections.emptyMap())); } @@ -69,15 +83,49 @@ public class AaiOverTLSClientTest { mockPropertyReader(); aaiRestClient.getAllSubscribers(); - Mockito.verify(syncRestClient).get(contains(SUBSCRIBERS), + verify(syncRestClient).get(contains(SUBSCRIBERS), eq(getHeaders()), eq(Collections.emptyMap()), eq(SubscriberList.class)); } + + @Test + public void probeMechanismShouldReturnAllSubscribers() { + mockPropertyReader(); + List<Subscriber> subscribers = Lists.newArrayList(new Subscriber()); + + SubscriberList subscriberList = new SubscriberList(subscribers); + InputStream json = IOUtils.toInputStream(new Gson().toJson(subscriberList)); + when(syncRestClient.get(contains(SUBSCRIBERS), eq(getHeaders()), eq(Collections.emptyMap()), + eq(SubscriberList.class))).thenReturn(response); + when(response.getStatus()).thenReturn(HttpStatus.OK.value()); + when(response.getRawBody()).thenReturn(json); + when(response.isSuccessful()).thenReturn(true); + + + ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers(); + + assertThat(externalComponentStatus.isAvailable()).isTrue(); + assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI); + } + + @Test + public void probeMechanismShouldHandleExceptionProperly(){ + mockPropertyReader(); + when(syncRestClient.get(contains(SUBSCRIBERS), eq(getHeaders()), eq(Collections.emptyMap()), + eq(SubscriberList.class))).thenThrow(new RuntimeException("call failed")); + + ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers(); + + assertThat(externalComponentStatus.isAvailable()).isFalse(); + assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI); + assertThat(externalComponentStatus.getMetadata().getDescription()).containsSequence("call failed"); + } + private void mockPropertyReader() { - Mockito.when(propertySupplier.getPassword()).thenReturn("Pass"); - Mockito.when(propertySupplier.getUsername()).thenReturn("User"); - Mockito.when(propertySupplier.getRequestId()).thenReturn("1"); - Mockito.when(propertySupplier.getRandomUUID()).thenReturn("2"); + when(propertySupplier.getPassword()).thenReturn("Pass"); + when(propertySupplier.getUsername()).thenReturn("User"); + when(propertySupplier.getRequestId()).thenReturn("1"); + when(propertySupplier.getRandomUUID()).thenReturn("2"); } private Map<String,String> getHeaders(){ @@ -85,5 +133,4 @@ public class AaiOverTLSClientTest { put("X-FromAppId", "VidAaiController").put("Accept", "application/json").put("X-ECOMP-RequestID", "1"). put("X-TransactionId", "2").put("Content-Type", "application/json").build(); } - } diff --git a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java index 5202661da..a20571785 100644 --- a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java @@ -27,17 +27,23 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.vid.asdc.AsdcCatalogException; +import org.onap.vid.asdc.AsdcClient; import org.onap.vid.asdc.beans.Service; import org.onap.vid.client.SyncRestClient; import java.io.InputStream; import java.nio.file.Path; +import java.util.Collections; import java.util.UUID; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.mockito.ArgumentMatchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.matches; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -60,6 +66,9 @@ public class SdcRestClientTest { private HttpResponse<InputStream> httpStreamResponse; @Mock + private HttpResponse<String> httpStringResponse; + + @Mock private InputStream inputStream; private UUID randomId; @@ -123,6 +132,16 @@ public class SdcRestClientTest { restClient.getServiceToscaModel(randomId); } + @Test + public void shouldCallSDCHealthCheck() { + when(mockedSyncRestClient.get(contains(AsdcClient.URIS.HEALTH_CHECK_ENDPOINT), anyMap(), + eq(Collections.emptyMap()), eq(String.class))).thenReturn(httpStringResponse); + + + HttpResponse<String> stringHttpResponse = restClient.checkSDCConnectivity(); + + assertThat(httpStringResponse, is(stringHttpResponse)); + } private Service createTestService() { Service service = new Service(); diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java index f54756e76..8681ecbce 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java @@ -23,12 +23,17 @@ package org.onap.vid.mso; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.Lists; +import com.google.gson.Gson; import io.joshworks.restclient.http.HttpResponse; import org.apache.commons.io.IOUtils; import org.jetbrains.annotations.NotNull; import org.mockito.hamcrest.MockitoHamcrest; import org.onap.vid.changeManagement.WorkflowRequestDetail; import org.onap.vid.model.SOWorkflowList; +import org.onap.vid.model.probes.ExternalComponentStatus; +import org.onap.vid.mso.rest.RequestList; +import org.onap.vid.mso.rest.RequestWrapper; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -84,8 +89,10 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.endsWith; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.onap.vid.controller.MsoController.CONFIGURATION_ID; import static org.onap.vid.controller.MsoController.REQUEST_TYPE; import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID; @@ -1384,6 +1391,31 @@ public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests { msoBusinessLogic.getWorkflowListByModelId("sampleModelId"); } + + @Test + public void probeShouldReturnOrchestrationRequestsAndConnectionStatus(){ + MsoResponseWrapper wrapper = getMsoResponseWrapper(); + given(msoInterface.getOrchestrationRequest(anyString(),anyString(), + anyString(),any(RestObject.class),anyBoolean())).willReturn(wrapper); + + ExternalComponentStatus externalComponentStatus = msoBusinessLogic.probeGetOrchestrationRequests(); + + assertThat(externalComponentStatus.isAvailable()).isTrue(); + assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.MSO); + } + + @NotNull + private MsoResponseWrapper getMsoResponseWrapper() { + MsoResponseWrapper wrapper=new MsoResponseWrapper(); + RequestWrapper requestWrapper = new RequestWrapper(); + requestWrapper.setRequest(new Request()); + RequestList requestList = new RequestList(); + List<RequestWrapper> response = Lists.newArrayList(requestWrapper); + requestList.setRequestList(response); + wrapper.setEntity(new Gson().toJson(requestList)); + return wrapper; + } + private WorkflowRequestDetail createWorkflowRequestDetail() { WorkflowRequestDetail workflowRequestDetail = new WorkflowRequestDetail(); org.onap.vid.changeManagement.RequestParameters requestParameters = new org.onap.vid.changeManagement.RequestParameters(); diff --git a/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java index 5dd2bcd19..3cd3aa8a6 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java @@ -7,9 +7,9 @@ * 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. @@ -21,6 +21,7 @@ package org.onap.vid.services; import com.google.common.collect.ImmutableMap; +import io.joshworks.restclient.http.HttpResponse; import org.apache.commons.lang3.reflect.FieldUtils; import org.mockito.Answers; import org.mockito.Mock; @@ -31,6 +32,8 @@ import org.onap.vid.asdc.AsdcClient; import org.onap.vid.asdc.beans.Service; import org.onap.vid.asdc.parser.ToscaParserImpl2; import org.onap.vid.model.ServiceModel; +import org.onap.vid.model.probes.ExternalComponentStatus; +import org.onap.vid.model.probes.HttpRequestMetadata; import org.onap.vid.properties.Features; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -40,22 +43,31 @@ import java.util.Map; import java.util.UUID; import static java.util.stream.Collectors.toMap; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.IsSame.sameInstance; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class VidServiceImplTest { @Mock(answer = Answers.RETURNS_MOCKS) - AsdcClient asdcClientMock; + private AsdcClient asdcClientMock; @Mock(answer = Answers.RETURNS_MOCKS) - ToscaParserImpl2 toscaParserMock; + private ToscaParserImpl2 toscaParserMock; @Mock - FeatureManager featureManager; + private FeatureManager featureManager; + + @Mock + private HttpResponse<String> httpResponse; private final UUID uuid1 = UUID.randomUUID(); private final UUID uuid2 = UUID.randomUUID(); @@ -86,8 +98,8 @@ public class VidServiceImplTest { when(featureManager.isActive(Features.FLAG_SERVICE_MODEL_CACHE)).thenReturn(true); - when(asdcClientMock.getService(any())).thenAnswer(invocation -> pseudoServiceByUuid.get(invocation.getArguments()[0])); - when(toscaParserMock.makeServiceModel(any(), any())).thenAnswer(invocation -> pseudoModelByService.get(invocation.getArguments()[1])); + when(asdcClientMock.getService(any())).thenAnswer(invocation -> pseudoServiceByUuid.get(invocation.getArguments()[0])); + when(toscaParserMock.makeServiceModel(any(), any())).thenAnswer(invocation -> pseudoModelByService.get(invocation.getArguments()[1])); } @Test @@ -138,5 +150,29 @@ public class VidServiceImplTest { vidService.getService(uuid1.toString()); } + @Test + public void shouldCheckConnectionToSdc() { + when(asdcClientMock.checkSDCConnectivity()).thenReturn(httpResponse); + when(httpResponse.isSuccessful()).thenReturn(true); + when(httpResponse.getBody()).thenReturn("sampleBody"); + + + ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection(); + + assertThat(externalComponentStatus.isAvailable(), is(true)); + assertThat(externalComponentStatus.getComponent(), is(ExternalComponentStatus.Component.SDC)); + HttpRequestMetadata metadata = (HttpRequestMetadata) externalComponentStatus.getMetadata(); + assertThat(metadata.getRawData(), is("sampleBody")); + } + + @Test + public void shouldProperlyHandleNotWorkingSDCConnection(){ + when(asdcClientMock.checkSDCConnectivity()).thenThrow(new RuntimeException("not working")); + + ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection(); + + assertThat(externalComponentStatus.isAvailable(), is(false)); + assertThat(externalComponentStatus.getMetadata().getDescription(),containsString("not working")); + } } |