summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2019-12-09 08:30:33 +0200
committerIttay Stern <ittay.stern@att.com>2019-12-09 19:03:53 +0200
commit8878a06f6023c4fc2fc03656c9e2012bde80bb70 (patch)
treed7c19c454c187f239ed052741e6649bf759c4968
parentbae7a355deae5b9b16ac0343d4ec13018963e900 (diff)
Extract getExistingCounterMap from AAITreeConverter to ModelUtil
Issue-ID: VID-724 Change-Id: I16305bc2f7c0f208d03da8e0255fad0ea16fb28d Signed-off-by: Ittay Stern <ittay.stern@att.com>
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java21
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/model/ModelUtil.java53
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java35
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/model/ModelUtilTest.java106
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java3
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java3
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java3
7 files changed, 127 insertions, 97 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java
index 48736bc01..111a260e1 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java
@@ -20,14 +20,12 @@
package org.onap.vid.aai.util;
-import static java.util.function.Function.identity;
-import static java.util.stream.Collectors.counting;
-import static java.util.stream.Collectors.groupingBy;
import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
import java.util.Map;
-import java.util.Objects;
+import javax.inject.Inject;
import org.apache.commons.lang3.StringUtils;
+import org.onap.vid.model.ModelUtil;
import org.onap.vid.model.aaiTree.AAITreeNode;
import org.onap.vid.model.aaiTree.CollectionResource;
import org.onap.vid.model.aaiTree.Network;
@@ -56,6 +54,13 @@ public class AAITreeConverter {
public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_ID = "service-instance.service-instance-id";
public static final String TENANT_TENANT_NAME = "tenant.tenant-name";
+ private final ModelUtil modelUtil;
+
+ @Inject
+ public AAITreeConverter(ModelUtil modelUtil) {
+ this.modelUtil = modelUtil;
+ }
+
public ServiceInstance convertTreeToUIModel(AAITreeNode rootNode, String globalCustomerId, String serviceType, String instantiationType, String instanceRole, String instanceType) {
ServiceInstance serviceInstance = new ServiceInstance();
serviceInstance.setInstanceId(rootNode.getId());
@@ -107,13 +112,7 @@ public class AAITreeConverter {
}
private <T extends Node> Map<String, Long> getExistingCounterMap(Map<String, T> nodeList) {
- return nodeList.entrySet().stream()
- .map(k -> {
- ModelInfo modelInfo = k.getValue().getModelInfo();
- return StringUtils.defaultIfEmpty(modelInfo.getModelCustomizationId(), modelInfo.getModelVersionId());
- })
- .filter(Objects::nonNull)
- .collect(groupingBy(identity(), counting()));
+ return modelUtil.getExistingCounterMap(nodeList, Node::getModelInfo);
}
private static ModelInfo createModelInfo(AAITreeNode aaiNode) {
diff --git a/vid-app-common/src/main/java/org/onap/vid/model/ModelUtil.java b/vid-app-common/src/main/java/org/onap/vid/model/ModelUtil.java
index 45e100fb1..6c56a464b 100644
--- a/vid-app-common/src/main/java/org/onap/vid/model/ModelUtil.java
+++ b/vid-app-common/src/main/java/org/onap/vid/model/ModelUtil.java
@@ -20,39 +20,26 @@
package org.onap.vid.model;
+import static java.util.function.Function.identity;
+import static java.util.stream.Collectors.counting;
+import static java.util.stream.Collectors.groupingBy;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.vid.mso.model.ModelInfo;
+import org.springframework.stereotype.Component;
+
+@Component
public class ModelUtil {
- /**
- * Gets the tags for the given element according to the configured namespace
- * @param namespaces the namespace list from the configuration
- * @param constantValue the constant portion of the tag name, i.e. resource.vf...
- * @return the tags
- */
- public static String[] getTags ( String[] namespaces, String constantValue ) {
- String[] tags;
- if ( namespaces == null || namespaces.length == 0 ) {
- return null;
- }
- int le = namespaces.length;
- tags = new String[le];
- for ( int i = 0; i < le; i++ ) {
- tags[i] = namespaces[i] + constantValue;
- }
- return (tags);
- }
- /**
- * Determine if a note template type matches a set of configurable tags
- * @param type the node template type
- * @param tags the model configurable namespaces
- * @return true if type starts with a tag in the array, false otherwise
- */
- public static boolean isType ( String type, String[] tags ) {
- if ( (tags != null) && (tags.length > 0) ) {
- for ( int i = 0; i < tags.length; i++ ) {
- if ( type.startsWith (tags[i]) ) {
- return (true);
- }
- }
- }
- return (false);
+ public <T> Map<String, Long> getExistingCounterMap(Map<String, T> nodeList, Function<T, ModelInfo> modelInfoExtractor) {
+ return nodeList.values().stream()
+ .map(it -> {
+ ModelInfo modelInfo = modelInfoExtractor.apply(it);
+ return StringUtils.defaultIfEmpty(modelInfo.getModelCustomizationId(), modelInfo.getModelVersionId());
+ })
+ .filter(Objects::nonNull)
+ .collect(groupingBy(identity(), counting()));
}
}
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java
index 560e6cb5f..7bfc8a68a 100644
--- a/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java
@@ -20,32 +20,41 @@
*/
package org.onap.vid.aai;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasKey;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
+import static org.testng.Assert.assertNull;
+
import com.google.common.collect.ImmutableList;
+import java.util.List;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
import org.onap.vid.aai.util.AAITreeConverter;
import org.onap.vid.model.Action;
-import org.onap.vid.model.aaiTree.*;
+import org.onap.vid.model.ModelUtil;
+import org.onap.vid.model.aaiTree.AAITreeNode;
+import org.onap.vid.model.aaiTree.CollectionResource;
+import org.onap.vid.model.aaiTree.Network;
+import org.onap.vid.model.aaiTree.NodeType;
import org.onap.vid.model.aaiTree.ServiceInstance;
+import org.onap.vid.model.aaiTree.VfModule;
+import org.onap.vid.model.aaiTree.Vnf;
import org.onap.vid.mso.model.CloudConfiguration;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.*;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.emptyOrNullString;
-import static org.hamcrest.Matchers.hasKey;
-import static org.hamcrest.Matchers.hasSize;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.not;
-import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
-import static org.testng.Assert.assertNull;
-
public class AAITreeConverterTest {
+ @Spy
+ private ModelUtil modelUtil;
+
@InjectMocks
private AAITreeConverter aaiTreeConverter;
diff --git a/vid-app-common/src/test/java/org/onap/vid/model/ModelUtilTest.java b/vid-app-common/src/test/java/org/onap/vid/model/ModelUtilTest.java
index 0dd6e32a9..922a59200 100644
--- a/vid-app-common/src/test/java/org/onap/vid/model/ModelUtilTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/model/ModelUtilTest.java
@@ -7,9 +7,9 @@
* 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.
@@ -20,49 +20,81 @@
package org.onap.vid.model;
-import org.junit.Assert;
-import org.junit.Test;
+import static java.util.Collections.emptyMap;
+import static java.util.function.Function.identity;
+import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.anEmptyMap;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.onap.vid.mso.model.ModelInfo;
+import org.testng.annotations.Test;
public class ModelUtilTest {
- private ModelUtil createTestSubject() {
- return new ModelUtil();
- }
+ private final ModelUtil testSubject = new ModelUtil();
+
+ private ModelInfo modelWithCustomizationId(String id) {
+ ModelInfo result = new ModelInfo();
+ result.setModelCustomizationId(id);
+ return result;
+ }
+
+ private ModelInfo modelWithModelVersionId(String id) {
+ ModelInfo result = new ModelInfo();
+ result.setModelVersionId(id);
+ return result;
+ }
-
- @Test
- public void testGetTags() throws Exception {
- String[] namespaces;
- String constantValue = "test";
- String[] result;
+ private ModelInfo modelWithNullValues() {
+ return new ModelInfo();
+ }
- // test 1
- namespaces = null;
- result = ModelUtil.getTags(namespaces, constantValue);
- Assert.assertNull(result);
+ @Test
+ public void getExistingCounterMap_trivialCase() {
+ Map<String, Long> existingCounterMap =
+ testSubject.getExistingCounterMap(
+ ImmutableMap.of(
+ "a", modelWithCustomizationId("model_1"),
+ "b", modelWithCustomizationId("model_1"),
+ "c", modelWithCustomizationId("model_2")
+ ),
+ identity()
+ );
- // test 2
- namespaces = new String[] { "" };
- result = ModelUtil.getTags(namespaces, constantValue);
- Assert.assertArrayEquals(new String[] { constantValue }, result);
- }
+ assertThat(existingCounterMap, jsonEquals(ImmutableMap.of(
+ "model_1", 2,
+ "model_2", 1
+ )));
+ }
-
- @Test
- public void testIsType() throws Exception {
- String type = "a";
- String[] tags;
- boolean result;
+ @Test
+ public void getExistingCounterMap_givenMixOfIdsAndNulls_resultContainsIdsAndOmitsNulls() {
+ Map<String, Long> existingCounterMap =
+ testSubject.getExistingCounterMap(
+ ImmutableMap.of(
+ "a", modelWithCustomizationId("model_1"),
+ "b", modelWithModelVersionId("model_1"),
+ "c", modelWithModelVersionId("model_2"),
+ "d", modelWithNullValues()
+ ),
+ identity()
+ );
- // test 1
- tags = null;
- result = ModelUtil.isType(type, tags);
- Assert.assertEquals(false, result);
+ assertThat(existingCounterMap, jsonEquals(ImmutableMap.of(
+ "model_1", 2,
+ "model_2", 1
+ )));
+ }
- // test 2
- tags = new String[] { "a" };
- result = ModelUtil.isType(type, tags);
- Assert.assertEquals(true, result);
- }
+ @Test
+ public void getExistingCounterMap_handleEmptyCollections() {
+ assertThat(testSubject.getExistingCounterMap(
+ emptyMap(),
+ any -> modelWithCustomizationId("foo")
+ ), is(anEmptyMap()));
+ }
}
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java
index 3a05a841b..a73a5a7bd 100644
--- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java
@@ -44,6 +44,7 @@ import org.onap.vid.aai.util.AAIRestInterface;
import org.onap.vid.aai.util.AAITreeConverter;
import org.onap.vid.aai.util.CacheProvider;
import org.onap.vid.aai.util.TestWithAaiClient;
+import org.onap.vid.model.ModelUtil;
import org.onap.vid.model.aaiTree.Network;
import org.onap.vid.model.aaiTree.VpnBinding;
import org.onap.vid.testUtils.TestUtils;
@@ -65,7 +66,7 @@ public class AAIServiceIntegrativeTest extends TestWithAaiClient {
AAIServiceTree aaiServiceTree = new AAIServiceTree(
aaiClient,
new AAITreeNodeBuilder(aaiClient, logging),
- new AAITreeConverter(),
+ new AAITreeConverter(new ModelUtil()),
null,
null,
executorService
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
index d6ee62ce4..0d2d51cee 100644
--- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
@@ -50,6 +50,7 @@ import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.parser.ServiceModelInflator;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.model.Action;
+import org.onap.vid.model.ModelUtil;
import org.onap.vid.model.ServiceModel;
import org.onap.vid.model.aaiTree.AAITreeNode;
import org.onap.vid.model.aaiTree.FailureAAITreeNode;
@@ -83,7 +84,7 @@ public class AAIServiceTreeIntegrativeTest {
private AAITreeNodeBuilder aaiTreeNodeBuilder;
- private AAITreeConverter aaiTreeConverter = new AAITreeConverter();
+ private AAITreeConverter aaiTreeConverter = new AAITreeConverter(new ModelUtil());
private ExecutorService executorService = Executors.newFixedThreadPool(10);
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java
index 7457e480e..be195c89b 100644
--- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java
@@ -51,6 +51,7 @@ import org.onap.vid.aai.AaiClient;
import org.onap.vid.aai.util.AAITreeConverter;
import org.onap.vid.asdc.parser.ServiceModelInflator;
import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
+import org.onap.vid.model.ModelUtil;
import org.onap.vid.model.aaiTree.AAITreeNode;
import org.onap.vid.model.aaiTree.NodeType;
import org.onap.vid.mso.model.CloudConfiguration;
@@ -201,7 +202,7 @@ public class AAIServiceTreeTest {
AAIServiceTree aaiServiceTree = new AAIServiceTree(
aaiClientMock,
new AAITreeNodeBuilder(aaiClientMock, new Logging()),
- new AAITreeConverter(),
+ new AAITreeConverter(new ModelUtil()),
null,
null,
executorService