summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap
diff options
context:
space:
mode:
authormark.j.leonard <mark.j.leonard@gmail.com>2019-03-13 18:09:51 +0000
committermark.j.leonard <mark.j.leonard@gmail.com>2019-03-13 18:11:47 +0000
commitf120b3d36cc6dbab97acc0020ddb9e03e35766cd (patch)
treee23af9505da0f85705307facebcb7b9c92880899 /src/test/java/org/onap
parentf4f3d47609349f511ff6ff1a8589ab5cc181d92c (diff)
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 <mark.j.leonard@gmail.com>
Diffstat (limited to 'src/test/java/org/onap')
-rw-r--r--src/test/java/org/onap/aai/validation/ruledriven/mock/TestDefaultRules.java102
1 files changed, 67 insertions, 35 deletions
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<String> 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<String> 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<Path> 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<ValidationResult> 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<Path> configurationPaths = Collections.singletonList(Paths.get("bundleconfig/etc/rules"));
- ruleDrivenValidator = new RuleDrivenValidator(configurationPaths, null, eventReader, null);
- }
-
- @Test
- public void testExecuteRulesForVserver() throws Exception {
- List<ValidationResult> 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")));
- }
-
}