Adding project endpoint and https support
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aai / simulator / 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.aai.simulator.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.aai.simulator.controller.TestUtils.getFile;
27 import static org.onap.so.aai.simulator.controller.TestUtils.getHttpHeaders;
28 import static org.onap.so.aai.simulator.controller.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.aai.simulator.service.providers.ProjectCacheServiceProvider;
36 import org.onap.so.aai.simulator.utils.Constants;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
41 import org.springframework.boot.test.web.client.TestRestTemplate;
42 import org.springframework.boot.web.server.LocalServerPort;
43 import org.springframework.context.annotation.Configuration;
44 import org.springframework.http.HttpEntity;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50
51 /**
52  * @author waqas.ikram@ericsson.com
53  *
54  */
55 @RunWith(SpringJUnit4ClassRunner.class)
56 @ActiveProfiles("test")
57 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 @Configuration
59 public class ProjectControllerTest {
60
61     private static final String RELATIONSHIP_URL = "/relationship-list/relationship";
62
63     private static final String BUSINESS_PROJECT_JSON_FILE = "test-data/business-project.json";
64
65     private static final String PROJECT_RELATION_SHIP_JSON_FILE = "test-data/business-project-relation-ship.json";
66
67     private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE";
68
69     @LocalServerPort
70     private int port;
71
72     @Autowired
73     private TestRestTemplate restTemplate;
74
75     @Value("${spring.security.username}")
76     private String username;
77
78     @Autowired
79     private ProjectCacheServiceProvider cacheServiceProvider;
80
81     @After
82     public void after() {
83         cacheServiceProvider.clearAll();
84     }
85
86     @Test
87     public void test_putProject_successfullyAddedToCache() throws Exception {
88         final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE;
89         final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath()));
90         final ResponseEntity<Void> actual = invokeHttpPut(url, body);
91
92         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
93
94         final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
95
96         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
97         assertTrue(actualResponse.hasBody());
98         final Project actualProject = actualResponse.getBody();
99         assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
100         assertNotNull(actualProject.getResourceVersion());
101
102     }
103
104     @Test
105     public void test_putProjectRelationShip_successfullyAddedToCache() throws Exception {
106         final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE;
107         final ResponseEntity<Void> actual = invokeHttpPut(url, getJsonString(BUSINESS_PROJECT_JSON_FILE));
108         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
109
110         final String projectRelationshipUrl = url + RELATIONSHIP_URL;
111
112         final ResponseEntity<Void> putResponse = invokeHttpPut(projectRelationshipUrl, getRelationship());
113
114         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
115
116         final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
117
118         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
119         assertTrue(actualResponse.hasBody());
120         final Project actualProject = actualResponse.getBody();
121         assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
122         assertNotNull(actualProject.getRelationshipList());
123         assertFalse(actualProject.getRelationshipList().getRelationship().isEmpty());
124         assertNotNull(actualProject.getRelationshipList().getRelationship().get(0));
125
126     }
127
128     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
129         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(username)), clazz);
130     }
131
132     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
133         final HttpEntity<?> httpEntity = getHttpEntity(obj);
134         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
135     }
136
137     private HttpEntity<?> getHttpEntity(final Object obj) {
138         return new HttpEntity<>(obj, getHttpHeaders(username));
139     }
140
141     private String getProjectEndPointUrl() {
142         return TestUtils.getBaseUrl(port) + Constants.PROJECT_URL;
143     }
144
145     private String getRelationship() throws IOException, Exception {
146         return TestUtils.getJsonString(PROJECT_RELATION_SHIP_JSON_FILE);
147     }
148 }