diff options
Diffstat (limited to 'test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java')
-rw-r--r-- | test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java | 148 |
1 files changed, 62 insertions, 86 deletions
diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java index 3603480bf..bed67e33e 100644 --- a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 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. @@ -20,24 +20,13 @@ package org.onap.pnfsimulator.rest; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -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.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import java.time.Duration; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.onap.pnfsimulator.simulator.ResourceReader; import org.onap.pnfsimulator.simulator.Simulator; import org.onap.pnfsimulator.simulator.SimulatorFactory; import org.onap.pnfsimulator.simulator.client.HttpClientAdapter; @@ -46,6 +35,20 @@ import org.onap.pnfsimulator.simulator.validation.ValidationException; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import java.time.Duration; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +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.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + class SimulatorControllerTest { private static final String START_URL = "/simulator/start"; @@ -53,43 +56,8 @@ class SimulatorControllerTest { private static final String STATUS_URL = "/simulator/status"; private static final String JSON_MSG_EXPRESSION = "$.message"; private static final String JSON_STATUS_EXPRESSION = "$.simulatorStatus"; - private static final String PROPER_JSON = "{\n" + - " \"simulatorParams\": {\n" + - " \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v7\",\n" + - " \"testDuration\": \"10\",\n" + - " \"messageInterval\": \"1\"\n" + - " },\n" + - " \"commonEventHeaderParams\": {\n" + - " \"eventName\": \"val11\",\n" + - " \"nfNamingCode\": \"val12\",\n" + - " \"nfcNamingCode\": \"val13\",\n" + - " \"sourceName\": \"val14\",\n" + - " \"sourceId\": \"val15\",\n" + - " \"reportingEntityName\": \"val16\",\n" + - " },\n" + - - " \"pnfRegistrationParams\": {\n" + - " \"SerialNumber\": \"val1\",\n" + - " \"VendorName\": \"val2\",\n" + - " \"OamIpv4Address\": \"val3\",\n" + - " \"OamIpv6Address\": \"val4\",\n" + - " \"Family\": \"val5\",\n" + - " \"ModelNumber\": \"val6\",\n" + - " \"SoftwareVersion\": \"val7\",\n" + - " }\n" + - "}"; - private static final String WRONG_JSON = "{\n" + - " \"mes\": {\n" + - " \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v5\",\n" + - " \"testDuration\": \"10\",\n" + - " \"messageInterval\": \"1\"\n" + - " },\n" + - " \"messageParams\": {\n" + - " \"sourceName\": \"val12\",\n" + - " \"sourceId\": \"val13\",\n" + - " \"reportingEntityName\": \"val14\"\n" + - " }\n" + - "}\n"; + private static String pnfRegistrationJson; + private static String incorrectJson; private MockMvc mockMvc; @@ -98,100 +66,108 @@ class SimulatorControllerTest { @Mock private SimulatorFactory factory; + @Mock private JSONValidator validator; private Simulator simulator; + @BeforeAll + static void beforeAll() { + ResourceReader reader = new ResourceReader("org/onap/pnfsimulator/rest/SimulatorControllerTest/"); + pnfRegistrationJson = reader.readResource("pnfRegistration.json"); + incorrectJson = reader.readResource("incorrectJson.json"); + } + @BeforeEach void setup() { MockitoAnnotations.initMocks(this); simulator = createEndlessSimulator(); mockMvc = MockMvcBuilders - .standaloneSetup(controller) - .build(); + .standaloneSetup(controller) + .build(); } private Simulator createEndlessSimulator() { return spy(Simulator.builder() - .withCustomHttpClientAdapter(mock(HttpClientAdapter.class)) - .withInterval(Duration.ofMinutes(1)) - .build()); + .withCustomHttpClientAdapter(mock(HttpClientAdapter.class)) + .withInterval(Duration.ofMinutes(1)) + .build()); } @Test void wrongJSONFormatOnStart() throws Exception { - when(factory.create(any(),any(), any(),any())).thenReturn(simulator); + when(factory.createSimulatorWithNotification(any(), any(), any())).thenReturn(simulator); doThrow(new ValidationException("")).when(validator).validate(anyString(), anyString()); - mockMvc.perform(post("/simulator/start").content(WRONG_JSON)) - .andExpect(status().isBadRequest()) - .andExpect(jsonPath("$.message").value("Cannot start simulator - Json format " + - "is not compatible with schema definitions")); + mockMvc.perform(post("/simulator/start").content(incorrectJson)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.message").value("Cannot start simulator - Json format " + + "is not compatible with schema definitions")); verify(validator).validate(anyString(), anyString()); } @Test void startSimulatorProperly() throws Exception { - startSimulator(); + startSimulatorWithPnfRegistration(); verify(validator).validate(anyString(), anyString()); - verify(factory).create(any(),any(), any(),any()); + verify(factory).createSimulatorWithPnfRegistration(any(), any(), any()); verify(simulator).start(); } @Test void notStartWhenAlreadyRunning() throws Exception { - startSimulator(); + startSimulatorWithPnfRegistration(); mockMvc - .perform(post(START_URL).content(PROPER_JSON)) - .andExpect(status().isBadRequest()) - .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot start simulator since it's already running")); + .perform(post(START_URL).content(pnfRegistrationJson)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot start simulator since it's already running")); } @Test void stopSimulatorWhenRunning() throws Exception { - startSimulator(); + startSimulatorWithPnfRegistration(); mockMvc - .perform(post(STOP_URL)) - .andExpect(status().isOk()) - .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped")); + .perform(post(STOP_URL)) + .andExpect(status().isOk()) + .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator successfully stopped")); } @Test void getNotRunningMessageWhenOff() throws Exception { mockMvc - .perform(post(STOP_URL)) - .andExpect(status().isBadRequest()) - .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running")); + .perform(post(STOP_URL)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Cannot stop simulator, because it's not running")); } @Test void getRunningStatusWhenOn() throws Exception { - startSimulator(); + startSimulatorWithPnfRegistration(); mockMvc - .perform(get(STATUS_URL)) - .andExpect(status().isOk()) - .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING")); + .perform(get(STATUS_URL)) + .andExpect(status().isOk()) + .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("RUNNING")); } @Test void getNotRunningStatusWhenOff() throws Exception { mockMvc - .perform(get(STATUS_URL)) - .andExpect(status().isOk()) - .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING")); + .perform(get(STATUS_URL)) + .andExpect(status().isOk()) + .andExpect(jsonPath(JSON_STATUS_EXPRESSION).value("NOT RUNNING")); } - private void startSimulator() throws Exception { - when(factory.create(any(), any(), any(),any())).thenReturn(simulator); + private void startSimulatorWithPnfRegistration() throws Exception { + when(factory.createSimulatorWithPnfRegistration(any(), any(), any())).thenReturn(simulator); mockMvc - .perform(post(START_URL).content(PROPER_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started")); + .perform(post(START_URL).content(pnfRegistrationJson)) + .andExpect(status().isOk()) + .andExpect(jsonPath(JSON_MSG_EXPRESSION).value("Simulator started")); } }
\ No newline at end of file |