AAIResourcesUriTemplates - Removed Sonar warnings 77/109877/2
authorChris André <chris.andre@yoppworks.com>
Mon, 6 Jul 2020 21:04:45 +0000 (17:04 -0400)
committerChris André <chris.andre@yoppworks.com>
Mon, 6 Jul 2020 22:44:47 +0000 (18:44 -0400)
- Added 'MissingTemplateException'
- Added test for presence of a value within an Optional

Issue-ID: AAI-2959
Signed-off-by: Chris Andre <chris.andre@yoppworks.com>
Change-Id: Ifb350815ecf7d4c46aeaac3e136d6a49bdffbafa

src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java [new file with mode: 0644]
src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java

diff --git a/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java b/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java
new file mode 100644 (file)
index 0000000..d47541b
--- /dev/null
@@ -0,0 +1,29 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2020 Bell Intellectual Property. 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.
+ * 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.cacher.exceptions;
+
+public class MissingTemplateException extends RuntimeException {
+
+    public static final String DEFAULT_EXCEPTION_MESSAGE = "Failed in uriToTemplates for truncatedUri '%s'";
+
+    public MissingTemplateException(String uri) {
+        super(String.format(DEFAULT_EXCEPTION_MESSAGE, uri));
+    }
+}
index 887e4d3..819a7a6 100644 (file)
@@ -24,6 +24,7 @@ import com.att.eelf.configuration.EELFManager;
 import com.google.gson.JsonObject;
 import org.apache.commons.lang3.StringUtils;
 import org.onap.aai.annotations.Metadata;
+import org.onap.aai.cacher.exceptions.MissingTemplateException;
 import org.onap.aai.cacher.util.AAIConstants;
 import org.reflections.Reflections;
 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@@ -44,7 +45,7 @@ import java.util.*;
 @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
 public class AAIResourcesUriTemplates {
 
-    private final static EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIResourcesUriTemplates.class);
+    private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIResourcesUriTemplates.class);
 
     private final Map<String, String> typeToUriTemplate;
 
@@ -63,13 +64,13 @@ public class AAIResourcesUriTemplates {
 
         Reflections reflections = new Reflections("org.onap.aai.domain.yang");
         reflections.getTypesAnnotatedWith(Metadata.class)
-                .stream()
-                .filter(aClass -> "org.onap.aai.domain.yang".equals(aClass.getPackage().getName()))
-                .filter(aClass -> !aClass.getAnnotation(Metadata.class).uriTemplate().isEmpty())
-                .forEach(aClass -> typeToUriTemplate.put(
-                        aClass.getAnnotation(XmlRootElement.class).name(),
-                        aClass.getAnnotation(Metadata.class).uriTemplate())
-                );
+            .stream()
+            .filter(aClass -> "org.onap.aai.domain.yang".equals(aClass.getPackage().getName()))
+            .filter(aClass -> !aClass.getAnnotation(Metadata.class).uriTemplate().isEmpty())
+            .forEach(aClass -> typeToUriTemplate.put(
+                aClass.getAnnotation(XmlRootElement.class).name(),
+                aClass.getAnnotation(Metadata.class).uriTemplate())
+            );
 
         LOGGER.info("AAI uri templates: " + typeToUriTemplate);
     }
@@ -80,7 +81,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * Get templated aai uri segment by type.
-     * 
+     *
      * @param type
      * @return
      */
@@ -90,6 +91,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * For the given template and uri get the variable key value pairs
+     *
      * @param uri
      * @param template
      * @return
@@ -106,7 +108,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * For a given uri get an ordered list of templates.
-     * 
+     *
      * @param uri
      * @return
      */
