diff options
author | Michal Banka <michal.banka@nokia.com> | 2020-08-04 14:58:25 +0200 |
---|---|---|
committer | Edyta Krukowska <edyta.krukowska@nokia.com> | 2020-08-19 15:07:14 +0200 |
commit | a0ba464faeb2e979d20758bc1091143108355974 (patch) | |
tree | 944b0f588daace51449b50ae94816f33a6a3c62d /src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java | |
parent | f1ea637a60bace906db5619d71a914ad601e9478 (diff) |
Add implementation of stndDefined fields validation1.7.3
Added implementation of stndDefined fields from incoming events.
Validation is performed using external-schema-manager tool from DCAE SDK.
StndDefined fields schemas are stored in etc/externalRepo directory.
Additional stndDefined related properties has been added to collector.properties.
VES version has been set to 1.7.3.
Issue-ID: DCAEGEN2-2254
Signed-off-by: Edyta Krukowska <edyta.krukowska@nokia.com>
Signed-off-by: Michal Banka <michal.banka@nokia.com>
Change-Id: Iedaa3622b1d527f6794822c8867b9dfd1860bb8f
Diffstat (limited to 'src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java')
-rw-r--r-- | src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java | 197 |
1 files changed, 197 insertions, 0 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 new file mode 100644 index 00000000..e5e21177 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/validator/GeneralEventValidatorTest.java @@ -0,0 +1,197 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 Nokia. 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.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.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcae.ApplicationSettings; +import org.onap.dcae.FileReader; +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.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +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 JSONObject sentEvent; + private static final String V7_VERSION = "v7"; + private static JSONObject jsonObject; + private static final String EVENT_TYPE = "event"; + + @Mock + private ApplicationSettings settings; + + private SchemaValidator schemaValidator = Mockito.spy( new SchemaValidator()); + + private GeneralEventValidator sut; + + + @BeforeAll + static void setupTests() { + jsonObject = new JSONObject("{" + DUMMY_TYPE + ":dummy}"); + } + + @BeforeEach + public void setUp(){ + this.sut = new GeneralEventValidator(settings, schemaValidator); + } + + @Test + void shouldNotValidateEventWhenJsonSchemaValidationDisabled() throws EventValidatorException { + //given + when(settings.eventSchemaValidationEnabled()).thenReturn(false); + + //when + this.sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + + //then + verify(schemaValidator, never()).conformsToSchema(any(), any()); + + } + + @Test + void shouldReturnInvalidJsonErrorOnWrongType() { + //given + 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()); + } + + + } + + @Test + void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() { + //given + String schemaRejectingEverything = "{\"not\":{}}"; + mockJsonSchema(schemaRejectingEverything); + 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()); + } + + } + + @Test + void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { + //given + String schemaAcceptingEverything = "{}"; + mockJsonSchema(schemaAcceptingEverything); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + try { + sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } + } + + @Test + public void shouldReturnNoErrorsWhenValidating30_1_1ValidEvent() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_30_1_1_event.json")); + + mockJsonSchema(newSchemaV7); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + try { + sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } + } + + @Test + void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves_stdnDefined_valid.json")); + + mockJsonSchema(newSchemaV7); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); + + //when + try { + sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } + } + + @Test + void shouldReturnSchemaValidationFailedWhenValidating30_1_1InvalidEvent() { + //given + sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_invalid_30_1_1_event.json")); + + mockJsonSchema(newSchemaV7); + 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()); + } + + + } + + private void failWithError() { + fail("Validation should not report any error!"); + } + + private void mockJsonSchema(String jsonSchemaContent) { + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); + + JsonSchema schema = factory.getSchema(jsonSchemaContent); + when(settings.jsonSchema(any())).thenReturn(schema); + } +}
\ No newline at end of file |