Added new tests for SdcRestClientUtils 16/92816/3
authork.kedron <k.kedron@partner.samsung.com>
Tue, 6 Aug 2019 14:57:36 +0000 (16:57 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Wed, 14 Aug 2019 13:18:22 +0000 (13:18 +0000)
Added new tests.
Fixed sonar issue:
# added private constructor to the SdcRestClientUtils
# used String.format instead of stream.collect

Issue-ID: SDC-2327
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I904e4a7e31bab8f26dbdbb03a956bc4b1dc5922e

dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SdcRestClientUtils.java
dcaedt_catalog/asdc/src/test/java/org/onap/sdc/dcae/utils/utils/SdcRestClientUtilsTest.java [new file with mode: 0644]

index 88c89a7..13e6e9d 100644 (file)
@@ -3,6 +3,7 @@
  * SDC
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (c) 2019 Samsung
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,12 +35,14 @@ import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.List;
-import java.util.stream.Collectors;
 
 public class SdcRestClientUtils {
 
     private static final String SDC_CATALOG_PATH = "/sdc/v1/catalog/";
 
+    private SdcRestClientUtils() {
+    }
+
     // TODO consider moving params elsewhere (user/password/instanceId can be constant)
     public static EnumMap<SdcConsumerInfo, String> extractConsumerInfoFromUri(URI configUri) {
         EnumMap<SdcConsumerInfo, String> userInfoMap = new EnumMap<>(SdcConsumerInfo.class);
@@ -68,7 +71,7 @@ public class SdcRestClientUtils {
             filters.add("category="+category);
         if(!StringUtils.isEmpty(subcategory))
             filters.add("subCategory="+subcategory);
-        return "?"+filters.stream().collect(Collectors.joining("&"));
+        return "?"+ String.join("&", filters);
     }
 
     public static UserRemarks buildUserRemarksObject(String userRemarks) {
diff --git a/dcaedt_catalog/asdc/src/test/java/org/onap/sdc/dcae/utils/utils/SdcRestClientUtilsTest.java b/dcaedt_catalog/asdc/src/test/java/org/onap/sdc/dcae/utils/utils/SdcRestClientUtilsTest.java
new file mode 100644 (file)
index 0000000..516f68c
--- /dev/null
@@ -0,0 +1,167 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. 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.sdc.dcae.utils.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.EnumMap;
+
+import org.json.JSONObject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
+import org.onap.sdc.dcae.enums.ArtifactGroupType;
+import org.onap.sdc.dcae.enums.SdcConsumerInfo;
+import org.onap.sdc.dcae.utils.SdcRestClientUtils;
+import org.springframework.util.Base64Utils;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SdcRestClientUtilsTest {
+
+    private static final String USER_INFO = "USER_INFO";
+    private static final String INSTANCE_INFO = "321";
+    private static final String SCHEMA = "testSchema";
+    private static final String HOST = "testHost";
+    private static final int PORT = 123;
+    private static final String PATH = "/test/path";
+    private static final String RESOURCE_TYPE = "testResourceType";
+    private static final String CATEGORY = "testCategory";
+    private static final String SUB_CATEGORY = "testSubcategory";
+    private static final String DESCRIPTION = "testDescription";
+    private static final String NAME = "testName";
+    private static final String LABEL = "testLabel";
+    private static final String PAYLOAD_STRING = "testPayload";
+    private static final byte[] PAYLOAD = PAYLOAD_STRING.getBytes();
+    private static final String FRAGMENT_NOT_FOUND_EXCEPTION_MESSAGE =
+        "The URI must contain a fragment specification, to be used as SDC instance id";
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void testExtractConsumerInfoFormat()
+        throws URISyntaxException, NoSuchFieldException, IllegalAccessException {
+        // given
+        URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, INSTANCE_INFO);
+
+        // when
+        EnumMap<SdcConsumerInfo, String> userInfoMap =
+            SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
+
+        // then
+        assertEquals("Basic " + Base64Utils.encodeToString(USER_INFO.getBytes()),
+            userInfoMap.get(SdcConsumerInfo.AUTH));
+        assertEquals(INSTANCE_INFO, userInfoMap.get(SdcConsumerInfo.INSTANCE_ID));
+
+        assertEquals(String.format("%s://%s:%d%s", SCHEMA, HOST, PORT, PATH) + getSdcCatalogPath(),
+            userInfoMap.get(SdcConsumerInfo.CATALOG_URL));
+    }
+
+    @Test
+    public void testExtractConsumerWithoutInstanceInfo() throws URISyntaxException {
+        expectedException.expect(IllegalArgumentException.class);
+        expectedException.expectMessage(FRAGMENT_NOT_FOUND_EXCEPTION_MESSAGE);
+        // given
+        URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, null);
+        // when - then
+        SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
+    }
+
+    @Test
+    public void testBuildResourceFilterQuery() {
+        // when
+        String query =
+            SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, SUB_CATEGORY);
+        // then
+        assertEquals(String.format("?resourceType=%s&category=%s&subCategory=%s", RESOURCE_TYPE,
+            CATEGORY, SUB_CATEGORY), query);
+        // when
+        query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, null);
+        // then
+        assertEquals(String.format("?resourceType=%s&category=%s", RESOURCE_TYPE, CATEGORY), query);
+        // when
+        query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, null, null);
+        // then
+        assertEquals("?resourceType=" + RESOURCE_TYPE, query);
+    }
+
+    @Test
+    public void testArtifactGeneration() {
+        // when
+        Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
+            RESOURCE_TYPE, LABEL, PAYLOAD);
+
+        // then
+        assertEquals(DESCRIPTION, artifact.getDescription());
+        assertEquals(NAME, artifact.getArtifactName());
+        assertEquals(ArtifactGroupType.DEPLOYMENT.name(), artifact.getArtifactGroupType());
+        assertEquals(RESOURCE_TYPE, artifact.getArtifactType());
+        assertEquals(LABEL, artifact.getArtifactLabel());
+        assertEquals(Base64Utils.encodeToString(PAYLOAD), artifact.getPayloadData());
+
+        // when
+        artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
+
+        // then
+        assertEquals(NAME, artifact.getArtifactName());
+        assertEquals(PATH, artifact.getArtifactURL());
+        assertEquals(PAYLOAD_STRING, artifact.getPayloadData());
+    }
+
+    @Test
+    public void testConvertArtifactToString() throws JsonProcessingException {
+        // when
+        Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
+            RESOURCE_TYPE, LABEL, PAYLOAD);
+        JSONObject resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
+
+        // then
+        assertEquals(DESCRIPTION, resultJson.get("description"));
+        assertEquals(NAME, resultJson.get("artifactName"));
+        assertEquals(ArtifactGroupType.DEPLOYMENT.name(), resultJson.get("artifactGroupType"));
+        assertEquals(RESOURCE_TYPE, resultJson.get("artifactType"));
+        assertEquals(LABEL, resultJson.get("artifactLabel"));
+        assertEquals(Base64Utils.encodeToString(PAYLOAD), resultJson.get("payloadData"));
+
+        // when
+        artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
+        resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
+
+        // then
+        assertEquals(NAME, resultJson.get("artifactName"));
+        assertEquals(PATH, resultJson.get("artifactURL"));
+        assertEquals(PAYLOAD_STRING, resultJson.get("payloadData"));
+    }
+
+    private String getSdcCatalogPath() throws NoSuchFieldException, IllegalAccessException {
+        Field field = SdcRestClientUtils.class.getDeclaredField("SDC_CATALOG_PATH");
+        field.setAccessible(true);
+        return field.get(null).toString();
+    }
+}