2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Samsung. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.dcae.utils.utils;
23 import static org.junit.Assert.assertEquals;
25 import java.lang.reflect.Field;
27 import java.net.URISyntaxException;
28 import java.util.EnumMap;
30 import org.json.JSONObject;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
37 import org.onap.sdc.dcae.enums.ArtifactGroupType;
38 import org.onap.sdc.dcae.enums.SdcConsumerInfo;
39 import org.onap.sdc.dcae.utils.SdcRestClientUtils;
40 import org.springframework.util.Base64Utils;
42 import com.fasterxml.jackson.core.JsonProcessingException;
44 @RunWith(MockitoJUnitRunner.class)
45 public class SdcRestClientUtilsTest {
47 private static final String USER_INFO = "USER_INFO";
48 private static final String INSTANCE_INFO = "321";
49 private static final String SCHEMA = "testSchema";
50 private static final String HOST = "testHost";
51 private static final int PORT = 123;
52 private static final String PATH = "/test/path";
53 private static final String RESOURCE_TYPE = "testResourceType";
54 private static final String CATEGORY = "testCategory";
55 private static final String SUB_CATEGORY = "testSubcategory";
56 private static final String DESCRIPTION = "testDescription";
57 private static final String NAME = "testName";
58 private static final String LABEL = "testLabel";
59 private static final String PAYLOAD_STRING = "testPayload";
60 private static final byte[] PAYLOAD = PAYLOAD_STRING.getBytes();
61 private static final String FRAGMENT_NOT_FOUND_EXCEPTION_MESSAGE =
62 "The URI must contain a fragment specification, to be used as SDC instance id";
65 public ExpectedException expectedException = ExpectedException.none();
68 public void testExtractConsumerInfoFormat()
69 throws URISyntaxException, NoSuchFieldException, IllegalAccessException {
71 URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, INSTANCE_INFO);
74 EnumMap<SdcConsumerInfo, String> userInfoMap =
75 SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
78 assertEquals("Basic " + Base64Utils.encodeToString(USER_INFO.getBytes()),
79 userInfoMap.get(SdcConsumerInfo.AUTH));
80 assertEquals(INSTANCE_INFO, userInfoMap.get(SdcConsumerInfo.INSTANCE_ID));
82 assertEquals(String.format("%s://%s:%d%s", SCHEMA, HOST, PORT, PATH) + getSdcCatalogPath(),
83 userInfoMap.get(SdcConsumerInfo.CATALOG_URL));
87 public void testExtractConsumerWithoutInstanceInfo() throws URISyntaxException {
88 expectedException.expect(IllegalArgumentException.class);
89 expectedException.expectMessage(FRAGMENT_NOT_FOUND_EXCEPTION_MESSAGE);
91 URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, null);
93 SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
97 public void testBuildResourceFilterQuery() {
100 SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, SUB_CATEGORY);
102 assertEquals(String.format("?resourceType=%s&category=%s&subCategory=%s", RESOURCE_TYPE,
103 CATEGORY, SUB_CATEGORY), query);
105 query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, null);
107 assertEquals(String.format("?resourceType=%s&category=%s", RESOURCE_TYPE, CATEGORY), query);
109 query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, null, null);
111 assertEquals("?resourceType=" + RESOURCE_TYPE, query);
115 public void testArtifactGeneration() {
117 Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
118 RESOURCE_TYPE, LABEL, PAYLOAD);
121 assertEquals(DESCRIPTION, artifact.getDescription());
122 assertEquals(NAME, artifact.getArtifactName());
123 assertEquals(ArtifactGroupType.DEPLOYMENT.name(), artifact.getArtifactGroupType());
124 assertEquals(RESOURCE_TYPE, artifact.getArtifactType());
125 assertEquals(LABEL, artifact.getArtifactLabel());
126 assertEquals(Base64Utils.encodeToString(PAYLOAD), artifact.getPayloadData());
129 artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
132 assertEquals(NAME, artifact.getArtifactName());
133 assertEquals(PATH, artifact.getArtifactURL());
134 assertEquals(PAYLOAD_STRING, artifact.getPayloadData());
138 public void testConvertArtifactToString() throws JsonProcessingException {
140 Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
141 RESOURCE_TYPE, LABEL, PAYLOAD);
142 JSONObject resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
145 assertEquals(DESCRIPTION, resultJson.get("description"));
146 assertEquals(NAME, resultJson.get("artifactName"));
147 assertEquals(ArtifactGroupType.DEPLOYMENT.name(), resultJson.get("artifactGroupType"));
148 assertEquals(RESOURCE_TYPE, resultJson.get("artifactType"));
149 assertEquals(LABEL, resultJson.get("artifactLabel"));
150 assertEquals(Base64Utils.encodeToString(PAYLOAD), resultJson.get("payloadData"));
153 artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
154 resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
157 assertEquals(NAME, resultJson.get("artifactName"));
158 assertEquals(PATH, resultJson.get("artifactURL"));
159 assertEquals(PAYLOAD_STRING, resultJson.get("payloadData"));
162 private String getSdcCatalogPath() throws NoSuchFieldException, IllegalAccessException {
163 Field field = SdcRestClientUtils.class.getDeclaredField("SDC_CATALOG_PATH");
164 field.setAccessible(true);
165 return field.get(null).toString();