From a0ba464faeb2e979d20758bc1091143108355974 Mon Sep 17 00:00:00 2001 From: Michal Banka Date: Tue, 4 Aug 2020 14:58:25 +0200 Subject: Add implementation of stndDefined fields validation 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 Signed-off-by: Michal Banka Change-Id: Iedaa3622b1d527f6794822c8867b9dfd1860bb8f --- .../org/onap/dcae/restapi/EventValidatorTest.java | 194 --------------------- .../onap/dcae/restapi/VesRestControllerTest.java | 49 +++++- 2 files changed, 44 insertions(+), 199 deletions(-) delete mode 100644 src/test/java/org/onap/dcae/restapi/EventValidatorTest.java (limited to 'src/test/java/org/onap/dcae/restapi') diff --git a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java deleted file mode 100644 index 0ca5c424..00000000 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ /dev/null @@ -1,194 +0,0 @@ -/*- - * ============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.restapi; - -import com.networknt.schema.JsonSchema; -import com.networknt.schema.JsonSchemaFactory; -import org.json.JSONObject; -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.junit.jupiter.MockitoExtension; -import org.onap.dcae.ApplicationSettings; -import org.onap.dcae.FileReader; -import org.onap.dcae.common.model.VesEvent; - -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.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class EventValidatorTest { - 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 = spy( new SchemaValidator()); - - private EventValidator sut; - - - @BeforeAll - static void setupTests() { - jsonObject = new JSONObject("{" + DUMMY_TYPE + ":dummy}"); - } - - @BeforeEach - public void setUp(){ - this.sut = new EventValidator(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 - 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/ves7_valid_eventWithStndDefinedFields.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); - } -} diff --git a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java index 10423053..765f2b43 100644 --- a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java +++ b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java @@ -40,6 +40,7 @@ import org.onap.dcae.common.EventSender; import org.onap.dcae.common.EventTransformation; import org.onap.dcae.common.HeaderUtils; import org.onap.dcae.common.JsonDataLoader; +import org.onap.dcae.common.validator.StndDefinedDataValidator; import org.onap.dcae.common.publishing.EventPublisher; import org.slf4j.Logger; import org.springframework.http.ResponseEntity; @@ -85,16 +86,17 @@ public class VesRestControllerTest { @Mock private EventPublisher eventPublisher; + @Mock + private StndDefinedDataValidator stndDefinedDataValidator; + @Before public void setUp(){ - final HashMap streamIds = HashMap.of( "fault", new String[]{VES_FAULT_TOPIC}, "3GPP-FaultSupervision", new String[]{VES_3_GPP_FAULT_SUPERVISION_TOPIC} ); - this.vesRestController = new VesRestController( - applicationSettings, logger, new EventSender(eventPublisher, streamIds),headerUtils - ); + this.vesRestController = new VesRestController(applicationSettings, logger, + new EventSender(eventPublisher, streamIds), headerUtils, stndDefinedDataValidator); } @Test @@ -229,7 +231,6 @@ public class VesRestControllerTest { configureHeadersForEventListener(); MockHttpServletRequest request = givenMockHttpServletRequest(); - String validEvent = JsonDataLoader.loadContent("/ves_stdnDefined_valid_unknown_topic.json"); //when @@ -241,6 +242,44 @@ public class VesRestControllerTest { verifyThatEventWasNotSend(); } + @Test + public void shouldExecuteStndDefinedValidationWhenFlagIsOnTrue() throws IOException { + //given + configureEventTransformations(); + configureHeadersForEventListener(); + + MockHttpServletRequest request = givenMockHttpServletRequest(); + String validEvent = JsonDataLoader.loadContent("/ves7_batch_with_stndDefined_valid.json"); + when(applicationSettings.getExternalSchemaValidationCheckflag()).thenReturn(true); + + //when + final ResponseEntity response = vesRestController.events(validEvent, VERSION_V7, request); + + //then + assertThat(response.getStatusCodeValue()).isEqualTo(HttpStatus.SC_ACCEPTED); + assertThat(response.getBody()).isEqualTo(ACCEPTED); + verify(stndDefinedDataValidator, times(2)).validate(any()); + } + + @Test + public void shouldNotExecuteStndDefinedValidationWhenFlagIsOnFalse() throws IOException { + //given + configureEventTransformations(); + configureHeadersForEventListener(); + + MockHttpServletRequest request = givenMockHttpServletRequest(); + String validEvent = JsonDataLoader.loadContent("/ves7_batch_with_stndDefined_valid.json"); + when(applicationSettings.getExternalSchemaValidationCheckflag()).thenReturn(false); + + //when + final ResponseEntity response = vesRestController.events(validEvent, VERSION_V7, request); + + //then + assertThat(response.getStatusCodeValue()).isEqualTo(HttpStatus.SC_ACCEPTED); + assertThat(response.getBody()).isEqualTo(ACCEPTED); + verify(stndDefinedDataValidator, times(0)).validate(any()); + } + private void verifyThatEventWasNotSend() { verify(eventPublisher, never()).sendEvent(any(), any()); } -- cgit 1.2.3-korg