Adding aai line of business endpoints
[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.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;
45
46 /**
47  * @author waqas.ikram@ericsson.com
48  *
49  */
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ActiveProfiles("test")
52 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
53 @Configuration
54 public class ProjectControllerTest {
55
56     private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE";
57
58     @LocalServerPort
59     private int port;
60
61     @Autowired
62     private TestRestTemplateService testRestTemplateService;
63
64     @Autowired
65     private ProjectCacheServiceProvider cacheServiceProvider;
66
67     @After
68     public void after() {
69         cacheServiceProvider.clearAll();
70     }
71
72     @Test
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);
77
78         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
79
80         final ResponseEntity<Project> actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class);
81
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());
87
88     }
89
90     @Test
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());
96
97         final String projectRelationshipUrl = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE, RELATIONSHIP_URL);
98
99         final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(projectRelationshipUrl,
100                 TestUtils.getBusinessProjectRelationship(), Void.class);
101
102         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
103
104         final ResponseEntity<Project> actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class);
105
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));
113
114     }
115
116     @Test
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);
121
122         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
123
124         final ResponseEntity<Results> actualResponse =
125                 testRestTemplateService.invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Results.class);
126
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));
133     }
134
135
136     private String getUrl(final String... urls) {
137         return TestUtils.getUrl(port, urls);
138     }
139
140 }