From ebcb4654ec66465fd63b35d5df2f6a3e5876fd22 Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 14 Sep 2018 14:36:51 +0200 Subject: Added tests for dcae dispacher services Issue-ID: CLAMP-221 Change-Id: Ifb1091f47aec38c04bbb98b8f4b1621c0c7b67a3 Signed-off-by: adam --- .../clds/client/DcaeDispatcherServicesTest.java | 153 +++++++++++++++++++++ .../org/onap/clamp/clds/it/CldsServiceItCase.java | 4 +- .../clds/it/DcaeHttpConnectionManagerItCase.java | 16 ++- .../java/org/onap/clamp/clds/it/HttpsItCase.java | 3 +- .../java/org/onap/clamp/clds/it/SdcReqItCase.java | 8 +- 5 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java (limited to 'src/test') diff --git a/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java new file mode 100644 index 000000000..c828f78a9 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java @@ -0,0 +1,153 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. 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. + * 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.clamp.clds.client; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.clamp.clds.config.ClampProperties; + + + +@RunWith(MockitoJUnitRunner.class) +public class DcaeDispatcherServicesTest { + + private static final String DEPLOYMENT_STATUS_URL = "http://portal.api.simpledemo.onap.org:30297/dcae-deployments/" + + "closedLoop_c9c8b281-6fbd-4702-ba13-affa90411152_deploymentId/" + + "operation/a97b46f6-d77c-42a1-9449-d5ae71e8f688"; + private static final String DCAE_URL = "dcae_url"; + private static final String DEPLOY_RESPONSE_STRING = "{\"links\":" + + "{\"status\":\"http://deployment-handler.onap:8443/dcae-deployments/" + + "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/" + + "operation/366eb098-7977-4966-ae82-abd2087edb10\"}}"; + + @Mock + private ClampProperties clampProperties; + + @Mock + DcaeHttpConnectionManager dcaeHttpConnectionManager; + + @InjectMocks + DcaeDispatcherServices dcaeDispatcherServices; + + private final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\",\"status\": \"processing\"}"; + private final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}"; + + @Before + public void setUp() { + ImmutableMap.builder() + .put("dcae.dispatcher.retry.limit", "3") + .put("dcae.dispatcher.retry.interval", "0") + .put("dcae.dispatcher.url", DCAE_URL) + .build() + .forEach((property, value) -> { + Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any())) + .thenReturn(value); + Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value); + }); + } + + @Test + public void shouldReturnDcaeOperationSataus() throws IOException { + //given + Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null)) + .thenReturn(STATUS_RESPONSE_PROCESSING); + //when + String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL); + + //then + Assertions.assertThat(operationStatus).isEqualTo("processing"); + } + + @Test + public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException { + //given + Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", + null, null)) + .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE); + //when + String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL); + + //then + Assertions.assertThat(operationStatus).isEqualTo("succeeded"); + Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3)) + .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null); + + } + + @Test + public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException { + //given + Mockito.when(dcaeHttpConnectionManager + .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null)) + .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, + STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING); + //when + String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL); + + //then + Assertions.assertThat(operationStatus).isEqualTo("processing"); + Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3)) + .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null); + + } + + @Test + public void shouldTriggerDeploymentCreation() throws IOException { + //given + String deploymentID = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId"; + String serviceTypeId = "e2ba40f7-bf42-41e7-acd7-48fd07586d90"; + Mockito.when(clampProperties.getJsonTemplate("dcae.deployment.template")) + .thenReturn(new ObjectMapper().readTree("{}")); + + Mockito.when(dcaeHttpConnectionManager + .doDcaeHttpQuery(DCAE_URL + + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId", + "PUT", + "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}", + "application/json")) + .thenReturn(DEPLOY_RESPONSE_STRING); + JsonNode blueprintInputJson = new ObjectMapper().readTree("{}"); + + //when + String operationStatus = dcaeDispatcherServices + .createNewDeployment(deploymentID, serviceTypeId, blueprintInputJson); + + //then + Assertions.assertThat(operationStatus).isEqualTo("http://deployment-handler.onap:8443/" + + "dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/" + + "operation/366eb098-7977-4966-ae82-abd2087edb10"); + + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java index 7a37a9dc2..baf4673c1 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java @@ -38,7 +38,6 @@ import java.security.GeneralSecurityException; import java.util.LinkedList; import java.util.List; import java.util.Properties; - import javax.servlet.http.HttpServletRequest; import javax.ws.rs.NotFoundException; import javax.xml.transform.TransformerException; @@ -69,9 +68,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java b/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java index 264853cd4..12e8dd90d 100644 --- a/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java @@ -17,6 +17,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== * */ @@ -45,6 +46,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.client.DcaeHttpConnectionManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -63,6 +65,10 @@ public class DcaeHttpConnectionManagerItCase { private String httpsPort; @Value("${server.http-to-https-redirection.port}") private String httpPort; + + @Autowired + DcaeHttpConnectionManager dcaeHttpConnectionManager; + private static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @@ -103,7 +109,7 @@ public class DcaeHttpConnectionManagerItCase { @Test public void testHttpGet() throws Exception { - String response = DcaeHttpConnectionManager + String response = dcaeHttpConnectionManager .doDcaeHttpQuery("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null); assertNotNull(response); // Should be a redirection so 302, so empty @@ -112,7 +118,7 @@ public class DcaeHttpConnectionManagerItCase { @Test public void testHttpsGet() throws Exception { - String response = DcaeHttpConnectionManager + String response = dcaeHttpConnectionManager .doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null); assertNotNull(response); // Should contain something @@ -121,21 +127,21 @@ public class DcaeHttpConnectionManagerItCase { @Test(expected = BadRequestException.class) public void testHttpsGet404() throws IOException { - DcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", + dcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", "GET", null, null); fail("Should have raised an BadRequestException"); } @Test(expected = BadRequestException.class) public void testHttpsPost404() throws IOException { - DcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", + dcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", "POST", "", "application/json"); fail("Should have raised an BadRequestException"); } @Test(expected = BadRequestException.class) public void testHttpException() throws IOException { - DcaeHttpConnectionManager.doDcaeHttpQuery("http://localhost:" + this.httpsPort + "/designer/index.html", "GET", + dcaeHttpConnectionManager.doDcaeHttpQuery("http://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null); fail("Should have raised an BadRequestException"); } diff --git a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java index 0da267ded..81b6b835b 100644 --- a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java @@ -133,7 +133,8 @@ public class HttpsItCase { .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/v1/api-doc", String.class); assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(httpsEntity.getBody()).contains("swagger"); - FileUtils.writeStringToFile(new File("docs/swagger/swagger.json"), httpsEntity.getBody(),Charset.defaultCharset()); + FileUtils.writeStringToFile( + new File("docs/swagger/swagger.json"), httpsEntity.getBody(), Charset.defaultCharset()); } /** diff --git a/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java b/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java index 36cbc590c..f05b33e10 100644 --- a/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java @@ -89,9 +89,11 @@ public class SdcReqItCase { @Test public void formatSdcReqTest() throws JSONException { - String jsonResult = sdcReq.formatSdcReq("payload", "artifactName", "artifactLabel", "artifactType"); + String jsonResult = sdcReq.formatSdcReq("payload", "artifactName", + "artifactLabel", "artifactType"); JSONAssert.assertEquals( - "{\"payloadData\" : \"cGF5bG9hZA==\",\"artifactLabel\" : \"artifactLabel\",\"artifactName\" :\"artifactName\",\"artifactType\" : \"artifactType\"," + "{\"payloadData\" : \"cGF5bG9hZA==\",\"artifactLabel\" : \"artifactLabel\"," + + "\"artifactName\" :\"artifactName\",\"artifactType\" : \"artifactType\"," + "\"artifactGroupType\" : \"DEPLOYMENT\",\"description\" : \"from CLAMP Cockpit\"}", jsonResult, true); } @@ -102,6 +104,6 @@ public class SdcReqItCase { assertNotNull(listUrls); assertTrue(listUrls.size() == 1); assertTrue(listUrls.get(0).contains( - "/sdc/v1/catalog/services/56441b4b-0467-41dc-9a0e-e68613838219/resourceInstances/vpacketgen0/artifacts")); + "/sdc/v1/catalog/services/56441b4b-0467-41dc-9a0e-e68613838219/resourceInstances/vpacketgen0/artifacts")); } } -- cgit 1.2.3-korg