diff options
Diffstat (limited to 'test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator')
5 files changed, 199 insertions, 298 deletions
diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java index aadb54cdc..4524b96c3 100644 --- a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.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,95 +20,100 @@ package org.onap.pnfsimulator.message; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.pnfsimulator.simulator.ResourceReader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER; import static org.onap.pnfsimulator.message.MessageConstants.EVENT; import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS; import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS; -import java.util.Optional; -import org.json.JSONObject; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - public class MessageProviderTest { - private static final String testParamsPnfRegistration = - "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}"; + private static String testParamsPnfRegistration; - private static final String testParamsNotification = - "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}"; + private static String testParamsNotification; - private static MessageProvider messageProvider; + private MessageProvider messageProvider = new MessageProvider(); @BeforeAll public static void setup() { - messageProvider = new MessageProvider(); + ResourceReader reader = new ResourceReader("org/onap/pnfsimulator/message/MessageProviderTest/"); + testParamsPnfRegistration = reader.readResource("pnfRegistrationParams.json"); + testParamsNotification = reader.readResource("notificationParams.json"); } @Test - public void createMessage_should_throw_when_given_empty_arguments() { - assertThrows(IllegalArgumentException.class, - () -> messageProvider.createMessage(new JSONObject(), Optional.empty(), Optional.empty()), - "Params object cannot be null"); + public void createMessageWithPnfRegistration_should_create_constant_message_when_no_params_specified() { + JSONObject message = messageProvider.createMessageWithPnfRegistration(new JSONObject(), new JSONObject()); + JSONObject event = message.getJSONObject(EVENT); + + JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER); + JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS); + + JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); + JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + + assertThat(commonEventHeader.keySet()) + .containsAll(expectedCommonEventHeader.keySet()); + assertThat(pnfRegistrationFields.keySet()) + .containsAll(expectedPnfRegistrationFields.keySet()); } @Test - public void createMessage_should_create_constant_message_when_no_params_specified() { - JSONObject message = messageProvider.createMessage(new JSONObject(), Optional.ofNullable(new JSONObject()), - Optional.ofNullable(new JSONObject())); + public void createMessageWithNotification_should_create_constant_message_when_no_params_specified() { + JSONObject message = messageProvider.createMessageWithNotification(new JSONObject(), + new JSONObject()); JSONObject event = message.getJSONObject(EVENT); JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER); - JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS); JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS); JSONObject expectedCommonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); - JSONObject expectedPnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); JSONObject expectedNotificationFields = JSONObjectFactory.generateNotificationFields(); - expectedCommonEventHeader - .toMap() - .forEach((key, val) -> assertTrue(commonEventHeader.has(key), - () -> String.format("Key %s is not present", key))); + assertThat(commonEventHeader.keySet()) + .containsAll(expectedCommonEventHeader.keySet()); - expectedPnfRegistrationFields - .toMap() - .forEach((key, val) -> assertTrue(pnfRegistrationFields.has(key), - () -> String.format("Key %s is not present", key))); - - expectedNotificationFields - .toMap() - .forEach((key, val) -> assertTrue(notificationFields.has(key), - () -> String.format("Key %s is not present", key))); + assertThat(notificationFields.keySet()) + .containsAll(expectedNotificationFields.keySet()); } @Test - public void createMessage_should_throw_exception_when_params_specified_as_empty() { - assertThrows(IllegalArgumentException.class, - () -> messageProvider.createMessage(new JSONObject(), Optional.empty(), - Optional.empty())); - } - - @Test - public void createMessage_should_add_specified_params_to_valid_subobjects() { + public void createMessageWithPnfRegistration_should_add_specified_params_to_valid_subobjects() { JSONObject message = messageProvider - .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)), - Optional.of(new JSONObject(testParamsNotification))); + .createMessageWithPnfRegistration(new JSONObject(), new JSONObject(testParamsPnfRegistration)); JSONObject event = message.getJSONObject(EVENT); JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER); - assertEquals(10, commonEventHeader.keySet().size()); JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS); - assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1")); - assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2")); + assertThat(pnfRegistrationFields.toMap()).contains( + entry("pnfKey1", "pnfVal1"), + entry("pnfKey2", "pnfVal2"), + entry("pnfKey3", "pnfVal3"), + entry("pnfKey4", "pnfVal4") + ); + } + + @Test + public void createMessageWithNotification_should_add_specified_params_to_valid_subobjects() { + JSONObject message = messageProvider + .createMessageWithNotification(new JSONObject(), + new JSONObject(testParamsNotification)); + JSONObject event = message.getJSONObject(EVENT); JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS); - assertEquals("notVal1", notificationFields.getString("notKey1")); - assertEquals("notVal2", notificationFields.getString("notKey2")); + assertThat(notificationFields.toMap()).contains( + entry("notKey1", "notVal1"), + entry("notKey2", "notVal2"), + entry("notKey3", "notVal3"), + entry("notKey4", "notVal4") + ); } 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 diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/ResourceReader.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/ResourceReader.java new file mode 100644 index 000000000..b5721533f --- /dev/null +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/ResourceReader.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 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. + * 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.pnfsimulator.simulator; + +import com.google.common.io.Resources; + +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class ResourceReader { + private final String baseDirectory; + + public ResourceReader(String baseDirectory) { + this.baseDirectory = baseDirectory; + } + + public String readResource(String resourceName) { + try { + URL pnfRegistrationJson = Resources.getResource(baseDirectory + resourceName); + return Resources.toString(pnfRegistrationJson, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java index ea7a09785..5781bbc0b 100644 --- a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.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,81 +20,72 @@ package org.onap.pnfsimulator.simulator; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_NOTIFICATION_PARAMS; -import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_1; -import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_2; -import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_3; -import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_SIMULATOR_PARAMS; -import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS; -import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS; -import static org.onap.pnfsimulator.simulator.TestMessages.VALID_PNF_REGISTRATION_PARAMS; -import static org.onap.pnfsimulator.simulator.TestMessages.VALID_SIMULATOR_PARAMS; - import com.github.fge.jsonschema.core.exceptions.ProcessingException; -import java.io.IOException; -import java.util.Optional; import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; +import org.json.JSONObject; import org.junit.jupiter.api.Test; import org.onap.pnfsimulator.message.MessageProvider; import org.onap.pnfsimulator.simulator.validation.JSONValidator; import org.onap.pnfsimulator.simulator.validation.ValidationException; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + class SimulatorFactoryTest { + private static final ResourceReader resourceReader = new ResourceReader("org/onap/pnfsimulator/simulator/SimulatorFactoryTest/"); + private static final JSONObject VALID_SIMULATOR_PARAMS = new JSONObject(resourceReader.readResource("validSimulatorParams.json")); + private static final JSONObject VALID_COMMON_EVENT_HEADER_PARAMS = new JSONObject(resourceReader.readResource("validCommonEventHeaderParams.json")); + private static final JSONObject VALID_PNF_REGISTRATION_PARAMS = new JSONObject(resourceReader.readResource("validPnfRegistrationParams.json")); + private static final JSONObject VALID_NOTIFICATION_PARAMS = new JSONObject(resourceReader.readResource("validNotificationParams.json")); - private SimulatorFactory simulatorFactory; + private static final JSONObject INVALID_SIMULATOR_PARAMS = new JSONObject(resourceReader.readResource("invalidSimulatorParams.json")); + private static final JSONObject INVALID_PNF_REGISTRATION_PARAMS_1 = new JSONObject(resourceReader.readResource("invalidPnfRegistrationParams1.json")); + private static final JSONObject INVALID_PNF_REGISTRATION_PARAMS_2 = new JSONObject(resourceReader.readResource("invalidPnfRegistrationParams2.json")); + private static final JSONObject INVALID_PNF_REGISTRATION_PARAMS_3 = new JSONObject(resourceReader.readResource("invalidPnfRegistrationParams3.json")); - @BeforeEach - void setUp() { - simulatorFactory = new SimulatorFactory(new MessageProvider(), new JSONValidator()); - } + private SimulatorFactory simulatorFactory = new SimulatorFactory(new MessageProvider(), new JSONValidator()); @Test void should_successfully_create_simulator_given_valid_pnf_registration_params_and_valid_output_message() - throws ValidationException, IOException, ProcessingException { - assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS,VALID_COMMON_EVENT_HEADER_PARAMS, - VALID_PNF_REGISTRATION_PARAMS,Optional.empty())); + throws ValidationException, IOException, ProcessingException { + assertNotNull(simulatorFactory.createSimulatorWithPnfRegistration(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS)); } @Test void should_successfully_create_simulator_given_valid_notification_params_and_valid_output_message() - throws ValidationException, IOException, ProcessingException { - assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, - Optional.empty(), VALID_NOTIFICATION_PARAMS)); + throws ValidationException, IOException, ProcessingException { + assertNotNull(simulatorFactory.createSimulatorWithNotification(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_NOTIFICATION_PARAMS)); } @Test void should_throw_given_invalid_params() { assertThrows( - JSONException.class, - () -> simulatorFactory.create(INVALID_SIMULATOR_PARAMS,VALID_COMMON_EVENT_HEADER_PARAMS, - VALID_PNF_REGISTRATION_PARAMS,Optional.empty())); + JSONException.class, + () -> simulatorFactory.createSimulatorWithPnfRegistration(INVALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS)); } @Test void should_throw_given_valid_params_and_invalid_output_message() { assertThrows( - ValidationException.class, - () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, - INVALID_PNF_REGISTRATION_PARAMS_1, Optional.empty())); - - assertThrows( - ValidationException.class, - () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, - INVALID_PNF_REGISTRATION_PARAMS_2, Optional.empty())); + ValidationException.class, + () -> simulatorFactory.createSimulatorWithPnfRegistration(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + INVALID_PNF_REGISTRATION_PARAMS_1)); assertThrows( - ValidationException.class, - () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, - INVALID_PNF_REGISTRATION_PARAMS_3, Optional.empty())); + ValidationException.class, + () -> simulatorFactory.createSimulatorWithPnfRegistration(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + INVALID_PNF_REGISTRATION_PARAMS_2)); assertThrows( - ValidationException.class, - () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, - VALID_PNF_REGISTRATION_PARAMS, INVALID_NOTIFICATION_PARAMS)); + ValidationException.class, + () -> simulatorFactory.createSimulatorWithPnfRegistration(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + INVALID_PNF_REGISTRATION_PARAMS_3)); } }
\ No newline at end of file diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java deleted file mode 100644 index 7511084c4..000000000 --- a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * 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.pnfsimulator.simulator; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Optional; -import org.json.JSONObject; - -final class TestMessages { - - static final JSONObject VALID_SIMULATOR_PARAMS = new JSONObject(getContent("validSimulatorParams.json")); - static final JSONObject VALID_COMMON_EVENT_HEADER_PARAMS = new JSONObject(getContent("validCommonEventHeaderParams.json")); - static final Optional<JSONObject> VALID_PNF_REGISTRATION_PARAMS = Optional - .of(new JSONObject(getContent("validPnfRegistrationParams.json"))); - static final Optional<JSONObject> VALID_NOTIFICATION_PARAMS = Optional - .of(new JSONObject(getContent("validNotificationParams.json"))); - - static final JSONObject INVALID_SIMULATOR_PARAMS = new JSONObject( - "{\n" + - " \"vesServerUrl\": \"http://10.42.111.42:8080/eventListener/v5\",\n" + - " \"messageInterval\": \"1\"\n" + - "}"); - - - static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_1 = Optional.of(new JSONObject( - "{\n" + - " \"pnfSerialNumber\": \"val1\",\n" + - " \"pnfVendorName\": \"val2\",\n" + - " \"pnfFamily\": \"val5\",\n" + - " \"pnfModelNumber\": \"val6\",\n" + - " \"pnfSoftwareVersion\": \"val7\",\n" + - " \"pnfType\": \"val8\",\n" + - " \"eventName\": \"val9\",\n" + - " \"nfNamingCode\": \"val10\",\n" + - " \"nfcNamingCode\": \"val11\",\n" + - " \"sourceName\": \"val12\",\n" + - " \"sourceId\": \"val13\",\n" + - " \"reportingEntityName\": \"val14\"\n" + - "}")); - - static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_2 = Optional.of(new JSONObject( - "{\n" + - " \"pnfVendorName\": \"val2\",\n" + - " \"pnfOamIpv4Address\": \"val3\",\n" + - " \"pnfOamIpv6Address\": \"val4\",\n" + - " \"pnfFamily\": \"val5\",\n" + - " \"pnfModelNumber\": \"val6\",\n" + - " \"pnfSoftwareVersion\": \"val7\",\n" + - " \"pnfType\": \"val8\",\n" + - " \"eventName\": \"val9\",\n" + - " \"nfNamingCode\": \"val10\",\n" + - " \"nfcNamingCode\": \"val11\",\n" + - " \"sourceName\": \"val12\",\n" + - " \"sourceId\": \"val13\",\n" + - " \"reportingEntityName\": \"val14\"\n" + - "}")); - - static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_3 = Optional.of(new JSONObject( - "{\n" + - " \"pnfSerialNumber\": \"val1\",\n" + - " \"pnfOamIpv4Address\": \"val3\",\n" + - " \"pnfFamily\": \"val5\",\n" + - " \"pnfModelNumber\": \"val6\",\n" + - " \"pnfSoftwareVersion\": \"val7\",\n" + - " \"pnfType\": \"val8\",\n" + - " \"eventName\": \"val9\",\n" + - " \"nfNamingCode\": \"val10\",\n" + - " \"nfcNamingCode\": \"val11\",\n" + - " \"sourceName\": \"val12\",\n" + - " \"sourceId\": \"val13\",\n" + - " \"reportingEntityName\": \"val14\"\n" + - "}")); - - static final Optional<JSONObject> INVALID_NOTIFICATION_PARAMS = Optional.of(new JSONObject( - "{\n" + - " \"mother\": \"val1\",\n" + - " \"father\": \"val3\",\n" + - "}")); - - - private TestMessages() { - } - - private static String getContent(String fileName) { - try { - String pathAsString = TestMessages.class.getResource(fileName).getPath(); - StringBuilder stringBuilder = new StringBuilder(); - Files.readAllLines(Paths.get(pathAsString)).forEach(line -> { - stringBuilder.append(line); - }); - return stringBuilder.toString(); - } catch (IOException e) { - throw new RuntimeException(String.format("Cannot read JSON file %s", fileName)); - } - } -} |