Merge "Fixing SonarQube violations"
authorToine Siebelink <toine.siebelink@est.tech>
Tue, 13 Dec 2022 11:08:25 +0000 (11:08 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 13 Dec 2022 11:08:25 +0000 (11:08 +0000)
1  2 
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java
cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy

@@@ -3,7 -3,6 +3,7 @@@
   *  Copyright (C) 2020-2022 Nordix Foundation
   *  Modifications Copyright (C) 2020-2022 Bell Canada.
   *  Modifications Copyright (C) 2021 Pantheon.tech
 + *  Modifications Copyright (C) 2022 TechMahindra Ltd.
   *  ================================================================================
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
@@@ -56,7 -55,6 +56,7 @@@ import org.onap.cps.spi.exceptions.Dupl
  import org.onap.cps.spi.exceptions.ModelValidationException;
  import org.onap.cps.spi.model.ModuleDefinition;
  import org.onap.cps.spi.model.ModuleReference;
 +import org.onap.cps.spi.model.SchemaSet;
  import org.onap.cps.spi.repository.DataspaceRepository;
  import org.onap.cps.spi.repository.ModuleReferenceRepository;
  import org.onap.cps.spi.repository.SchemaSetRepository;
@@@ -156,14 -154,6 +156,14 @@@ public class CpsModulePersistenceServic
          }
      }
  
 +    @Override
 +    public Collection<SchemaSet> getSchemaSetsByDataspaceName(final String dataspaceName) {
 +        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
 +        final List<SchemaSetEntity> schemaSetEntities = schemaSetRepository.getByDataspace(dataspaceEntity);
 +        return schemaSetEntities.stream()
 +                .map(CpsModulePersistenceServiceImpl::toSchemaSet).collect(Collectors.toList());
 +    }
 +
      @Override
      @Transactional
      // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
       */
      private String getNameForChecksum(
              final String checksum, final Collection<YangResourceEntity> yangResourceEntities) {
-         return
-                 yangResourceEntities.stream()
+         final Optional<String> optionalFileName = yangResourceEntities.stream()
                          .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
                          .findFirst()
-                         .map(YangResourceEntity::getFileName)
-                         .orElse(null);
+                         .map(YangResourceEntity::getFileName);
+         if (optionalFileName.isPresent()) {
+             return optionalFileName.get();
+         }
+         return null;
      }
  
      /**
                  yangResourceEntity.getRevision(),
                  yangResourceEntity.getContent());
      }
 +
 +    private static SchemaSet toSchemaSet(final SchemaSetEntity schemaSetEntity) {
 +        return SchemaSet.builder().name(schemaSetEntity.getName())
 +                .dataspaceName(schemaSetEntity.getDataspace().getName()).build();
 +    }
  }
@@@ -23,6 -23,7 +23,7 @@@ package org.onap.cps.spi.model
  
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.ImmutableSet;
+ import java.io.Serializable;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.List;
@@@ -33,7 -34,6 +34,7 @@@ import lombok.extern.slf4j.Slf4j
  import org.onap.cps.spi.exceptions.DataValidationException;
  import org.onap.cps.utils.YangUtils;
  import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 +import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
  import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
  import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
  import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
@@@ -49,7 -49,7 +50,7 @@@ public class DataNodeBuilder 
      private String xpath;
      private String moduleNamePrefix;
      private String parentNodeXpath = "";
-     private Map<String, Object> leaves = Collections.emptyMap();
+     private Map<String, Serializable> leaves = Collections.emptyMap();
      private Collection<DataNode> childDataNodes = Collections.emptySet();
  
      /**
       * @param leaves for the data node
       * @return DataNodeBuilder
       */
