From 9af9975a352b499b5d59dc90f6a6f76d13feb7c3 Mon Sep 17 00:00:00 2001 From: "Claudio D. Gasparini" Date: Fri, 19 Feb 2021 12:26:22 +0100 Subject: [PATCH] Initial drop of tests Issue-ID: CPS-243 Signed-off-by: Claudio D. Gasparini Change-Id: I9562bcb172a8341885a4a0165d20baba71f9d7fb --- pom.xml | 17 +++ .../onap/cps/tdmt/client/CpsRestClientTest.java | 108 +++++++++++++++ .../java/org/onap/cps/tdmt/model/ModelTest.java | 49 +++++++ .../cps/tdmt/rest/ExecutionControllerTest.java | 119 +++++++++++++++++ .../onap/cps/tdmt/rest/TemplateControllerTest.java | 134 +++++++++++++++++++ .../tdmt/service/ExecutionBusinessLogicTest.java | 147 +++++++++++++++++++++ .../tdmt/service/TemplateBusinessLogicTest.java | 116 ++++++++++++++++ src/test/resources/application-test.properties | 3 + 8 files changed, 693 insertions(+) create mode 100644 src/test/java/org/onap/cps/tdmt/client/CpsRestClientTest.java create mode 100644 src/test/java/org/onap/cps/tdmt/model/ModelTest.java create mode 100644 src/test/java/org/onap/cps/tdmt/rest/ExecutionControllerTest.java create mode 100644 src/test/java/org/onap/cps/tdmt/rest/TemplateControllerTest.java create mode 100644 src/test/java/org/onap/cps/tdmt/service/ExecutionBusinessLogicTest.java create mode 100644 src/test/java/org/onap/cps/tdmt/service/TemplateBusinessLogicTest.java create mode 100644 src/test/resources/application-test.properties diff --git a/pom.xml b/pom.xml index 058e9ae..13ac02c 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,23 @@ org.apache.tomcat.embed tomcat-embed-core + + + com.openpojo + openpojo + 0.8.13 + test + + + junit + junit + test + + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/src/test/java/org/onap/cps/tdmt/client/CpsRestClientTest.java b/src/test/java/org/onap/cps/tdmt/client/CpsRestClientTest.java new file mode 100644 index 0000000..ca03fdd --- /dev/null +++ b/src/test/java/org/onap/cps/tdmt/client/CpsRestClientTest.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * 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.cps.tdmt.client; + +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.onap.cps.tdmt.exception.CpsException; +import org.onap.cps.tdmt.model.AppConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringRunner.class) +@EnableConfigurationProperties(AppConfiguration.class) +@TestPropertySource("classpath:application-test.properties") +public class CpsRestClientTest { + + @TestConfiguration + static class CpsRestClientTestContextConfiguration { + + @Bean + public CpsRestClient cpsRestClient() { + return new CpsRestClient(); + } + } + + @Autowired + private CpsRestClient cpsRestClient; + + @MockBean + private RestTemplate restTemplate; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void testFetchNode() throws Exception { + final HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setContentType(MediaType.APPLICATION_JSON); + final ResponseEntity response = new ResponseEntity<>("sample response", responseHeaders, + HttpStatus.OK); + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.>any())) + .thenReturn(response); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample")); + + final ResponseEntity errorResponse = new ResponseEntity<>("sample response", + responseHeaders, HttpStatus.NOT_FOUND); + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.>any())) + .thenReturn(errorResponse); + exception.expect(CpsException.class); + exception.expectMessage("Response code from CPS other than 200: 404"); + cpsRestClient.fetchNode("coverage-area-onap", "sample"); + + } + + @Test + public void testFetchNodeException() throws Exception { + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.>any())) + .thenThrow(new RestClientException("Connection refused")); + exception.expect(CpsException.class); + exception.expectMessage("Connection refused"); + cpsRestClient.fetchNode("coverage-area-onap", "sample"); + } +} diff --git a/src/test/java/org/onap/cps/tdmt/model/ModelTest.java b/src/test/java/org/onap/cps/tdmt/model/ModelTest.java new file mode 100644 index 0000000..0ac8475 --- /dev/null +++ b/src/test/java/org/onap/cps/tdmt/model/ModelTest.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * 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.cps.tdmt.model; + +import com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import org.junit.Test; + +public class ModelTest { + + private static final String POJO_PACKAGE = "org.onap.cps.tdmt.model"; + + @Test + public void testModels() { + final Validator validator = ValidatorBuilder.create() + .with(new GetterMustExistRule()) + .with(new SetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + + } + +} diff --git a/src/test/java/org/onap/cps/tdmt/rest/ExecutionControllerTest.java b/src/test/java/org/onap/cps/tdmt/rest/ExecutionControllerTest.java new file mode 100644 index 0000000..38f412f --- /dev/null +++ b/src/test/java/org/onap/cps/tdmt/rest/ExecutionControllerTest.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * 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.cps.tdmt.rest; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.onap.cps.tdmt.exception.ExecuteException; +import org.onap.cps.tdmt.exception.RecordNotFoundException; +import org.onap.cps.tdmt.model.ExecutionRequest; +import org.onap.cps.tdmt.service.ExecutionBusinessLogic; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@WebMvcTest(ExecutionController.class) +public class ExecutionControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private ExecutionBusinessLogic executionBusinessLogic; + + private ObjectMapper mapper; + private String requestJson; + private String executePath; + + /** + * Setup variables before test. + * + */ + @Before + public void setup() throws Exception { + executePath = "/execute/ran-network/getNbr"; + final Map input = new HashMap<>(); + input.put("coverageArea", "Zone 1"); + final ExecutionRequest request = new ExecutionRequest(input); + mapper = new ObjectMapper(); + requestJson = mapper.writeValueAsString(request); + } + + @Test + public void testExecuteTemplate() throws Exception { + final String result = "{\"key\": \"value\"}"; + Mockito.when(executionBusinessLogic + .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(), + ArgumentMatchers.any())) + .thenReturn(result); + mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON) + .characterEncoding("utf-8") + .content(requestJson).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(result)); + + Mockito.when(executionBusinessLogic + .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(), + ArgumentMatchers.any())) + .thenThrow(new RecordNotFoundException("Template does not exist")); + mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON) + .characterEncoding("utf-8") + .content(requestJson).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + + mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON) + .characterEncoding("utf-8") + .content("{\"bad\": \"request\"").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + public void testExecuteTemplateException() throws Exception { + final String responseJson = "{\n" + + " \"message\": \"Error while executing template\",\n" + + " \"details\": [\"Response from CPS other than 200: 404\"]\n" + + "}"; + + Mockito.when(executionBusinessLogic + .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(), + ArgumentMatchers.any())) + .thenThrow(new ExecuteException("Response from CPS other than 200: 404")); + mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON) + .characterEncoding("utf-8") + .content(requestJson).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(responseJson)); + } +} diff --git a/src/test/java/org/onap/cps/tdmt/rest/TemplateControllerTest.java b/src/test/java/org/onap/cps/tdmt/rest/TemplateControllerTest.java new file mode 100644 index 0000000..7e31265 --- /dev/null +++ b/src/test/java/org/onap/cps/tdmt/rest/TemplateControllerTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * 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.cps.tdmt.rest; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.onap.cps.tdmt.exception.RecordNotFoundException; +import org.onap.cps.tdmt.model.Template; +import org.onap.cps.tdmt.model.TemplateRequest; +import org.onap.cps.tdmt.service.TemplateBusinessLogic; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@WebMvcTest(TemplateController.class) +public class TemplateControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private TemplateBusinessLogic templateBusinessLogic; + + private ObjectMapper mapper; + + private Template template; + + @Before + public void setup() { + mapper = new ObjectMapper(); + template = new Template("getNbr", "ran-network", "sample"); + } + + @Test + public void testCreateTemplate() throws Exception { + final TemplateRequest emptyTemplateRequest = new TemplateRequest(); + emptyTemplateRequest.setId("getNbr"); + emptyTemplateRequest.setSchemaSet("ran-network"); + final String emptyTemplateJson = mapper.writeValueAsString(emptyTemplateRequest); + mockMvc.perform( + post("/templates").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .content(emptyTemplateJson).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + + final TemplateRequest request = new TemplateRequest("getNbr", "ran-network", "sample"); + final String templateJson = mapper.writeValueAsString(request); + Mockito.when(templateBusinessLogic.createTemplate(ArgumentMatchers.any())) + .thenReturn(template); + mockMvc.perform( + post("/templates").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .content(templateJson).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + .andExpect(content().json(templateJson)); + } + + @Test + public void testGetAllTemplates() throws Exception { + final List