diff options
Diffstat (limited to 'pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator')
5 files changed, 643 insertions, 0 deletions
diff --git a/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java new file mode 100644 index 0000000..d8e60c1 --- /dev/null +++ b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java @@ -0,0 +1,66 @@ +/* + * ============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 static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +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 java.util.Optional; +import org.json.JSONException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SimulatorFactoryTest { + + + private SimulatorFactory simulatorFactory; + + @BeforeEach + void setUp() { + simulatorFactory = new SimulatorFactory(); + } + + @Test + void should_successfully_create_simulator_given_valid_pnf_registration_params() { + assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS, Optional.empty())); + } + + @Test + void should_successfully_create_simulator_given_valid_notification_params_and_valid_output_message() { + assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + Optional.empty(), VALID_NOTIFICATION_PARAMS)); + } + + @Test + void should_throw_given_invalid_simulator_params() { + assertThrows( + JSONException.class, + () -> simulatorFactory.create(INVALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS, VALID_NOTIFICATION_PARAMS)); + } +} + + diff --git a/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java new file mode 100644 index 0000000..1f15c01 --- /dev/null +++ b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java @@ -0,0 +1,204 @@ +/* + * ============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 static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTimeout; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +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.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 java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.mockito.Mockito; +import org.onap.pnfsimulator.FileProvider; +import org.onap.pnfsimulator.simulator.client.RestTemplateAdapter; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; +import org.onap.pnfsimulator.simulator.validation.ValidationException; + +public class SimulatorTest { + + private static final String TEST_VES_URL = "http://localhost:10000/eventListener/v7"; + private static final String TEST_XNF_URL = "sftp://onap:pano@10.11.0.68" + "/"; + private FileProvider fileProvider = mock(FileProvider.class); + + private void createSampleFileList() { + List<String> fileList = new ArrayList<>(); + fileList.add("A20190401.1608+0000-1622+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + fileList.add("A20190401.1623+0000-1637+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + + try { + doReturn(fileList).when(fileProvider).getFiles(); + } catch (NoRopFilesException e) { + e.printStackTrace(); + } + } + + @Test + void builder_should_create_endless_simulator_when_duration_not_specified() { + Simulator simulator = Simulator + .builder() + .withDuration(Duration.ofSeconds(1)) + .withVesUrl(TEST_VES_URL).build(); + + assertFalse(simulator.isEndless()); + + simulator = Simulator + .builder() + .withVesUrl(TEST_VES_URL).build(); + + assertTrue(simulator.isEndless()); + } + + @Test + void simulator_should_stop_when_interrupted() { + createSampleFileList(); + + RestTemplateAdapter httpClientMock = Mockito.mock(RestTemplateAdapter.class); + Simulator simulator = Simulator.builder() + .withInterval(Duration.ofSeconds(1)) + .withCustomRestTemplateAdapter(httpClientMock) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(Optional.empty()) + .withNotificationParams(VALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withCustomRestTemplateAdapter(httpClientMock) + .withFileProvider(fileProvider).build(); + + simulator.start(); + simulator.interrupt(); + + assertTimeout(Duration.ofSeconds(1), (Executable) simulator::join); + } + + @Test + void should_throw_noropfiles_exception_given_empty_filelist() { + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(new FileProvider()).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertTrue(e instanceof NoRopFilesException); + } + + @Test + void should_throw_validation_exception_given_invalid_params() { + createSampleFileList(); + + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_1) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_2) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_3) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS) + .withNotificationParams(INVALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + } + + @Test + void simulator_should_send_fileready_message() { + createSampleFileList(); + + RestTemplateAdapter httpClientMock = Mockito.mock(RestTemplateAdapter.class); + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(100)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(Optional.empty()) + .withNotificationParams(VALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withCustomRestTemplateAdapter(httpClientMock) + .withFileProvider(fileProvider).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertNull(e); + + assertTimeout(Duration.ofMillis(150), (Executable) simulator::join); + verify(httpClientMock, times(1)).send(anyString(), eq(TEST_VES_URL)); + } +} + diff --git a/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java new file mode 100644 index 0000000..d92b3c2 --- /dev/null +++ b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java @@ -0,0 +1,116 @@ +/* + * ============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; + +public final class TestMessages { + + static final JSONObject VALID_SIMULATOR_PARAMS = new JSONObject(getContent("validSimulatorParams.json")); + public 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"))); + public 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)); + } + } +} diff --git a/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/client/RestTemplateAdapterImplTest.java b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/client/RestTemplateAdapterImplTest.java new file mode 100644 index 0000000..d423222 --- /dev/null +++ b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/client/RestTemplateAdapterImplTest.java @@ -0,0 +1,134 @@ +/* + * ============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.client; + +import org.mockito.Mock; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.notNull; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class RestTemplateAdapterImplTest { + + private RestTemplateAdapter adapter; + + @Mock + private RestTemplate restTemplate; + @Mock + ResponseEntity<String> response; + + @BeforeEach + void setup() { + initMocks(this); + adapter = new RestTemplateAdapterImpl(restTemplate); + } + + @Test + void send_should_successfully_send_request_given_valid_http_url() + throws HttpClientErrorException { + + String content = "test-msg"; + String urlHttp = "http://valid-url"; + + when( + restTemplate.postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ) + ).thenReturn(response); + + adapter.send(content, urlHttp); + + verify( + restTemplate, times(1)).postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ); + verify(response).getStatusCode(); + } + + @Test + void send_should_successfully_send_request_given_valid_https_url() + throws HttpClientErrorException { + + String content = "test-msg"; + String urlHttps = "https://valid-url"; + + when( + restTemplate.postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ) + ).thenReturn(response); + + adapter.send(content, urlHttps); + + verify( + restTemplate, times(1)).postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ); + verify(response).getStatusCode(); + } + + @Test + void send_should_not_send_request_given_invalid_url() + throws HttpClientErrorException { + + String content = "test-msg"; + String url = "http://invalid-url"; + + doThrow( + new HttpClientErrorException(HttpStatus.BAD_REQUEST)).when( + restTemplate).postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ); + + adapter.send(content, url); + + verify( + restTemplate, times(1)).postForEntity( + (String) notNull(), + (HttpEntity) notNull(), + eq(String.class) + ); + verify(response, never()).getStatusCode(); + } +} diff --git a/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/validation/JSONValidatorTest.java b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/validation/JSONValidatorTest.java new file mode 100644 index 0000000..30dfe06 --- /dev/null +++ b/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/validation/JSONValidatorTest.java @@ -0,0 +1,123 @@ +/* + * ============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.validation; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.github.fge.jsonschema.core.exceptions.InvalidSchemaException; +import com.github.fge.jsonschema.core.exceptions.ProcessingException; +import java.io.IOException; +import java.net.URL; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class JSONValidatorTest { + + private final static String VALID_SCHEMA_NAME = "valid-test-schema.json"; + private final static String INVALID_SCHEMA_NAME = "invalid-test-schema.json"; + + private JSONValidator validator; + + @BeforeEach + void setUp() { + validator = new JSONValidator(); + } + + @Test + void validate_should_not_throw_given_valid_json() throws ProcessingException, IOException, ValidationException { + validator.validate(getValidJsonString(), getResourcePath(VALID_SCHEMA_NAME)); + } + + @Test + void validate_should_not_throw_when_optional_parameter_missing() + throws ProcessingException, IOException, ValidationException { + + String invalidJsonString = new JSONObject() + .put("key1", "value1") + .put("key2", "value2") + .toString(); + + validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME)); + } + + @Test + void validate_should_throw_when_mandatory_parameter_missing() { + + String invalidJsonString = new JSONObject() + .put("key1", "value1") + .put("key3", "value3") + .toString(); + + assertThrows( + ValidationException.class, + () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME))); + } + + @Test + void validate_should_throw_when_invalid_json_format() { + String invalidJsonString = "{" + + "\"key1\": \"value1\"" + + "\"key2\": \"value2" + + "}"; + + assertThrows( + IOException.class, + () -> validator.validate(invalidJsonString, getResourcePath(VALID_SCHEMA_NAME))); + } + + @Test + void validate_should_throw_when_invalid_schema_format() { + assertThrows( + InvalidSchemaException.class, + () -> validator.validate(getValidJsonString(), getResourcePath(INVALID_SCHEMA_NAME))); + } + + @Test + void validate_should_throw_when_invalid_schema_path() { + + assertThrows( + IOException.class, + () -> validator.validate(getValidJsonString(), "/not/existing/path/schema.json")); + } + + private String getResourcePath(String schemaFileName) { + URL result = getClass() + .getClassLoader() + .getResource(schemaFileName); + + if (result == null) { + throw new IllegalArgumentException("Given file doesn't exist"); + } else { + return result + .toString() + .replace("file:", ""); + } + } + + private String getValidJsonString() { + return new JSONObject() + .put("key1", "value1") + .put("key2", "value2") + .put("key3", "value3") + .toString(); + } +}
\ No newline at end of file |