diff options
author | Wojciech Sliwka <wojciech.sliwka@nokia.com> | 2019-06-19 11:06:54 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-06-19 11:06:54 +0000 |
commit | 7f0e6936709cc19e3e1e1ad035e5b7d00809d6c6 (patch) | |
tree | 163542e67746e7693daf12e7835f5b2f6c78255a /vid-app-common/src/test/java | |
parent | 46adb535d3af416773475b9aabb2ace8e1e9e1de (diff) | |
parent | 72b3fb69e548cf665204ec025d2778dcf3ce0216 (diff) |
Merge "Extend probe mechanism"
Diffstat (limited to 'vid-app-common/src/test/java')
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")); + } } |