From f120b3d36cc6dbab97acc0020ddb9e03e35766cd Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Wed, 13 Mar 2019 18:09:51 +0000 Subject: Improve error message for unknown entity-type When the Rule-driven Validator cannot find any rules for the event's entity type, include the entity type in the content of the error message that is logged and thrown as an Exception. Change-Id: If037a0d512f690350caa169d3af2f0f152ac233b Issue-ID: AAI-2259 Signed-off-by: mark.j.leonard --- .../exception/ValidationServiceError.java | 68 +++++++------- .../validation/ruledriven/RuleDrivenValidator.java | 5 +- .../ruledriven/mock/TestDefaultRules.java | 102 ++++++++++++++------- 3 files changed, 106 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/onap/aai/validation/exception/ValidationServiceError.java b/src/main/java/org/onap/aai/validation/exception/ValidationServiceError.java index 45d79d4..421d866 100644 --- a/src/main/java/org/onap/aai/validation/exception/ValidationServiceError.java +++ b/src/main/java/org/onap/aai/validation/exception/ValidationServiceError.java @@ -1,37 +1,41 @@ -/* - * ============LICENSE_START=================================================== - * Copyright (c) 2018 Amdocs - * ============================================================================ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2018-2019 European Software Marketing Ltd. + * ================================================================================ * 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 + * 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===================================================== + * ============LICENSE_END========================================================= */ + package org.onap.aai.validation.exception; import java.text.MessageFormat; /** - * Error text formatting + * Error text formatting. * */ public enum ValidationServiceError { - //@formatter:off + //@formatter:off // Rule Configuration exceptions. Range 100..199 RULES_FILE_ERROR("VS-100", "Error reading rules configuration file(s) {0}"), RULE_UNEXPECTED_TOKEN("VS-101", "Token {0} unexpected in rules configuration file."), - RULES_NOT_DEFINED("VS-102", "Event type {0} has no rule definitions."), - + RULES_NOT_DEFINED("VS-102", "Entity type {0} (Event type {1}) has no rule definitions."), + // Rule exceptions. Range 200..299 RULE_EXECUTION_ERROR("VS-201", "Error executing rule {0} with arguments {1}"), @@ -78,26 +82,26 @@ public enum ValidationServiceError { //@formatter:on - private String id; - private String message; - - private ValidationServiceError(String id, String message) { - this.id = id; - this.message = message; - } - - public String getId() { - return this.id; - } - - /** - * @param args - * to be formatted - * @return the formatted error message - */ - public String getMessage(Object... args) { - MessageFormat formatter = new MessageFormat(""); - formatter.applyPattern(this.message); - return formatter.format(args); - } + private String id; + private String message; + + private ValidationServiceError(String id, String message) { + this.id = id; + this.message = message; + } + + public String getId() { + return this.id; + } + + /** + * @param args + * to be formatted + * @return the formatted error message + */ + public String getMessage(Object... args) { + MessageFormat formatter = new MessageFormat(""); + formatter.applyPattern(this.message); + return formatter.format(args); + } } diff --git a/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java b/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java index 5ca7025..1c6dff7 100644 --- a/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java +++ b/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java @@ -133,8 +133,9 @@ public class RuleDrivenValidator implements Validator { Entity entity = getEventReader().getEntity(event); Optional eventType = eventReader.getEventType(event); - List rules = getRulesToApply(entity, eventType).orElseThrow( - () -> new ValidationServiceException(ValidationServiceError.RULES_NOT_DEFINED, eventType.orElse(null))); + List rules = getRulesToApply(entity, eventType) + .orElseThrow(() -> new ValidationServiceException(ValidationServiceError.RULES_NOT_DEFINED, + entity.getType(), eventType.orElse(null))); ValidationResult validationResult = new ValidationResultBuilder(eventReader, event).build(); Violation.Builder builder = new Violation.Builder(entity); diff --git a/src/test/java/org/onap/aai/validation/ruledriven/mock/TestDefaultRules.java b/src/test/java/org/onap/aai/validation/ruledriven/mock/TestDefaultRules.java index f2ee3c9..b87e4af 100644 --- a/src/test/java/org/onap/aai/validation/ruledriven/mock/TestDefaultRules.java +++ b/src/test/java/org/onap/aai/validation/ruledriven/mock/TestDefaultRules.java @@ -54,7 +54,8 @@ import org.onap.aai.validation.result.Violation; import org.onap.aai.validation.ruledriven.RuleDrivenValidator; /** - * Test that the rules present under bundleconfig/etc/rules/ can be loaded and evaluated (using a mocked event). + * Test that the rules present under bundleconfig/etc/rules/ can be loaded and evaluated (using a mocked event). Test + * for both a supported and an unsupported entity type. * */ @RunWith(MockitoJUnitRunner.class) @@ -65,14 +66,38 @@ public class TestDefaultRules { } enum TestCase { - NULL, VSERVER; + NULL(null), VSERVER("aai-event", "vserver"), UNKNOWN("aai-event", "unknown"); + + private Optional eventType; + private String entityType; + + TestCase(String eventType, String entityType) { + this(eventType); + this.entityType = entityType; + } + + TestCase(String eventType) { + if (eventType != null) { + this.eventType = Optional.of(eventType); + } + } + + public Optional getEventType() { + return eventType; + } + + public String getEntityType() { + return entityType; + } } - // Data returned by the mocked EventReader + /** + * Data returned by the mocked EventReader. Currently this applies to the vserver test only. + * + */ enum TestData { // @formatter:off ENTITTY_DATA ("vserver dummy json data"), - ENTITTY_TYPE ("vserver"), RESOURCE_VERSION_VALUE("1476735182"), VSERVER_ID_KEY ("vserver-id"), VSERVER_ID_VALUE ("13b629a4-87ae-492d-943f-acb8f3d9c3d9"); @@ -91,22 +116,52 @@ public class TestDefaultRules { @Mock private Entity entity; + /** + * A single instance shared by each test. + */ private Validator ruleDrivenValidator; + @Before + public void createRuleDrivenValidator() throws ValidationServiceException { + List configurationPaths = Collections.singletonList(Paths.get("bundleconfig/etc/rules")); + ruleDrivenValidator = new RuleDrivenValidator(configurationPaths, null, eventReader, null); + } + + @Test + public void testExecuteRulesForVserver() throws Exception { + createMockEventReader(TestCase.VSERVER); + + List results = ruleDrivenValidator.validate(TestCase.VSERVER.name()); + assertThat(results.size(), is(1)); + + ValidationResult validationResult = results.get(0); + assertThat(validationResult.getEntityType(), is(equalTo(TestCase.VSERVER.getEntityType()))); + JsonObject expectedEntityId = new JsonObject(); + expectedEntityId.addProperty(TestData.VSERVER_ID_KEY.value, TestData.VSERVER_ID_VALUE.value); + assertThat(validationResult.getEntityId(), is(equalTo(expectedEntityId))); + assertThat(validationResult.getViolations().size(), is(2)); + + Violation violation = validationResult.getViolations().get(0); + assertThat(violation.getCategory(), is(equalTo("MISSING_REL"))); + } + + @Test(expected = ValidationServiceException.class) + public void testExecuteRulesForUnsupportedEntityType() throws ValidationServiceException { + createMockEventReader(TestCase.UNKNOWN); + ruleDrivenValidator.validate(TestCase.UNKNOWN.name()); + } + /** * @throws ValidationServiceException - * if mocking a JSON parsing exception + * if mocking a JSON parsing exception */ - @Before - public void createMockEventReader() throws ValidationServiceException { - when(eventReader.getEventType(TestCase.VSERVER.name())).thenReturn(Optional.of("aai-event")); - when(eventReader.getEventType(TestCase.NULL.name())).thenReturn(null); - - when(eventReader.getEntityType(anyString())).thenReturn(Optional.of(TestData.ENTITTY_TYPE.value)); + private void createMockEventReader(TestCase testCase) throws ValidationServiceException { + when(eventReader.getEventType(testCase.name())).thenReturn(testCase.getEventType()); + when(eventReader.getEntityType(anyString())).thenReturn(Optional.ofNullable(testCase.getEntityType())); when(eventReader.getEntity(anyString())).thenReturn(entity); // Mocked entity returned by the event reader - when(entity.getType()).thenReturn(TestData.ENTITTY_TYPE.value); + when(entity.getType()).thenReturn(testCase.getEntityType()); when(entity.getResourceVersion()).thenReturn(Optional.of(TestData.RESOURCE_VERSION_VALUE.value)); EntityId entityId = new EntityId(TestData.VSERVER_ID_KEY.value, TestData.VSERVER_ID_VALUE.value); @@ -129,27 +184,4 @@ public class TestDefaultRules { } }); } - - @Before - public void createRuleDrivenValidator() throws ValidationServiceException { - List configurationPaths = Collections.singletonList(Paths.get("bundleconfig/etc/rules")); - ruleDrivenValidator = new RuleDrivenValidator(configurationPaths, null, eventReader, null); - } - - @Test - public void testExecuteRulesForVserver() throws Exception { - List results = ruleDrivenValidator.validate(TestCase.VSERVER.name()); - assertThat(results.size(), is(1)); - - ValidationResult validationResult = results.get(0); - assertThat(validationResult.getEntityType(), is(equalTo(TestData.ENTITTY_TYPE.value))); - JsonObject expectedEntityId = new JsonObject(); - expectedEntityId.addProperty(TestData.VSERVER_ID_KEY.value, TestData.VSERVER_ID_VALUE.value); - assertThat(validationResult.getEntityId(), is(equalTo(expectedEntityId))); - assertThat(validationResult.getViolations().size(), is(2)); - - Violation violation = validationResult.getViolations().get(0); - assertThat(violation.getCategory(), is(equalTo("MISSING_REL"))); - } - } -- cgit 1.2.3-korg