Refactor rule manager key generation 68/78468/1
authormark.j.leonard <mark.j.leonard@gmail.com>
Thu, 14 Feb 2019 10:47:49 +0000 (10:47 +0000)
committermark.j.leonard <mark.j.leonard@gmail.com>
Thu, 14 Feb 2019 10:47:49 +0000 (10:47 +0000)
Simplify the generateKey() method by using a Java List instead of an
Array. This makes the code easier to read.

Change-Id: Ida772d33af5a244bafa19b6fff465fd590ac6d1a
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/main/java/org/onap/aai/validation/ruledriven/RuleManager.java
src/main/java/org/onap/aai/validation/ruledriven/configuration/RulesConfigurationLoader.groovy

index 57ace7d..4b22d77 100644 (file)
@@ -9,7 +9,7 @@
  * 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,
@@ -230,7 +230,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}));
+                return ruleManager
+                        .getRulesForEntity(RuleManager.generateKey(Collections.singletonList(defaultIndexKey)));
             } else {
                 applicationLogger.debug("Default index value not configured, unable to get rules");
                 applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, eventType);
@@ -252,7 +253,7 @@ public class RuleDrivenValidator implements Validator {
             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));
+            return RuleManager.generateKey(valuesMap.values());
         } catch (ValidationServiceException e) {
             applicationLogger.debug("Failed to retrieve index key attributes from event: " + e.getMessage());
             applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, e, eventType);
index 380c99a..6c67341 100644 (file)
@@ -1,32 +1,33 @@
-/*
- * ============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.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
+import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.stream.Collectors;
 import org.onap.aai.validation.ruledriven.configuration.EntitySection;
 import org.onap.aai.validation.ruledriven.configuration.GroovyConfigurationException;
 import org.onap.aai.validation.ruledriven.configuration.RuleSection;
@@ -47,7 +48,7 @@ public class RuleManager {
      * Create the rules for each type of entity based on the supplied configuration
      *
      * @param entities
-     *        configuration (all entities)
+     *            configuration (all entities)
      * @throws InstantiationException
      * @throws IllegalAccessException
      * @throws GroovyConfigurationException
@@ -77,16 +78,7 @@ public class RuleManager {
         return Optional.ofNullable(rulesMap.get(entityType));
     }
 
-    public static String generateKey(String[] indices) {
-        SortedSet<String> sortedIndices = new TreeSet<>();
-        Collections.addAll(sortedIndices, indices);
-        StringBuilder sb = new StringBuilder();
-        Iterator<String> iterator = sortedIndices.iterator();
-        while (iterator.hasNext()) {
-            sb.append("[");
-            sb.append(iterator.next());
-            sb.append("]");
-        }
-        return sb.toString();
+    public static String generateKey(Collection<Object> collection) {
+        return collection.stream().sorted().map(String::valueOf).collect(Collectors.joining("][", "[", "]"));
     }
 }
index 5fabde9..6d8aa6e 100644 (file)
@@ -185,7 +185,7 @@ class IndexingDelegate {
 
     void indices(String... indices) {
         this.configuration.setIndices indices
-        def index = RuleManager.generateKey(indices)
+        def index = RuleManager.generateKey(indices.toList())
         this.configuration.type = index
         this.configuration.getRules().each { rule ->
             rule.setObject configuration.type