From ab6421bcf7b480f9955b6b4376554ec9677e71ed Mon Sep 17 00:00:00 2001 From: awudzins Date: Mon, 13 Jan 2020 11:43:08 +0100 Subject: Changed EventTransformation schema to be loaded only once Issue-ID: DCAEGEN2-1774 Signed-off-by: adamwudzinski Change-Id: I50f3da2c11201a40be948ab199aaca89bbbb38db --- .../org/onap/dcae/restapi/EventValidatorTest.java | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'src/test/java/org/onap/dcae/restapi/EventValidatorTest.java') diff --git a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java index 4ac3c487..53595100 100644 --- a/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java +++ b/src/test/java/org/onap/dcae/restapi/EventValidatorTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2019 Nokia. All rights reserved. + * 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. @@ -20,8 +20,13 @@ 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.Test; @@ -32,13 +37,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcae.ApplicationSettings; import org.springframework.http.ResponseEntity; -import java.io.IOException; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) public class EventValidatorTest { private static final String DUMMY_SCHEMA_VERSION = "v5"; @@ -60,7 +58,7 @@ public class EventValidatorTest { @Test public void shouldReturnEmptyOptionalOnJsonSchemaValidationDisabled() { //given - when(settings.jsonSchemaValidationEnabled()).thenReturn(false); + when(settings.eventSchemaValidationEnabled()).thenReturn(false); //when Optional> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -73,7 +71,7 @@ public class EventValidatorTest { @Test public void shouldReturnInvalidJsonErrorOnWrongType() { //given - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional> result = sut.validate(jsonObject, "wrongType", DUMMY_SCHEMA_VERSION); @@ -83,11 +81,11 @@ public class EventValidatorTest { } @Test - public void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() throws IOException { + public void shouldReturnSchemaValidationFailedErrorOnInvalidJsonObjectSchema() { //given String schemaRejectingEverything = "{\"not\":{}}"; mockJsonSchema(schemaRejectingEverything); - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -97,11 +95,11 @@ public class EventValidatorTest { } @Test - public void shouldReturnEmptyOptionalOnValidJsonObjectSchema() throws IOException { + public void shouldReturnEmptyOptionalOnValidJsonObjectSchema() { //given String schemaAcceptingEverything = "{}"; mockJsonSchema(schemaAcceptingEverything); - when(settings.jsonSchemaValidationEnabled()).thenReturn(true); + when(settings.eventSchemaValidationEnabled()).thenReturn(true); //when Optional> result = sut.validate(jsonObject, DUMMY_TYPE, DUMMY_SCHEMA_VERSION); @@ -110,6 +108,7 @@ public class EventValidatorTest { assertEquals(Optional.empty(), result); } + private void mockJsonSchema(String jsonSchemaContent) { JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); @@ -121,4 +120,4 @@ public class EventValidatorTest { return Optional.of(ResponseEntity.status(schemaValidationFailed.httpStatusCode) .body(schemaValidationFailed.toJSON().toString())); } -} \ No newline at end of file +} -- cgit 1.2.3-korg