Add initial configurations + basic test for ETSI NFVO NS LCM CSIT Tests
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / test / java / org / onap / so / sdcsimulator / controller / CatalogControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdcsimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import java.util.Base64;
26 import java.util.Set;
27 import java.util.UUID;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.so.sdcsimulator.models.ResourceAssetInfo;
31 import org.onap.so.sdcsimulator.models.ResourceMetadata;
32 import org.onap.so.sdcsimulator.models.ServiceAssetInfo;
33 import org.onap.so.sdcsimulator.models.ServiceMetadata;
34 import org.onap.so.sdcsimulator.utils.Constants;
35 import org.onap.so.simulator.model.UserCredentials;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
39 import org.springframework.boot.test.web.client.TestRestTemplate;
40 import org.springframework.boot.web.server.LocalServerPort;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.core.ParameterizedTypeReference;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.test.context.ActiveProfiles;
50 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
51
52 /**
53  * @author Waqas Ikram (waqas.ikram@est.tech)
54  *
55  */
56 @RunWith(SpringJUnit4ClassRunner.class)
57 @ActiveProfiles("test")
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 @Configuration
60 public class CatalogControllerTest {
61
62     private static final String SUB_CATEGORY = "Network Service";
63
64     private static final String DISTRIBUTION_STATUS = "DISTRIBUTED";
65
66     private static final String SERVICE_ID = "9bb8c882-44a1-4b67-a12c-5a998e18d6ba";
67
68     private static final String RESOURCE_ID = "73522444-e8e9-49c1-be29-d355800aa349";
69
70     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
71
72     @LocalServerPort
73     private int port;
74
75     @Autowired
76     private TestRestTemplate restTemplate;
77
78     @Autowired
79     private UserCredentials userCredentials;
80
81     @Test
82     public void test_getResourceCsar_validCsarId_matchContent() {
83
84         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/toscaModel";
85
86         final ResponseEntity<byte[]> response =
87                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
88
89         assertEquals(HttpStatus.OK, response.getStatusCode());
90         assertTrue(response.hasBody());
91         assertEquals(117247, response.getBody().length);
92
93     }
94
95     @Test
96     public void test_getServiceCsar_validCsarId_matchContent() {
97
98         final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/toscaModel";
99
100         final ResponseEntity<byte[]> response =
101                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
102
103         assertEquals(HttpStatus.OK, response.getStatusCode());
104         assertTrue(response.hasBody());
105         assertEquals(147255, response.getBody().length);
106
107     }
108
109     @Test
110     public void test_getResources_validResourcesFromClassPath() {
111
112         final ResponseEntity<Set<ResourceAssetInfo>> response =
113                 restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
114                         new ParameterizedTypeReference<Set<ResourceAssetInfo>>() {});
115
116         assertEquals(HttpStatus.OK, response.getStatusCode());
117         assertTrue(response.hasBody());
118         assertEquals(1, response.getBody().size());
119         assertEquals(SUB_CATEGORY, response.getBody().iterator().next().getSubCategory());
120
121     }
122
123     @Test
124     public void test_getServices_validServicesFromClassPath() {
125
126         final ResponseEntity<Set<ServiceAssetInfo>> response =
127                 restTemplate.exchange(getBaseUrl() + "/services", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
128                         new ParameterizedTypeReference<Set<ServiceAssetInfo>>() {});
129
130         assertEquals(HttpStatus.OK, response.getStatusCode());
131         assertTrue(response.hasBody());
132         assertEquals(1, response.getBody().size());
133         assertEquals(DISTRIBUTION_STATUS, response.getBody().iterator().next().getDistributionStatus());
134
135     }
136
137     @Test
138     public void test_getResourceCsar_invalidCsar_internalServerError() {
139         final String url = getBaseUrl() + "/resources/" + UUID.randomUUID().toString() + "/toscaModel";
140
141         final ResponseEntity<byte[]> response =
142                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
143
144
145         assertFalse(response.hasBody());
146         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
147     }
148
149     @Test
150     public void test_getResourceMetadata_validMetadataFileInClasspath_matchContent() {
151
152         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/metadata";
153
154         final ResponseEntity<ResourceMetadata> response =
155                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ResourceMetadata.class);
156
157
158         assertEquals(HttpStatus.OK, response.getStatusCode());
159         assertTrue(response.hasBody());
160         final ResourceMetadata actual = response.getBody();
161         assertEquals(8, actual.getResources().size());
162         assertEquals(3, actual.getArtifacts().size());
163         assertEquals(SUB_CATEGORY, actual.getSubCategory());
164
165     }
166
167     @Test
168     public void test_getServiceMetadata_validMetadataFileInClasspath_matchContent() {
169
170         final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/metadata";
171
172         final ResponseEntity<ServiceMetadata> response =
173                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceMetadata.class);
174
175
176         assertEquals(HttpStatus.OK, response.getStatusCode());
177         assertTrue(response.hasBody());
178         final ServiceMetadata actual = response.getBody();
179         assertEquals(1, actual.getResources().size());
180         assertEquals(1, actual.getArtifacts().size());
181         assertEquals(DISTRIBUTION_STATUS, actual.getDistributionStatus());
182
183     }
184
185
186     private String getBaseUrl() {
187         return "http://localhost:" + port + Constants.CATALOG_URL;
188     }
189
190     private HttpHeaders getHttpHeaders() {
191         final HttpHeaders requestHeaders = new HttpHeaders();
192         requestHeaders.add("Authorization", getBasicAuth(userCredentials.getUsers().get(0).getUsername()));
193         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
194         return requestHeaders;
195     }
196
197     private String getBasicAuth(final String username) {
198         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
199     }
200
201 }