Add a JUnit test for the Data Dictionary 04/78404/1
authormark.j.leonard <mark.j.leonard@gmail.com>
Wed, 13 Feb 2019 15:44:48 +0000 (15:44 +0000)
committermark.j.leonard <mark.j.leonard@gmail.com>
Wed, 13 Feb 2019 15:47:23 +0000 (15:47 +0000)
Create sample configuration for debugging the DataDictionary.java code.
Fix an issue in the Rule Driven Validator: re-enable invocation of the
default rules when no indexed rules are found for an event.

Change-Id: I85fb6c0797e1240c14997b6e8ba1f6c0304152db
Issue-ID: AAI-2057
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java
src/test/java/org/onap/aai/validation/ruledriven/validator/TestDataDictionary.java [new file with mode: 0644]
src/test/resources/data-dictionary/rule-data-dictionary.properties [new file with mode: 0644]
src/test/resources/data-dictionary/rule-indexing.properties [new file with mode: 0644]
src/test/resources/data-dictionary/rules/poa-event/default-rules.groovy [new file with mode: 0644]
src/test/resources/data-dictionary/schemaIngest.properties [new file with mode: 0644]
src/test/resources/data-dictionary/test-data-dictionary-beans.xml [new file with mode: 0644]
src/test/resources/data-dictionary/test_events/test-create-event.json [new file with mode: 0644]
src/test/resources/data-dictionary/validation-service-auth.properties [new file with mode: 0644]
src/test/resources/data-dictionary/validation-service.properties [new file with mode: 0644]

