diff options
author | Edyta Krukowska <edyta.krukowska@nokia.com> | 2021-02-23 15:02:59 +0100 |
---|---|---|
committer | Edyta Krukowska <edyta.krukowska@nokia.com> | 2021-02-24 11:40:48 +0100 |
commit | 9c719934d618b5c989522d701f653aa31a85325c (patch) | |
tree | b947db409d79613d9426bbe5ffe5bfafaaf1c725 /src/test/java/org | |
parent | 4c67640edccbbd1820fb658370cad0f666a0e210 (diff) |
Use updated CommonEventSchema in VES Collector1.8.0
Issue-ID: DCAEGEN2-2477
Signed-off-by: Edyta Krukowska <edyta.krukowska@nokia.com>
Change-Id: I8c5fbbd17ebb3a5e1b96b5ac4f4ab3632fd2c484
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java | 115 |
1 files changed, 78 insertions, 37 deletions
diff --git a/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java b/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java index e5e21177..2ac4cb1a 100644 --- a/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java +++ b/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java @@ -23,11 +23,13 @@ package org.onap.dcae.common.validator; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; import org.json.JSONObject; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -37,7 +39,9 @@ import org.onap.dcae.common.model.VesEvent; import org.onap.dcae.restapi.ApiException; import org.onap.dcae.restapi.EventValidatorException; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; @@ -49,6 +53,7 @@ class GeneralEventValidatorTest { private static final String DUMMY_SCHEMA_VERSION = "v5"; private static final String DUMMY_TYPE = "type"; private final String newSchemaV7 = FileReader.readFileAsString("etc/CommonEventFormat_30.2_ONAP.json"); + private final String schemaWithIP = FileReader.readFileAsString("etc/CommonEventFormat_30.2.1_ONAP.json"); private JSONObject sentEvent; private static final String V7_VERSION = "v7"; private static JSONObject jsonObject; @@ -57,7 +62,7 @@ class GeneralEventValidatorTest { @Mock private ApplicationSettings settings; - private SchemaValidator schemaValidator = Mockito.spy( new SchemaValidator()); + private final SchemaValidator schemaValidator = Mockito.spy(new SchemaValidator()); private GeneralEventValidator sut; @@ -68,7 +73,7 @@ class GeneralEventValidatorTest { } @BeforeEach - public void setUp(){ + public void setUp() { this.sut = new GeneralEventValidator(settings, schemaValidator); } @@ -91,13 +96,9 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(jsonObject), "wrongType", DUMMY_SCHEMA_VERSION); - } catch (EventValidatorException e) { - //then - Assertions.assertEquals(ApiException.INVALID_JSON_INPUT, e.getApiException()); - } - + Executable testedMethod = () -> sut.validate(new VesEvent(jsonObject), "wrongType", DUMMY_SCHEMA_VERSION); + EventValidatorException thrownException = assertThrows(EventValidatorException.class, testedMethod); + assertEquals(ApiException.INVALID_JSON_INPUT, thrownException.getApiException()); } @@ -109,15 +110,13 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); - } catch (EventValidatorException e) { - //then - assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException()); - } + Executable testedMethod = () -> sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + EventValidatorException thrownException = assertThrows(EventValidatorException.class, testedMethod); + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, thrownException.getApiException()); } + @Test void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { //given @@ -126,11 +125,7 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); - } catch (EventValidatorException e) { - failWithError(); - } + assertDoesNotThrow(() -> sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION)); } @Test @@ -142,11 +137,7 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); - } catch (EventValidatorException e) { - failWithError(); - } + assertDoesNotThrow(() -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION)); } @Test @@ -158,11 +149,7 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); - } catch (EventValidatorException e) { - failWithError(); - } + assertDoesNotThrow(() -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION)); } @Test @@ -174,14 +161,68 @@ class GeneralEventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - try { - sut.validate(new VesEvent(this.sentEvent), EVENT_TYPE, V7_VERSION); - } catch (EventValidatorException e) { - //then - assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException()); - } + Executable testedMethod = () -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + EventValidatorException thrownException = assertThrows(EventValidatorException.class, testedMethod); + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, thrownException.getApiException()); + } + + @Test + void shouldReturnNoErrorWhenIPv4ValidInLongFrom() { + //given + mockJsonSchema(schemaWithIP); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_ip_v4.json")); + //when + assertDoesNotThrow(() -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION)); + + } + + @ParameterizedTest(name = "{0}") + @ValueSource(strings = {"ves7_invalid_ip_v4_with_ipv6_format.json", "ves7_invalid_ipv4.json"}) + void shouldReturnSchemaValidationErrorWhenIPv4Invalid(String filename) { + //given + mockJsonSchema(schemaWithIP); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/" + filename)); + //when + Executable testedMethod = () -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + EventValidatorException thrownException = assertThrows(EventValidatorException.class, testedMethod); + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, thrownException.getApiException()); + } + @ParameterizedTest(name = "{0}") + @ValueSource(strings = {"ves7_valid_ip_v6_with_zone_index.json", + "ves7_valid_ip_v6.json", "ves7_valid_ip_v6_short_one.json", + "ves7_valid_ip_v6_full.json", "ves7_valid_ip_v6_short_without_end.json", + "ves7_valid_ip_v6_short_with_big_letters.json", "ves7_valid_ip_v6_multicast_example.json", + "ves7_valid_ip_v6_ipv4_translated.json"}) + void shouldReturnNoErrorWhenIPv6Valid(String filename) { + //given + mockJsonSchema(schemaWithIP); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/" + filename)); + //when + assertDoesNotThrow(() -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION)); + } + @ParameterizedTest(name = "{0}") + @ValueSource(strings = {"ves7_invalid_ip_v6_short_with_more_than_two_colons.json", + "ves7_invalid_ip_v6_with_ipv4_format.json", + "ves7_invalid_ip_v6_short_with_too_many_colons.json", + "ves7_invalid_ip_v6_with_one_colon_at_begining.json", + "ves7_invalid_ip_v6_double_colon_more_than_once.json", + "ves7_invalid_ip_v6_out_of_range.json" + }) + void shouldReturnSchemaValidationErrorWhenIPv6Invalid(String filename) { + //given + String schema = schemaWithIP; + mockJsonSchema(schema); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/" + filename)); + //when + Executable testedMethod = () -> sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + EventValidatorException thrownException = assertThrows(EventValidatorException.class, testedMethod); + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, thrownException.getApiException()); } private void failWithError() { |