@@ -118,19 +120,20 @@ public class AAIResourcesUriTemplates {
 
         while (truncatedUri.contains("/")) {
             matchingStartingTemplate = this.getMatchingStartingTemplate(truncatedUri);
-            if ( !matchingStartingTemplate.isPresent()) {
+            if (!matchingStartingTemplate.isPresent()) {
                 LOGGER.error("failed in uriToTemplates for truncatedUri " + truncatedUri);
-                // exception expected for missing template
-            }
-            template = matchingStartingTemplate.get();
-            uriTemplateList.add(template);
-            int count = StringUtils.countMatches(template, "/");
-            if (count < StringUtils.countMatches(truncatedUri, "/")) {
-                truncatedUri = StringUtils.substring(
+                throw new MissingTemplateException(truncatedUri);
+            } else {
+                template = matchingStartingTemplate.get();
+                uriTemplateList.add(template);
+                int count = StringUtils.countMatches(template, "/");
+                if (count < StringUtils.countMatches(truncatedUri, "/")) {
+                    truncatedUri = StringUtils.substring(
                         truncatedUri,
                         StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1));
-            } else {
-                truncatedUri = "";
+                } else {
+                    truncatedUri = "";
+                }
             }
         }
 
@@ -139,7 +142,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * For a given uri get an ordered list of templates.
-     * 
+     *
      * @param uri
      * @return
      */
@@ -149,14 +152,17 @@ public class AAIResourcesUriTemplates {
         String truncatedUri = uri;
 
         while (truncatedUri.contains("/")) {
-            template = this.getMatchingStartingTemplate(truncatedUri).get();
-            int count = StringUtils.countMatches(template, "/");
-            int cutIndex = truncatedUri.length();
-            if (count != StringUtils.countMatches(truncatedUri, "/")) {
-                cutIndex = StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1);
+            Optional<String> templateOpt = this.getMatchingStartingTemplate(truncatedUri);
+            if (templateOpt.isPresent()) {
+                template = templateOpt.get();
+                int count = StringUtils.countMatches(template, "/");
+                int cutIndex = truncatedUri.length();
+                if (count != StringUtils.countMatches(truncatedUri, "/")) {
+                    cutIndex = StringUtils.ordinalIndexOf(truncatedUri, "/", count + 1);
+                }
+                uriList.add(StringUtils.substring(truncatedUri, 0, cutIndex));
+                truncatedUri = StringUtils.substring(truncatedUri, cutIndex);
             }
-            uriList.add(StringUtils.substring(truncatedUri, 0, cutIndex));
-            truncatedUri = StringUtils.substring(truncatedUri, cutIndex);
         }
 
         return uriList;
@@ -164,7 +170,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * returns the template matching the start of the uri.
-     * 
+     *
      * @param uri
      * @return @see java.util.Optional
      */
@@ -174,7 +180,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * Given aai type and json object generate the uri for it.
-     * 
+     *
      * @param type
      * @param jo
      * @return
@@ -190,7 +196,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * Get encoded values from json object for each key in keys
-     * 
+     *
      * @param keys
      * @param jo
      * @return
@@ -204,7 +210,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * extract uri keys from the templated uri
-     * 
+     *
      * @param template
      * @return
      */
@@ -216,7 +222,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * UTF-8 encoding of @param string
-     * 
+     *
      * @param string string to be encoded
      * @return
      */
@@ -230,7 +236,7 @@ public class AAIResourcesUriTemplates {
 
     /**
      * UTF-8 decoding of @param string
-     * 
+     *
      * @param string string to be encoded
      * @return
      */
@@ -260,7 +266,7 @@ public class AAIResourcesUriTemplates {
         for (int i = 0; i < uriSegments.size(); i++) {
             aus = new AAIUriSegment(uriSegments.get(i), uriSegmentTemplates.get(i));
             aus.setSegmentKeyValues(
-                    getUriTemplateMappings(aus.getSegment(), aus.getSegmentTemplate()));
+                getUriTemplateMappings(aus.getSegment(), aus.getSegmentTemplate()));
             uriSegmentList.add(aus);
         }
         return uriSegmentList;