diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-07-22 08:19:51 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-08-06 08:07:39 +0200 |
commit | f6260a26de44a9338ca998626a93c0d0fa56abc3 (patch) | |
tree | 2a3dffaf850a7a934861c0fd10003168324c9f69 /src/test/java/org/onap/dcae/restapi/EventValidatorTest.java | |
parent | dc29687c2c83b387718c6a26b227170b17dcf08d (diff) |
StndDefined event routing
Route stndDefined events to streams defined in namespace event field.
Change-Id: I3963e220095665f8ca3fd1b21c5c20b44057cf76
Issue-ID: DCAEGEN2-1771
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Diffstat (limited to 'src/test/java/org/onap/dcae/restapi/EventValidatorTest.java')
-rw-r--r-- | src/test/java/org/onap/dcae/restapi/EventValidatorTest.java | 113 |
1 files changed, 66 insertions, 47 deletions
diff --git a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java index 0d38fdac..0ca5c424 100644 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java @@ -20,28 +20,29 @@ package org.onap.dcae.restapi; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; - -import java.util.Optional; - 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.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcae.ApplicationSettings; import org.onap.dcae.FileReader; -import org.springframework.http.ResponseEntity; +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) -public class EventValidatorTest { +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"); @@ -51,10 +52,11 @@ public class EventValidatorTest { private static final String EVENT_TYPE = "event"; @Mock - private static ApplicationSettings settings; + private ApplicationSettings settings; + + private SchemaValidator schemaValidator = spy( new SchemaValidator()); - @InjectMocks - private static EventValidator sut; + private EventValidator sut; @BeforeAll @@ -62,57 +64,70 @@ public class EventValidatorTest { jsonObject = new JSONObject("{" + DUMMY_TYPE + ":dummy}"); } + @BeforeEach + public void setUp(){ + this.sut = new EventValidator(settings, schemaValidator); + } + @Test - public void shouldReturnEmptyOptionalOnJsonSchemaValidationDisabled() { + void shouldNotValidateEventWhenJsonSchemaValidationDisabled() throws EventValidatorException { //given when(settings.eventSchemaValidationEnabled()).thenReturn(false); //when - Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + this.sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); //then - assertEquals(Optional.empty(), result); + verify(schemaValidator, never()).conformsToSchema(any(), any()); } @Test - public void shouldReturnInvalidJsonErrorOnWrongType() { + void shouldReturnInvalidJsonErrorOnWrongType() { //given when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(jsonObject, "wrongType", DUMMY_SCHEMA_VERSION); + try { + sut.validate(new VesEvent(jsonObject), "wrongType", DUMMY_SCHEMA_VERSION); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.INVALID_JSON_INPUT, e.getApiException()); + } + - //then - assertEquals(generateResponseOptional(ApiException.INVALID_JSON_INPUT), result); } @Test - public void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() { + void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() { //given String schemaRejectingEverything = "{\"not\":{}}"; mockJsonSchema(schemaRejectingEverything); when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + try { + sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException()); + } - //then - assertEquals(generateResponseOptional(ApiException.SCHEMA_VALIDATION_FAILED), result); } @Test - public void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { + void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { //given String schemaAcceptingEverything = "{}"; mockJsonSchema(schemaAcceptingEverything); when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); - - //then - assertEquals(Optional.empty(), result); + try { + sut.validate(new VesEvent(jsonObject), DUMMY_TYPE, DUMMY_SCHEMA_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } } @Test @@ -124,14 +139,15 @@ public class EventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(sentEvent, EVENT_TYPE, V7_VERSION); - - //then - assertEquals(Optional.empty(), result); + try { + sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } } @Test - public void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() { + void shouldReturnNoErrorsWhenValidatingValidEventWithStndDefinedFields() { //given sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_valid_eventWithStndDefinedFields.json")); @@ -139,14 +155,15 @@ public class EventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(sentEvent, EVENT_TYPE, V7_VERSION); - - //then - assertEquals(Optional.empty(), result); + try { + sut.validate(new VesEvent(sentEvent), EVENT_TYPE, V7_VERSION); + } catch (EventValidatorException e) { + failWithError(); + } } @Test - public void shouldReturnSchemaValidationFailedWhenValidating30_1_1InvalidEvent() { + void shouldReturnSchemaValidationFailedWhenValidating30_1_1InvalidEvent() { //given sentEvent = new JSONObject(FileReader.readFileAsString("src/test/resources/ves7_invalid_30_1_1_event.json")); @@ -154,12 +171,19 @@ public class EventValidatorTest { when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when - Optional<ResponseEntity<String>> result = sut.validate(this.sentEvent, EVENT_TYPE, V7_VERSION); + try { + sut.validate(new VesEvent(this.sentEvent), EVENT_TYPE, V7_VERSION); + } catch (EventValidatorException e) { + //then + assertEquals(ApiException.SCHEMA_VALIDATION_FAILED, e.getApiException()); + } + - //then - assertEquals(generateResponseOptional(ApiException.SCHEMA_VALIDATION_FAILED), result); } + private void failWithError() { + fail("Validation should not report any error!"); + } private void mockJsonSchema(String jsonSchemaContent) { JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); @@ -167,9 +191,4 @@ public class EventValidatorTest { JsonSchema schema = factory.getSchema(jsonSchemaContent); when(settings.jsonSchema(any())).thenReturn(schema); } - - private Optional<ResponseEntity<String>> generateResponseOptional(ApiException schemaValidationFailed) { - return Optional.of(ResponseEntity.status(schemaValidationFailed.httpStatusCode) - .body(schemaValidationFailed.toJSON().toString())); - } } |