diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/controllers')
3 files changed, 615 insertions, 267 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java index ca7a163a8..da9cdaa6e 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java @@ -1,48 +1,101 @@ package org.onap.vid.controllers; +import org.apache.log4j.BasicConfigurator; +import org.junit.Before; import org.junit.Test; -import org.onap.vid.controllers.HealthCheckController; -import org.onap.vid.controllers.HealthCheckController.HealthStatus; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.dao.FnAppDoaImpl; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import java.sql.SQLException; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyString; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(MockitoJUnitRunner.class) public class HealthCheckControllerTest { - private HealthCheckController createTestSubject() { - return new HealthCheckController(); - } - - @Test - public void testGetProfileCount() throws Exception { - HealthCheckController testSubject; - String driver = ""; - String URL = ""; - String username = ""; - String password = ""; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getProfileCount(driver, URL, username, password); - } - - @Test - public void testGethealthCheckStatusforIDNS() throws Exception { - HealthCheckController testSubject; - HealthStatus result; - - // default test - testSubject = createTestSubject(); - result = testSubject.gethealthCheckStatusforIDNS(); - } - - @Test - public void testGetHealthCheck() throws Exception { - HealthCheckController testSubject; - String UserAgent = ""; - String ECOMPRequestID = ""; - HealthStatus result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getHealthCheck(UserAgent, ECOMPRequestID); - } + private static final String ERROR_MESSAGE = "error message"; + private HealthCheckController testSubject; + private MockMvc mockMvc; + + @Mock + private FnAppDoaImpl fnAppDoa; + + @Before + public void setUp() { + testSubject = new HealthCheckController(fnAppDoa); + BasicConfigurator.configure(); + mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build(); + } + + @Test + public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception { + databaseConnectionEstablished(); + mockMvc.perform(get("/healthCheck") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.statusCode").value(OK.value())) + .andExpect(jsonPath("$.detailedMsg").value("health check succeeded")); + } + + @Test + public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception { + databaseNotAccessible(); + mockMvc.perform(get("/healthCheck") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value())) + .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE)); + } + + @Test + public void getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception { + databaseConnectionEstablished(); + mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.statusCode").value(OK.value())) + .andExpect(jsonPath("$.detailedMsg").value("health check succeeded")) + .andExpect(jsonPath("$.date").isString()); + } + + @Test + public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception { + databaseNotAccessible(); + mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value())) + .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE)); + } + + @Test + public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception { + mockMvc.perform(get("/commitInfo") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.commitId").value("123")) + .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message")) + .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200")); + } + + private void databaseConnectionEstablished() throws SQLException { + given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString())) + .willReturn(0); + } + + private void databaseNotAccessible() throws SQLException { + given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString())) + .willThrow(new SQLException(ERROR_MESSAGE)); + } }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/controllers/MaintenanceControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controllers/MaintenanceControllerTest.java index 80c65935e..7bdd6b863 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controllers/MaintenanceControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controllers/MaintenanceControllerTest.java @@ -1,66 +1,340 @@ package org.onap.vid.controllers; -import javax.servlet.http.HttpServletRequest; +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright © 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright 2018 Nokia + * ================================================================================ + * 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========================================================= + */ +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.BDDMockito.willThrow; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.times; +import static org.onap.vid.model.CategoryParameter.Family.PARAMETER_STANDARDIZATION; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.function.BiFunction; +import javax.ws.rs.ForbiddenException; +import org.apache.log4j.BasicConfigurator; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.category.AddCategoryOptionResponse; import org.onap.vid.category.AddCategoryOptionsRequest; import org.onap.vid.category.CategoryParameterOptionRep; +import org.onap.vid.category.CategoryParametersResponse; +import org.onap.vid.model.CategoryParameter; import org.onap.vid.model.CategoryParameterOption; -import org.springframework.http.ResponseEntity; +import org.onap.vid.services.CategoryParameterService; +import org.onap.vid.services.CategoryParameterServiceImpl; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +@RunWith(MockitoJUnitRunner.class) public class MaintenanceControllerTest { - private MaintenanceController createTestSubject() { - return new MaintenanceController(); + final private String MAINTENANCE_CATEGORY_PATH = "/maintenance/category_parameter"; + final private String CATEGORY_PARAMETER_PATH = MAINTENANCE_CATEGORY_PATH + "/{name}"; + final private String DELETE_CATEGORY_PATH = "/maintenance/delete_category_parameter/{name}"; + + @Mock + private CategoryParameterService service; + private MaintenanceController maintenanceController; + private MockMvc mockMvc; + private ObjectMapper objectMapper; + + @Before + public void setUp() { + maintenanceController = new MaintenanceController(service); + BasicConfigurator.configure(); + mockMvc = MockMvcBuilders.standaloneSetup(maintenanceController).build(); + objectMapper = new ObjectMapper(); + } + + @Test + public void addCategoryOptions_shouldReturnMultiStatus_whenErrorsExist() throws Exception { + String categoryName = "catName1"; + AddCategoryOptionsRequest req = new AddCategoryOptionsRequest(); + req.options = ImmutableList.of("first option", "second option"); + AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse( + ImmutableList.of("error one", "error two")); + + given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req)))) + .willReturn(addCategoryOptionResponse); + + prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(req)) + .andExpect(status().isMultiStatus()) + .andExpect( + content().json(objectMapper + .writeValueAsString(addCategoryOptionResponse))); + } + + @Test + public void addCategoryOptions_shouldReturnOk_whenNoErrorsExist() throws Exception { + String categoryName = "catName2"; + AddCategoryOptionsRequest req = new AddCategoryOptionsRequest(); + req.options = ImmutableList.of("first option", "second option", "third option"); + AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList()); + + given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req)))) + .willReturn(addCategoryOptionResponse); + prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(req)) + .andExpect(status().isOk()) + .andExpect(content().json( + objectMapper.writeValueAsString(addCategoryOptionResponse))); + } + + @Test + public void addCategoryOptions_shouldReturnNotFound_whenUnfoundedCategoryExceptionIsThrown() throws Exception { + String unfoundedMsg = "unfounded category exception message"; + String categoryName = "catName3"; + AddCategoryOptionsRequest req = new AddCategoryOptionsRequest(); + req.options = ImmutableList.of("first option"); + + given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req)))) + .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg)); + + prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(req)) + .andExpect(status().isNotFound()) + .andExpect(content().json( + objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg))))); + } + + @Test + public void addCategoryOptions_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception { + String categoryName = "catName13"; + AddCategoryOptionsRequest req = new AddCategoryOptionsRequest(); + req.options = ImmutableList.of("option second", "first option"); + + given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req)))) + .willThrow(new RuntimeException()); + + prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(req)) + .andExpect(status().isInternalServerError()); + } + + @Test + public void updateNameForOption_shouldReturnMultiStatus_whenErrorsExist() throws Exception { + String categoryName = "catName4"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id1", "name1"); + AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse( + ImmutableList.of("error one", "error two")); + + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willReturn(addCategoryOptionResponse); + + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isMultiStatus()) + .andExpect( + content().json(objectMapper + .writeValueAsString(addCategoryOptionResponse))); } @Test - public void testAddCategoryOptions() throws Exception { - MaintenanceController testSubject; - HttpServletRequest request = null; - String categoryName = ""; - AddCategoryOptionsRequest option = null; - ResponseEntity result; + public void updateNameForOption_shouldReturnOk_whenNoErrorsExist() throws Exception { + String categoryName = "catName5"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id2", "name2"); + AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList()); - // default test - testSubject = createTestSubject(); - result = testSubject.addCategoryOptions(request, categoryName, option); + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willReturn(addCategoryOptionResponse); + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isOk()) + .andExpect(content().json( + objectMapper.writeValueAsString(addCategoryOptionResponse))); } @Test - public void testUpdateNameForOption() throws Exception { - MaintenanceController testSubject; - HttpServletRequest request = null; - String categoryName = ""; - CategoryParameterOptionRep option = null; - ResponseEntity result; + public void updateNameForOption_shouldReturnForbidden_whenForbiddenExceptionIsThrown() throws Exception { + String categoryName = "catName6"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id3", "name3"); - // default test - testSubject = createTestSubject(); - result = testSubject.updateNameForOption(request, categoryName, option); + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willThrow(new ForbiddenException()); + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isForbidden()) + .andExpect(content().json( + objectMapper + .writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of("HTTP 403 Forbidden"))))); } @Test - public void testGetCategoryParameter() throws Exception { - MaintenanceController testSubject; - HttpServletRequest request = null; - ResponseEntity result; + public void updateNameForOption_shouldReturnNotFound_whenUnfoundedIsThrown() throws Exception { + String unfoundedOptionMsg = "unfounded category option exception message"; + String categoryName = "catName7"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id4", "name4"); + + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryOptionException(unfoundedOptionMsg)); - // default test - testSubject = createTestSubject(); - result = testSubject.getCategoryParameter(request, null); + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isNotFound()) + .andExpect(content().json( + objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedOptionMsg))))); } @Test - public void testDeleteCategoryOption() throws Exception { - MaintenanceController testSubject; - HttpServletRequest request = null; - String categoryName = ""; - CategoryParameterOption option = null; - ResponseEntity result; - - // default test - testSubject = createTestSubject(); - result = testSubject.deleteCategoryOption(request, categoryName, option); + public void updateNameForOption_shouldReturnConflict_whenAlreadyExistOptionNameIsThrown() throws Exception { + String conflictMsg = "already exists option name exception message"; + String categoryName = "catName8"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id5", "name5"); + + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willThrow(new CategoryParameterServiceImpl.AlreadyExistOptionNameException(conflictMsg)); + + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isConflict()) + .andExpect(content().json( + objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(conflictMsg))))); + } + + @Test + public void updateNameForOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception { + String categoryName = "catName18"; + CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id6", "name6"); + + given(service + .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep)))) + .willThrow(new RuntimeException()); + + prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOptionRep)) + .andExpect(status().isInternalServerError()); + } + + @Test + public void getCategoryParameter_shouldReturnExistingMap() throws Exception { + CategoryParametersResponse categoryParametersResponse = + new CategoryParametersResponse( + ImmutableMap.of( + "key1", ImmutableList.of( + new CategoryParameterOptionRep("testId", "testName")))); + + given(service.getCategoryParameters(PARAMETER_STANDARDIZATION)) + .willReturn(categoryParametersResponse); + + mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH) + .param("familyName", "PARAMETER_STANDARDIZATION") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse))); + } + + @Test + public void getCategoryParameter_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception { + given(service.getCategoryParameters(PARAMETER_STANDARDIZATION)) + .willThrow(new RuntimeException()); + + mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH) + .param("familyName", "PARAMETER_STANDARDIZATION") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()); + } + + @Test + public void deleteCategoryOption_shouldReturnOk_whenNoExceptionIsThrown() throws Exception { + String categoryName = "catName9"; + CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id1", "name1", + new CategoryParameter()); + + prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOption)) + .andExpect(status().isOk()); + + then(service).should(times(1)) + .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption))); + } + + @Test + public void deleteCategoryOption_shouldReturnNotFound_whenUnfoundedExceptionIsThrown() throws Exception { + String unfoundedMsg = "unfounded category exception message"; + String categoryName = "catName10"; + CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id2", "name2", + new CategoryParameter()); + + willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg)) + .given(service).deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption))); + + prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOption)) + .andExpect(status().isNotFound()) + .andExpect(content().json( + objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg))))); + } + + @Test + public void deleteCategoryOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception { + String categoryName = "catName19"; + CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id3", "name3", + new CategoryParameter()); + + willThrow(new RuntimeException()).given(service) + .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption))); + + prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName, + objectMapper.writeValueAsString(categoryParameterOption)) + .andExpect(status().isInternalServerError()); + } + + private <T> ArgumentMatcher<T> requestMatcher(T t) { + return new ArgumentMatcher<T>() { + @Override + public boolean matches(Object o) { + return t.equals(o); + } + }; + } + + private ResultActions prepareRequestExpectations( + BiFunction<String, String, MockHttpServletRequestBuilder> httpMethod, + String path, String name, String jsonContent) throws Exception { + return mockMvc.perform(httpMethod.apply(path, name) + .contentType(MediaType.APPLICATION_JSON) + .content(jsonContent)); } }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/controllers/VidControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controllers/VidControllerTest.java index 4e2d994e6..168d90090 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controllers/VidControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controllers/VidControllerTest.java @@ -1,189 +1,210 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Modifications Copyright 2018 Nokia + * ================================================================================ + * 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.vid.controllers; -//import com.fasterxml.jackson.databind.ObjectMapper; -//import net.javacrumbs.jsonunit.JsonAssert; -//import org.apache.commons.io.IOUtils; -//import org.onap.vid.asdc.AsdcCatalogException; -//import org.onap.vid.asdc.AsdcClient; -//import org.onap.vid.asdc.parser.ToscaParserImpl2; -//import org.onap.vid.model.*; -//import org.onap.portalsdk.core.util.SystemProperties; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.mock.web.MockServletContext; -//import org.springframework.test.context.ContextConfiguration; -//import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; -//import org.springframework.test.context.web.WebAppConfiguration; -//import org.testng.Assert; -//import org.testng.annotations.Test; -// -//import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls; -// -//import java.io.IOException; -//import java.io.InputStream; -//import java.nio.file.Path; -//import java.util.Map; -//import java.util.UUID; -// -////import org.junit.Assert; -////import org.junit.Ignore; -////import org.junit.Test; -////import org.junit.runner.RunWith; -////import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -// -//@ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class}) -////@RunWith(SpringJUnit4ClassRunner.class) -//@WebAppConfiguration -// -//public class VidControllerTest extends AbstractTestNGSpringContextTests { -// -// @Autowired -// MockServletContext context; -// @Autowired -// private AsdcClient asdcClient; -// private ToscaParserImpl2 p2 = new ToscaParserImpl2(); -// private ObjectMapper om = new ObjectMapper(); -// -// -// @Test -// public void assertEqualsBetweenServices() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Service expectedService = mockHelper.getNewServiceModel().getService(); -// Service actualService = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService(); -// assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedService), om.writeValueAsString(actualService)); -// } -// } -// -//// @Test -//// public void assertEqualBetweenObjects() throws Exception { -//// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -//// final Path csarPath = getCsarPath(mockHelper.getUuid()); -//// System.out.println("Comparing for csar " + csarPath); -//// ServiceModel actualServiceModel = p2.makeServiceModel(csarPath, getServiceByUuid(mockHelper.getUuid())); -//// assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(mockHelper.getNewServiceModel()), om.writeValueAsString(actualServiceModel)); -//// } -//// } -// -//// @Test -//// public void assertEqualsBetweenNetworkNodes() throws Exception { -//// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -//// Map<String, Network> expectedNetworksMap = mockHelper.getNewServiceModel().getNetworks(); -//// Map<String, Network> actualNetworksMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks(); -//// for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) { -//// Network expectedNetwork = entry.getValue(); -//// Network actualNetwork = actualNetworksMap.get(entry.getKey()); -//// Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName()); -//// verifyBaseNodeProperties(expectedNetwork, actualNetwork); -//// compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties()); -//// } -//// } -//// } -// -// //Because we are not supporting the old flow, the JSON are different by definition. -// @Test -// public void assertEqualsBetweenVnfsOfTosca() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Map<String, VNF> expectedVnfsMap = mockHelper.getNewServiceModel().getVnfs(); -// Map<String, VNF> actualVnfsMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs(); -// for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) { -// VNF expectedVnf = entry.getValue(); -// VNF actualVnf = actualVnfsMap.get(entry.getKey()); -// //need to uncomment these after 1806 merge -// //verifyBaseNodeProperties(expectedVnf, actualVnf); -// Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName()); -// //compareProperties(expectedVnf.getProperties(), actualVnf.getProperties()); -// //assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf)); -// } -// } -// } -// -// @Test -// public void assertEqualsBetweenVolumeGroups() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Map<String, VolumeGroup> actualVolumeGroups = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVolumeGroups(); -// Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getNewServiceModel().getVolumeGroups(); -// JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups); -// } -// } -// -// @Test -// public void assertEqualsBetweenVfModules() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Map<String, VfModule> actualVfModules = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules(); -// Map<String, VfModule> expectedVfModules = mockHelper.getNewServiceModel().getVfModules(); -// //need to uncomment after 1906 merge -// //JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules); -// } -// } -// -// /*@Test -// public void assertEqualsBetweenPolicyConfigurationNodes() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Map<String, PortMirroringConfig> actualConfigurations = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations(); -// Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations(); -// JsonAssert.assertJsonEquals(actualConfigurations, expectedConfigurations); -// } -// }*/ -// -// @Test -// public void assertEqualsBetweenServiceProxyNodes() throws Exception { -// for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) { -// Map<String, ServiceProxy> actualServiceProxies = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getServiceProxies(); -// Map<String, ServiceProxy> expectedServiceProxies = mockHelper.getNewServiceModel().getServiceProxies(); -// JsonAssert.assertJsonEquals(actualServiceProxies, expectedServiceProxies); -// } -// } -// -// private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) { -// Assert.assertEquals(expectedNode.getName(), actualNode.getName()); -// Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid()); -// Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription()); -// Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid()); -// Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid()); -// Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion()); -// } -// -// private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) { -// for (Map.Entry<String, String> property : expectedProperties.entrySet()) { -// String expectedValue = property.getValue(); -// String key = property.getKey(); -// String actualValue = actualProperties.get(key); -// Assert.assertEquals(expectedValue, actualValue); -// } -// } -// -// private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException { -// ToscaParserMockHelper[] mockHelpers = { -// new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath), -// new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath), -// new ToscaParserMockHelper(Constants.configurationUuid, Constants.configurationFilePath), -// }; -// for (ToscaParserMockHelper mockHelper : mockHelpers) { -// InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(mockHelper.getFilePath()); -// String expectedJsonAsString = IOUtils.toString(jsonFile); -// NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class); -// mockHelper.setNewServiceModel(newServiceModel1); -// } -// return mockHelpers; -// } -// -// private Path getCsarPath(String uuid) throws AsdcCatalogException { -// return asdcClient.getServiceToscaModel(UUID.fromString(uuid)); -// } -// -// private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException { -// return asdcClient.getService(UUID.fromString(uuid)); -// } -// -// public class Constants { -// public static final String configurationUuid = "ee6d61be-4841-4f98-8f23-5de9da846ca7"; -// public static final String configurationFilePath = "policy-configuration-csar.JSON"; -// static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931"; -// static final String vlUuid = "cb49608f-5a24-4789-b0f7-2595473cb997"; -// // public static final String PNFUuid = "68101369-6f08-4e99-9a28-fa6327d344f3"; -// static final String vfFilePath = "vf-csar.JSON"; -// static final String vlFilePath = "vl-csar.JSON"; -//// public static final String PNFFilePath = "/Users/Oren/Git/Att/vid_internal/vid-app-common/src/main/resources/pnf.csar"; -// -// } -// -//}
\ No newline at end of file + +import static java.util.stream.Collectors.toMap; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.not; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.times; +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.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.IntStream; +import javax.ws.rs.core.MediaType; +import org.apache.log4j.BasicConfigurator; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.asdc.AsdcCatalogException; +import org.onap.vid.asdc.beans.SecureServices; +import org.onap.vid.asdc.beans.Service; +import org.onap.vid.asdc.beans.ServiceBuilder; +import org.onap.vid.model.CR; +import org.onap.vid.model.Network; +import org.onap.vid.model.Node; +import org.onap.vid.model.PombaInstance.PombaRequest; +import org.onap.vid.model.PombaInstance.ServiceInstance; +import org.onap.vid.model.ServiceModel; +import org.onap.vid.model.ServiceProxy; +import org.onap.vid.model.VNF; +import org.onap.vid.model.VfModule; +import org.onap.vid.model.VolumeGroup; +import org.onap.vid.roles.RoleProvider; +import org.onap.vid.services.AaiService; +import org.onap.vid.services.PombaService; +import org.onap.vid.services.VidService; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class VidControllerTest { + + public static final String REST_MODELS_SERVICES = "/rest/models/services"; + public static final String REST_MODELS_SERVICES_UUID = "/rest/models/services/{uuid}"; + public static final String REST_MODELS_RESET = "/rest/models/reset"; + public static final String REST_MODELS_SERVICES_VERIFY_SERVICE = "/rest/models/services/verifyService"; + @Mock + private VidService vidService; + @Mock + private AaiService aaiService; + @Mock + private RoleProvider roleProvider; + @Mock + private PombaService pombaService; + + private VidController vidController; + private MockMvc mockMvc; + private ObjectMapper objectMapper; + + private String uuid1; + private String uuid2; + private String uuid3; + + @Before + public void setUp() { + vidController = new VidController(vidService, aaiService, roleProvider, pombaService); + BasicConfigurator.configure(); + mockMvc = MockMvcBuilders.standaloneSetup(vidController).build(); + objectMapper = new ObjectMapper(); + + uuid1 = UUID.randomUUID().toString(); + uuid2 = UUID.randomUUID().toString(); + uuid3 = UUID.randomUUID().toString(); + } + + @Test + public void getServices_shouldReturnService_whenServiceExists() throws Exception { + List<Service> services = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3)); + + given(aaiService.getServicesByDistributionStatus()).willReturn(services); + + SecureServices secureServices = new SecureServices(); + secureServices.setServices(services); + secureServices.setReadOnly(false); + + mockMvc.perform(get(REST_MODELS_SERVICES) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(secureServices))); + } + + @Test + public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception { + ServiceModel model = expectedServiceModel(uuid1); + + given(vidService.getService(uuid1)).willReturn(model); + + mockMvc.perform(get(REST_MODELS_SERVICES_UUID, uuid1) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(model))); + } + + @Test + public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception { + String testUuid = UUID.randomUUID().toString(); + + given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg")); + + mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isServiceUnavailable()); + } + + @Test + public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception { + mockMvc.perform(post(REST_MODELS_RESET) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isAccepted()); + + then(vidService).should(times(1)).invalidateServiceCache(); + } + + @Test + public void verifyServiceInstance_shouldReturnOk() throws Exception { + PombaRequest pombaRequest = new PombaRequest(); + pombaRequest.serviceInstanceList = ImmutableList.of(new ServiceInstance()); + + mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(pombaRequest))) + .andExpect(status().isOk()); + + ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class); + then(pombaService).should(times(1)).verify(argumentCaptor.capture()); + + assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue()); + } + + private ServiceModel expectedServiceModel(String uuid) { + final ServiceModel serviceModel = getModelsByUuid().get(uuid); + Assert.assertThat(serviceModel, is(not(nullValue()))); + return serviceModel; + } + + private Service createService(String uuid, int i) { + return new ServiceBuilder().setUuid(uuid).setInvariantUUID("invariantUUID" + i) + .setCategory("category" + i).setVersion("version" + i).setName("name" + i) + .setDistributionStatus("distStatus" + i).setToscaModelURL("toscaModelUrl" + i).build(); + } + + private ServiceModel createServiceModel(int i) { + ServiceModel model = new ServiceModel(); + + model.setCollectionResource(ImmutableMap.of("resKey" + i, new CR())); + model.setNetworks(ImmutableMap.of("network" + i, new Network())); + model.setPnfs(ImmutableMap.of("pnf" + i, new Node())); + model.setServiceProxies(ImmutableMap.of("servProxy" + i, new ServiceProxy())); + model.setVfModules(ImmutableMap.of("vfmod" + i, new VfModule())); + model.setVnfs(ImmutableMap.of("vnf" + i, new VNF())); + model.setVolumeGroups(ImmutableMap.of("volgroup" + i, new VolumeGroup())); + model.setService(new org.onap.vid.model.Service()); + return model; + } + + private Map<String, ServiceModel> getModelsByUuid() { + ServiceModel serviceModel1 = createServiceModel(1); + ServiceModel serviceModel2 = createServiceModel(2); + ServiceModel serviceModel3 = createServiceModel(3); + + List<ServiceModel> pseudoServiceModels = ImmutableList.of(serviceModel1, serviceModel2, serviceModel3); + List<String> uuids = ImmutableList.of(uuid1, uuid2, uuid3); + return IntStream.range(0, pseudoServiceModels.size()).boxed() + .collect(toMap(i -> uuids.get(i), i -> pseudoServiceModels.get(i))); + } +}
\ No newline at end of file |