index cee29f7..57ace7d 100644 (file)
@@ -1,20 +1,24 @@
-/*
- * ============LICENSE_START===================================================
+/**
+ * ============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.ruledriven;
 
 import java.io.File;
@@ -55,7 +59,7 @@ import org.onap.aai.validation.ruledriven.rule.Rule;
 import org.onap.aai.validation.ruledriven.rule.RuleResult;
 
 /**
- * Validator using explicit rules
+ * Validator using explicit rules.
  *
  */
 public class RuleDrivenValidator implements Validator {
@@ -71,16 +75,16 @@ public class RuleDrivenValidator implements Validator {
     // Map of event type name against RuleManager for that event type
     private Map<String, RuleManager> ruleManagers;
 
-
     /**
-     * Construct a Validator that is configured using rule files
+     * Construct a Validator that is configured using rule files.
      *
      * @param configurationPath
-     *        path to the Groovy rules files
+     *            path to the Groovy rules files
      * @param oxmReader
-     *        required for validating entity types
+     *            required for validating entity types
      * @param eventReader
-     *        a reader for extracting entities from each event to be validated
+     *            a reader for extracting entities from each event to be validated
+     * @param ruleIndexingConfig
      */
     public RuleDrivenValidator(final Path configurationPath, final OxmReader oxmReader, final EventReader eventReader,
             final RuleIndexingConfig ruleIndexingConfig) {
@@ -146,6 +150,7 @@ public class RuleDrivenValidator implements Validator {
 
     /*
      * (non-Javadoc)
+     * 
      * @see org.onap.aai.validation.Validator#validate(java.lang.String)
      */
     @Override
@@ -224,8 +229,8 @@ public class RuleDrivenValidator implements Validator {
 
         if (!rulesDefined && ruleIndexingConfig.isPresent()) {
             final String defaultIndexKey = ruleIndexingConfig.get().getDefaultIndexKey();
-            if (StringUtils.isEmpty(defaultIndexKey)) {
-                return ruleManager.getRulesForEntity(RuleManager.generateKey(new String[] { defaultIndexKey }));
+            if (!StringUtils.isEmpty(defaultIndexKey)) {
+                return ruleManager.getRulesForEntity(RuleManager.generateKey(new String[] {defaultIndexKey}));
             } else {
                 applicationLogger.debug("Default index value not configured, unable to get rules");
                 applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, eventType);
@@ -244,11 +249,10 @@ public class RuleDrivenValidator implements Validator {
             return "";
         }
         try {
-            AttributeValues attributeValues = entity.getAttributeValues(ruleIndexingConfig.get().getIndexAttributes());
-            applicationLogger
-                    .debug("Generating index using attributes: " + attributeValues.generateReport().toString());
-            Collection<Object> values = attributeValues.generateReport().values();
-            return RuleManager.generateKey(values.stream().toArray(String[]::new));
+            Map<String, Object> valuesMap =
+                    entity.getAttributeValues(ruleIndexingConfig.get().getIndexAttributes()).generateReport();
+            applicationLogger.debug("Generating index using attributes: " + valuesMap);
+            return RuleManager.generateKey(valuesMap.values().stream().toArray(String[]::new));
         } catch (ValidationServiceException e) {
             applicationLogger.debug("Failed to retrieve index key attributes from event: " + e.getMessage());
             applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, e, eventType);
@@ -271,7 +275,7 @@ public class RuleDrivenValidator implements Validator {
      * Read the text content of the specified Path and append this to the specified String
      *
      * @param sb
-     *        StringBuilder for the rule configuration text
+     *            StringBuilder for the rule configuration text
      * @return a Consumer function that appends file content
      */
     private Consumer<? super Path> appendFileContent(StringBuilder sb) {
diff --git a/src/test/java/org/onap/aai/validation/ruledriven/validator/TestDataDictionary.java b/src/test/java/org/onap/aai/validation/ruledriven/validator/TestDataDictionary.java
new file mode 100644 (file)
index 0000000..c3bad43
--- /dev/null
@@ -0,0 +1,62 @@
+/**
+ * ============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
+ *
+ * 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.aai.validation.ruledriven.validator;
+
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.List;
+import javax.inject.Inject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.aai.validation.exception.ValidationServiceException;
+import org.onap.aai.validation.result.ValidationResult;
+import org.onap.aai.validation.ruledriven.RuleDrivenValidator;
+import org.onap.aai.validation.test.util.TestUtil;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@TestPropertySource(locations = {"classpath:oxm-reader/schemaIngest.properties"})
+@ContextConfiguration(locations = {"classpath:data-dictionary/test-data-dictionary-beans.xml"})
+public class TestDataDictionary {
+
+    static {
+        System.setProperty("APP_HOME", ".");
+    }
+
+    @Inject
+    private RuleDrivenValidator validator;
+
+    @Test
+    public void testValidateWithRuleIndexing() throws ValidationServiceException, URISyntaxException, IOException {
+        List<ValidationResult> results =
+                validator.validate(TestUtil.getFileAsString("data-dictionary/test_events/test-create-event.json"));
+        assertThat(results, is(not(empty())));
+        assertThat(results.get(0).getViolations(), is(empty()));
+    }
+}
diff --git a/src/test/resources/data-dictionary/rule-data-dictionary.properties b/src/test/resources/data-dictionary/rule-data-dictionary.properties
new file mode 100644 (file)
index 0000000..35403e4
--- /dev/null
@@ -0,0 +1,24 @@
+# ============LICENSE_START===================================================
+# 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
+#
+# 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=====================================================
+
+rule.datadictionary.hostport=localhost:8080
+rule.datadictionary.connect.timeout=1000
+rule.datadictionary.read.timeout=1000
+
+# basic authentication: base64 encoding of username:password
+rule.datadictionary.credentials=
+
+rule.datadictionary.uri=/commonModelElements/instance~nfValuesCatalog~1.0/validateInstance
diff --git a/src/test/resources/data-dictionary/rule-indexing.properties b/src/test/resources/data-dictionary/rule-indexing.properties
new file mode 100644 (file)
index 0000000..0139a8b
--- /dev/null
@@ -0,0 +1,20 @@
+# ============LICENSE_START===================================================
+# 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
+#
+# 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=====================================================
+
+rule.indexing.events=POA-EVENT
+rule.indexing.exclude.oxm.validation=POA-EVENT
+rule.indexing.key.attributes=$.poa-event.modelVersionId,$.poa-event.modelInvariantId
+rule.indexing.default.key=default-rules
diff --git a/src/test/resources/data-dictionary/rules/poa-event/default-rules.groovy b/src/test/resources/data-dictionary/rules/poa-event/default-rules.groovy
new file mode 100644 (file)
index 0000000..574c3a2
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * ============LICENSE_START===================================================
+ * 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
+ *
+ * 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=====================================================
+ */
+
+entity {
+    name 'POA-EVENT'
+    indexing { indices 'default-rules' }
+    validation {
+        useRule {
+            name       'Data-Dictionary validate VF type'
+            attributes 'vnfList[*].vfModuleList[*].networkList[*].type'
+        }
+    }
+}
+
+rule {
+    name        'Data-Dictionary validate VF type'
+    category    'INVALID_VALUE'
+    description 'Validate all VF type values against data-dictionary'
+    errorText   'VF type [{0}] failed data-dictionary validation: {1}'
+    severity    'ERROR'
+    attributes  'typeList'
+    validate    '''
+        List<String> details = new ArrayList<>()
+        typeList.each {
+            def result = org.onap.aai.validation.ruledriven.rule.builtin.DataDictionary.validate("instance", "vfModuleNetworkType", "type", "$it")
+            if (!result.isEmpty()) {
+                details.add("$it")
+                details.add("$result")
+            }
+        }
+        return new Tuple2(details.isEmpty(), details)
+        '''
+}
diff --git a/src/test/resources/data-dictionary/schemaIngest.properties b/src/test/resources/data-dictionary/schemaIngest.properties
new file mode 100644 (file)
index 0000000..8df810a
--- /dev/null
@@ -0,0 +1,48 @@
+# ============LICENSE_START===================================================
+# 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
+#
+# 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=====================================================
+
+# Properties for the org.onap.aai.setup.SchemaLocationsBean
+# Schema related attributes for the oxm and edges
+# Any additional schema related attributes should start with prefix schema
+schema.configuration.location=N/A
+schema.nodes.location=src/test/resources/oxm-reader/single/oxm
+schema.edges.location=src/test/resources/oxm-reader/single/dbedgerules
+
+# Schema Version Related Attributes
+
+# Lists all of the versions in the schema
+schema.version.list=v8,v9,v10
+# Specifies from which version should the depth parameter to default to zero
+schema.version.depth.start=v9
+# Specifies from which version should the related link be displayed in response payload
+schema.version.related.link.start=v10
+# Specifies from which version should the client see only the uri excluding host info
+# Before this version server base will also be included
+schema.version.app.root.start=v10
+# Specifies from which version should the namespace be changed
+schema.version.namespace.change.start=v10
+# Specifies from which version should the client start seeing the edge label in payload
+schema.version.edge.label.start=v10
+# Specifies the version that the application should default to
+schema.version.api.default=v9
+
+schema.service.base.url=https://localhost:8452/aai/schema-service/v1/
+schema.service.nodes.endpoint=nodes?version=
+schema.service.edges.endpoint=edgerules?version=
+schema.service.versions.endpoint=versions
+
+schema.service.ssl.key-store=src/integration-test/resources/authentication/aai-client-cert.p12
+schema.service.ssl.trust-store=src/integration-test/resources/authentication/tomcat_keystore
diff --git a/src/test/resources/data-dictionary/test-data-dictionary-beans.xml b/src/test/resources/data-dictionary/test-data-dictionary-beans.xml
new file mode 100644 (file)
index 0000000..78a92f1
--- /dev/null
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:util="http://www.springframework.org/schema/util"
+       xsi:schemaLocation="
+               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
+               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
+
+       <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
+
+       <import resource="../oxm-reader/oxm-reader-beans.xml" />
+
+       <context:property-placeholder
+               location="
+        classpath:event-reader.properties,
+        classpath:aai-environment.properties,
+        classpath:data-dictionary/validation-service.properties,
+        classpath:data-dictionary/validation-service-auth.properties,
+        classpath:data-dictionary/schemaIngest.properties,
+        classpath:data-dictionary/rule-indexing.properties"
+               ignore-unresolvable="true" />
+
+       <util:properties id="dataDictionaryProperties" location="classpath:data-dictionary/rule-data-dictionary.properties" />
+
+       <!-- CONFIG BEANS -->
+       <bean id="eventReaderConfig" class="org.onap.aai.validation.config.EventReaderConfig" />
+
+       <!-- READER BEANS -->
+       <bean id="jsonReader" class="org.onap.aai.validation.reader.JsonReader" />
+
+       <bean id="eventReader" class="org.onap.aai.validation.reader.EventReader">
+               <constructor-arg ref="eventReaderConfig" />
+               <constructor-arg ref="jsonReader" />
+               <constructor-arg ref="oxmReader" />
+       </bean>
+
+       <bean id="rulesConfigurationPath" class="java.nio.file.Paths" factory-method="get">
+               <constructor-arg value="src/test/resources/data-dictionary/rules/" />
+               <constructor-arg>
+                       <array />
+               </constructor-arg>
+       </bean>
+
+       <bean id="ruleIndexingConfig" class="org.onap.aai.validation.config.RuleIndexingConfig">
+               <property name="indexedEvents" value="#{'${rule.indexing.events}'.split(',')}" />
+               <property name="excludedOxmValidationEvents" value="#{'${rule.indexing.exclude.oxm.validation}'.split(',')}" />
+               <property name="indexAttributes" value="#{'${rule.indexing.key.attributes}'.split(',')}" />
+               <property name="defaultIndexKey" value="${rule.indexing.default.key}" />
+       </bean>
+
+       <bean id="ruleDrivenValidator" class="org.onap.aai.validation.ruledriven.RuleDrivenValidator">
+               <constructor-arg ref="rulesConfigurationPath" />
+               <constructor-arg ref="oxmReader" />
+               <constructor-arg ref="eventReader" />
+               <constructor-arg ref="ruleIndexingConfig" />
+       </bean>
+
+       <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
+               <property name="staticMethod" value="org.onap.aai.validation.ruledriven.rule.builtin.DataDictionary.setProperties" />
+               <property name="arguments">
+                       <list>
+                               <ref bean="dataDictionaryProperties" />
+                       </list>
+               </property>
+       </bean>
+
+</beans>
diff --git a/src/test/resources/data-dictionary/test_events/test-create-event.json b/src/test/resources/data-dictionary/test_events/test-create-event.json
new file mode 100644 (file)
index 0000000..3693c99
--- /dev/null
@@ -0,0 +1,39 @@
+{
+       "cambria.partition": "AAI",
+       "event-header": {
+               "id": "test",
+               "timestamp": "20190213-16:27:37:353",
+               "domain": "test",
+               "event-type": "POA-EVENT",
+               "action": "CREATE",
+               "entity-type": "poa-entity",
+               "top-entity-type": "poa-entity",
+               "entity-link": ""
+       },
+       "entity": {
+               "poa-event": {
+                       "modelVersionId": "model-version-id",
+                       "modelInvariantId": "model-invariant-id"
+               },
+               "vnfList": [
+                       {
+                               "vfModuleList": [
+                                       {
+                                               "networkList": [
+                                                       {
+                                                               "type": "test1"
+                                                       }
+                                               ]
+                                       },
+                                       {
+                                               "networkList": [
+                                                       {
+                                                               "type": "test2"
+                                                       }
+                                               ]
+                                       }
+                               ]
+                       }
+               ]
+       }
+}
diff --git a/src/test/resources/data-dictionary/validation-service-auth.properties b/src/test/resources/data-dictionary/validation-service-auth.properties
new file mode 100644 (file)
index 0000000..e729536
--- /dev/null
@@ -0,0 +1,18 @@
+# ============LICENSE_START===================================================
+# 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
+#
+# 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=====================================================
+
+auth.policy.file=auth/auth_policy.json
+auth.authentication.disable=false
\ No newline at end of file
diff --git a/src/test/resources/data-dictionary/validation-service.properties b/src/test/resources/data-dictionary/validation-service.properties
new file mode 100644 (file)
index 0000000..105953a
--- /dev/null
@@ -0,0 +1,28 @@
+# ============LICENSE_START===================================================
+# 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
+#
+# 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=====================================================
+
+topic.publish.enable=true
+topic.publish.retries=3
+topic.consume.enable=true
+topic.consume.polling.interval.seconds=7
+
+event.domain=onap
+event.action.exclude=DELETE
+event.type.rule=poa-event
+event.type.model=NOT_APPLICABLE
+event.type.end=END-EVENT
+
+model.cache.expirySeconds=3