Added new tests for SdcRestClientUtils
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / test / java / org / onap / sdc / dcae / utils / utils / SdcRestClientUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.dcae.utils.utils;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.lang.reflect.Field;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.util.EnumMap;
29
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;
41
42 import com.fasterxml.jackson.core.JsonProcessingException;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class SdcRestClientUtilsTest {
46
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";
63
64     @Rule
65     public ExpectedException expectedException = ExpectedException.none();
66
67     @Test
68     public void testExtractConsumerInfoFormat()
69         throws URISyntaxException, NoSuchFieldException, IllegalAccessException {
70         // given
71         URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, INSTANCE_INFO);
72
73         // when
74         EnumMap<SdcConsumerInfo, String> userInfoMap =
75             SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
76
77         // then
78         assertEquals("Basic " + Base64Utils.encodeToString(USER_INFO.getBytes()),
79             userInfoMap.get(SdcConsumerInfo.AUTH));
80         assertEquals(INSTANCE_INFO, userInfoMap.get(SdcConsumerInfo.INSTANCE_ID));
81
82         assertEquals(String.format("%s://%s:%d%s", SCHEMA, HOST, PORT, PATH) + getSdcCatalogPath(),
83             userInfoMap.get(SdcConsumerInfo.CATALOG_URL));
84     }
85
86     @Test
87     public void testExtractConsumerWithoutInstanceInfo() throws URISyntaxException {
88         expectedException.expect(IllegalArgumentException.class);
89         expectedException.expectMessage(FRAGMENT_NOT_FOUND_EXCEPTION_MESSAGE);
90         // given
91         URI configUri = new URI(SCHEMA, USER_INFO, HOST, PORT, PATH, null, null);
92         // when - then
93         SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
94     }
95
96     @Test
97     public void testBuildResourceFilterQuery() {
98         // when
99         String query =
100             SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, SUB_CATEGORY);
101         // then
102         assertEquals(String.format("?resourceType=%s&category=%s&subCategory=%s", RESOURCE_TYPE,
103             CATEGORY, SUB_CATEGORY), query);
104         // when
105         query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, CATEGORY, null);
106         // then
107         assertEquals(String.format("?resourceType=%s&category=%s", RESOURCE_TYPE, CATEGORY), query);
108         // when
109         query = SdcRestClientUtils.buildResourceFilterQuery(RESOURCE_TYPE, null, null);
110         // then
111         assertEquals("?resourceType=" + RESOURCE_TYPE, query);
112     }
113
114     @Test
115     public void testArtifactGeneration() {
116         // when
117         Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
118             RESOURCE_TYPE, LABEL, PAYLOAD);
119
120         // then
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());
127
128         // when
129         artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
130
131         // then
132         assertEquals(NAME, artifact.getArtifactName());
133         assertEquals(PATH, artifact.getArtifactURL());
134         assertEquals(PAYLOAD_STRING, artifact.getPayloadData());
135     }
136
137     @Test
138     public void testConvertArtifactToString() throws JsonProcessingException {
139         // when
140         Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(DESCRIPTION, NAME,
141             RESOURCE_TYPE, LABEL, PAYLOAD);
142         JSONObject resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
143
144         // then
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"));
151
152         // when
153         artifact = SdcRestClientUtils.generateCatalogDcaeToscaArtifact(NAME, PATH, PAYLOAD);
154         resultJson = new JSONObject(SdcRestClientUtils.artifactToString(artifact));
155
156         // then
157         assertEquals(NAME, resultJson.get("artifactName"));
158         assertEquals(PATH, resultJson.get("artifactURL"));
159         assertEquals(PAYLOAD_STRING, resultJson.get("payloadData"));
160     }
161
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();
166     }
167 }