e7b067db00f1aa1e0e75c03a55f5d288ca5166c3
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / ProjectControllerTest.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.aaisimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.so.aaisimulator.utils.TestUtils.getFile;
27 import static org.onap.so.aaisimulator.utils.TestUtils.getHttpHeaders;
28 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import org.junit.After;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.aai.domain.yang.Project;
35 import org.onap.so.aaisimulator.models.Result;
36 import org.onap.so.aaisimulator.service.providers.ProjectCacheServiceProvider;
37 import org.onap.so.aaisimulator.utils.Constants;
38 import org.onap.so.aaisimulator.utils.TestUtils;
39 import org.onap.so.simulator.model.UserCredentials;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
43 import org.springframework.boot.test.web.client.TestRestTemplate;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.test.context.ActiveProfiles;
51 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
52
53 /**
54  * @author waqas.ikram@ericsson.com
55  *
56  */
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @Configuration
61 public class ProjectControllerTest {
62
63     private static final String RELATIONSHIP_URL = "/relationship-list/relationship";
64
65     private static final String BUSINESS_PROJECT_JSON_FILE = "test-data/business-project.json";
66
67     private static final String PROJECT_RELATION_SHIP_JSON_FILE = "test-data/business-project-relation-ship.json";
68
69     private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE";
70
71     @LocalServerPort
72     private int port;
73
74     @Autowired
75     private TestRestTemplate restTemplate;
76
77     @Autowired
78     private UserCredentials userCredentials;
79
80     @Autowired
81     private ProjectCacheServiceProvider cacheServiceProvider;
82
83     @After
84     public void after() {
85         cacheServiceProvider.clearAll();
86     }
87
88     @Test
89     public void test_putProject_successfullyAddedToCache() throws Exception {
90         final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE;
91         final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath()));
92         final ResponseEntity<Void> actual = invokeHttpPut(url, body);
93
94         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
95
96         final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
97
98         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
99         assertTrue(actualResponse.hasBody());
100         final Project actualProject = actualResponse.getBody();
101         assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
102         assertNotNull(actualProject.getResourceVersion());
103
104     }
105
106     @Test
107     public void test_putProjectRelationShip_successfullyAddedToCache() throws Exception {
108         final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE;
109         final ResponseEntity<Void> actual = invokeHttpPut(url, getJsonString(BUSINESS_PROJECT_JSON_FILE));
110         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
111
112         final String projectRelationshipUrl = url + RELATIONSHIP_URL;
113
114         final ResponseEntity<Void> putResponse = invokeHttpPut(projectRelationshipUrl, getRelationship());
115
116         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
117
118         final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
119
120         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
121         assertTrue(actualResponse.hasBody());
122         final Project actualProject = actualResponse.getBody();
123         assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
124         assertNotNull(actualProject.getRelationshipList());
125         assertFalse(actualProject.getRelationshipList().getRelationship().isEmpty());
126         assertNotNull(actualProject.getRelationshipList().getRelationship().get(0));
127
128     }
129
130     @Test
131     public void test_getProjectCount_correctResult() throws Exception {
132         final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE;
133         final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath()));
134         final ResponseEntity<Void> actual = invokeHttpPut(url, body);
135
136         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
137
138         final ResponseEntity<Result> actualResponse =
139                 invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Result.class);
140
141         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
142         assertTrue(actualResponse.hasBody());
143         final Result result = actualResponse.getBody();
144         assertNotNull(result.getValues());
145         assertFalse(result.getValues().isEmpty());
146         assertEquals(1, result.getValues().get(0).get(Constants.PROJECT));
147     }
148
149     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
150         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(getUsername())), clazz);
151     }
152
153     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
154         final HttpEntity<?> httpEntity = getHttpEntity(obj);
155         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
156     }
157
158     private HttpEntity<?> getHttpEntity(final Object obj) {
159         return new HttpEntity<>(obj, getHttpHeaders(getUsername()));
160     }
161
162     private String getUsername() {
163         return userCredentials.getUsers().iterator().next().getUsername();
164     }
165
166     private String getProjectEndPointUrl() {
167         return TestUtils.getBaseUrl(port) + Constants.PROJECT_URL;
168     }
169
170     private String getRelationship() throws IOException, Exception {
171         return TestUtils.getJsonString(PROJECT_RELATION_SHIP_JSON_FILE);
172     }
173 }