From 27b2add68b647b1a744c949398bd963d8e35c84d Mon Sep 17 00:00:00 2001 From: Chris André Date: Mon, 6 Jul 2020 17:04:45 -0400 Subject: AAIResourcesUriTemplates - Removed Sonar warnings - Added 'MissingTemplateException' - Added test for presence of a value within an Optional Issue-ID: AAI-2959 Signed-off-by: Chris Andre Change-Id: Ifb350815ecf7d4c46aeaac3e136d6a49bdffbafa --- .../exceptions/MissingTemplateException.java | 29 +++++++++ .../strategy/aai/AAIResourcesUriTemplates.java | 76 ++++++++++++---------- 2 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.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 index 0000000..d47541b --- /dev/null +++ b/src/main/java/org/onap/aai/cacher/exceptions/MissingTemplateException.java @@ -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)); + } +} diff --git a/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java b/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java index 887e4d3..819a7a6 100644 --- a/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java +++ b/src/main/java/org/onap/aai/cacher/injestion/parser/strategy/aai/AAIResourcesUriTemplates.java @@ -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 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 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; -- cgit 1.2.3-korg