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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aai.simulator.controller;
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.utils.TestUtils.getFile;
27 import static org.onap.so.aai.simulator.utils.TestUtils.getHttpHeaders;
28 import static org.onap.so.aai.simulator.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.aai.simulator.models.Result;
36 import org.onap.so.aai.simulator.service.providers.ProjectCacheServiceProvider;
37 import org.onap.so.aai.simulator.utils.Constants;
38 import org.onap.so.aai.simulator.utils.TestUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
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;
54 * @author waqas.ikram@ericsson.com
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
61 public class ProjectControllerTest {
63 private static final String RELATIONSHIP_URL = "/relationship-list/relationship";
65 private static final String BUSINESS_PROJECT_JSON_FILE = "test-data/business-project.json";
67 private static final String PROJECT_RELATION_SHIP_JSON_FILE = "test-data/business-project-relation-ship.json";
69 private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE";
75 private TestRestTemplate restTemplate;
77 @Value("${spring.security.username}")
78 private String username;
81 private ProjectCacheServiceProvider cacheServiceProvider;
85 cacheServiceProvider.clearAll();
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);
94 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
96 final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
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());
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());
112 final String projectRelationshipUrl = url + RELATIONSHIP_URL;
114 final ResponseEntity<Void> putResponse = invokeHttpPut(projectRelationshipUrl, getRelationship());
116 assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
118 final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class);
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));
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);
136 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
138 final ResponseEntity<Result> actualResponse =
139 invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Result.class);
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));
149 private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
150 return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(username)), clazz);
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);
158 private HttpEntity<?> getHttpEntity(final Object obj) {
159 return new HttpEntity<>(obj, getHttpHeaders(username));
162 private String getProjectEndPointUrl() {
163 return TestUtils.getBaseUrl(port) + Constants.PROJECT_URL;
166 private String getRelationship() throws IOException, Exception {
167 return TestUtils.getJsonString(PROJECT_RELATION_SHIP_JSON_FILE);