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.aaisimulator.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.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
27 import org.junit.After;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.aai.domain.yang.Project;
31 import org.onap.so.aaisimulator.models.Results;
32 import org.onap.so.aaisimulator.service.providers.ProjectCacheServiceProvider;
33 import org.onap.so.aaisimulator.utils.Constants;
34 import org.onap.so.aaisimulator.utils.TestRestTemplateService;
35 import org.onap.so.aaisimulator.utils.TestUtils;
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.web.server.LocalServerPort;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 * @author waqas.ikram@ericsson.com
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ActiveProfiles("test")
52 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 public class ProjectControllerTest {
56 private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE";
62 private TestRestTemplateService testRestTemplateService;
65 private ProjectCacheServiceProvider cacheServiceProvider;
69 cacheServiceProvider.clearAll();
73 public void test_putProject_successfullyAddedToCache() throws Exception {
74 final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE);
75 final ResponseEntity<Void> actual =
76 testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class);
78 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
80 final ResponseEntity<Project> actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class);
82 assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
83 assertTrue(actualResponse.hasBody());
84 final Project actualProject = actualResponse.getBody();
85 assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
86 assertNotNull(actualProject.getResourceVersion());
91 public void test_putProjectRelationShip_successfullyAddedToCache() throws Exception {
92 final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE);
93 final ResponseEntity<Void> actual =
94 testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class);
95 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
97 final String projectRelationshipUrl = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE, RELATIONSHIP_URL);
99 final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(projectRelationshipUrl,
100 TestUtils.getBusinessProjectRelationship(), Void.class);
102 assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
104 final ResponseEntity<Project> actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class);
106 assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
107 assertTrue(actualResponse.hasBody());
108 final Project actualProject = actualResponse.getBody();
109 assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName());
110 assertNotNull(actualProject.getRelationshipList());
111 assertFalse(actualProject.getRelationshipList().getRelationship().isEmpty());
112 assertNotNull(actualProject.getRelationshipList().getRelationship().get(0));
117 public void test_getProjectCount_correctResult() throws Exception {
118 final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE);
119 final ResponseEntity<Void> actual =
120 testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class);
122 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
124 final ResponseEntity<Results> actualResponse =
125 testRestTemplateService.invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Results.class);
127 assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
128 assertTrue(actualResponse.hasBody());
129 final Results result = actualResponse.getBody();
130 assertNotNull(result.getValues());
131 assertFalse(result.getValues().isEmpty());
132 assertEquals(1, result.getValues().get(0).get(Constants.PROJECT));
136 private String getUrl(final String... urls) {
137 return TestUtils.getUrl(port, urls);