-     public DataNodeBuilder withLeaves(final Map<String, Object> leaves) {
+     public DataNodeBuilder withLeaves(final Map<String, Serializable> leaves) {
          this.leaves = leaves;
          return this;
      }
      private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
          final NormalizedNode normalizedNode) {
  
 -        if (normalizedNode instanceof DataContainerNode) {
 +        if (normalizedNode instanceof ChoiceNode) {
 +            addChoiceNode(currentDataNode, (ChoiceNode) normalizedNode);
 +        } else if (normalizedNode instanceof DataContainerNode) {
              addYangContainer(currentDataNode, (DataContainerNode) normalizedNode);
          } else if (normalizedNode instanceof MapNode) {
              addDataNodeForEachListElement(currentDataNode, (MapNode) normalizedNode);
          } else if (normalizedNode instanceof ValueNode) {
              final ValueNode<NormalizedNode> valuesNode = (ValueNode) normalizedNode;
              addYangLeaf(currentDataNode, valuesNode.getIdentifier().getNodeType().getLocalName(),
-                     valuesNode.body());
+                     (Serializable) valuesNode.body());
          } else if (normalizedNode instanceof LeafSetNode) {
              addYangLeafList(currentDataNode, (LeafSetNode<?>) normalizedNode);
          } else {
          }
      }
  
-     private static void addYangLeaf(final DataNode currentDataNode, final String leafName, final Object leafValue) {
-         final Map<String, Object> leaves = new ImmutableMap.Builder<String, Object>()
+     private static void addYangLeaf(final DataNode currentDataNode, final String leafName,
+                                     final Serializable leafValue) {
+         final Map<String, Serializable> leaves = new ImmutableMap.Builder<String, Serializable>()
              .putAll(currentDataNode.getLeaves())
              .put(leafName, leafValue)
              .build();
                  .stream()
                  .map(normalizedNode -> (normalizedNode).body())
                  .collect(Collectors.toUnmodifiableList());
-         addYangLeaf(currentDataNode, leafListName, leafListValues);
+         addYangLeaf(currentDataNode, leafListName, (Serializable) leafListValues);
      }
  
      private static void addDataNodeForEachListElement(final DataNode currentDataNode, final MapNode mapNode) {
          return newChildDataNode;
      }
  
 +    private static void addChoiceNode(final DataNode currentDataNode, final ChoiceNode choiceNode) {
 +
 +        final Collection<DataContainerChild> normalizedChildNodes = choiceNode.body();
 +        for (final NormalizedNode normalizedNode : normalizedChildNodes) {
 +            addDataNodeFromNormalizedNode(currentDataNode, normalizedNode);
 +        }
 +    }
 +
 +
  }
@@@ -32,7 -32,7 +32,7 @@@ import spock.lang.Specificatio
  
  class DataNodeBuilderSpec extends Specification {
  
-     Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
+     Map<String, Map<String, Serializable>> expectedLeavesByXpathMap = [
              '/test-tree'                                            : [],
              '/test-tree/branch[@name=\'Left\']'                     : [name: 'Left'],
              '/test-tree/branch[@name=\'Left\']/nest'                : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
              assert result.leaves['source-tp'] == '1-2-1'
      }
  
 +    def 'Converting NormalizedNode (tree) to a DataNode (tree) -- with ChoiceNode.'() {
 +        given: 'a schema context for expected model'
 +            def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('yang-with-choice-node.yang')
 +            def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
 +        and: 'the json data fragment parsed into normalized node object'
 +            def jsonData = TestUtils.getResourceFileContent('data-with-choice-node.json')
 +            def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
 +        when: 'the normalized node is converted to a data node'
 +            def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
 +            def mappedResult = TestUtils.getFlattenMapByXpath(result)
 +        then: 'the resulting data node contains only one xpath with 3 leaves'
 +            mappedResult.keySet().containsAll([
 +                "/container-with-choice-leaves"
 +            ])
 +            assert result.leaves['leaf-1'] == "test"
 +            assert result.leaves['choice-case1-leaf-a'] == "test"
 +            assert result.leaves['choice-case1-leaf-b'] == "test"
 +    }
 +
      def 'Converting NormalizedNode into DataNode collection: #scenario.'() {
          given: 'a schema context for expected model'
              def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')