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 / OwningEntityControllerTest.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.OwningEntity;
31 import org.onap.so.aaisimulator.models.Format;
32 import org.onap.so.aaisimulator.models.Results;
33 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
34 import org.onap.so.aaisimulator.utils.Constants;
35 import org.onap.so.aaisimulator.utils.TestRestTemplateService;
36 import org.onap.so.aaisimulator.utils.TestUtils;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
40 import org.springframework.boot.web.server.LocalServerPort;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.test.context.ActiveProfiles;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46
47 /**
48  * @author waqas.ikram@ericsson.com
49  *
50  */
51 @RunWith(SpringJUnit4ClassRunner.class)
52 @ActiveProfiles("test")
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @Configuration
55 public class OwningEntityControllerTest {
56
57     private static final String OWN_ENTITY_ID_VALUE = "oe_1";
58     private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
59
60     @LocalServerPort
61     private int port;
62
63     @Autowired
64     private TestRestTemplateService testRestTemplateService;
65
66     @Autowired
67     private OwnEntityCacheServiceProvider cacheServiceProvider;
68
69     @After
70     public void after() {
71         cacheServiceProvider.clearAll();
72     }
73
74     @Test
75     public void test_putOwningEntity_successfullyAddedToCache() throws Exception {
76         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
77         final ResponseEntity<Void> actual =
78                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
79
80         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
81
82         final ResponseEntity<OwningEntity> actualResponse =
83                 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
84
85         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
86         assertTrue(actualResponse.hasBody());
87         final OwningEntity actualOwningEntity = actualResponse.getBody();
88         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
89         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
90         assertNotNull(actualOwningEntity.getResourceVersion());
91
92     }
93
94     @Test
95     public void test_getOwningEntityCount_correctResult() throws Exception {
96         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
97         final ResponseEntity<Void> actual =
98                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
99
100         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
101
102         final ResponseEntity<Results> actualResponse = testRestTemplateService
103                 .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
104
105         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
106         assertTrue(actualResponse.hasBody());
107         final Results result = actualResponse.getBody();
108         assertNotNull(result.getValues());
109         assertFalse(result.getValues().isEmpty());
110         assertEquals(1, result.getValues().get(0).get(Constants.OWNING_ENTITY));
111     }
112
113     @Test
114     public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception {
115         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
116         final ResponseEntity<Void> actual =
117                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
118         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
119
120         final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL;
121
122         final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl,
123                 TestUtils.getOwningEntityRelationship(), Void.class);
124
125         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
126
127         final ResponseEntity<OwningEntity> actualResponse =
128                 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
129
130         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
131         assertTrue(actualResponse.hasBody());
132         final OwningEntity actualOwningEntity = actualResponse.getBody();
133         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
134         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
135         assertNotNull(actualOwningEntity.getRelationshipList());
136         assertFalse(actualOwningEntity.getRelationshipList().getRelationship().isEmpty());
137         assertNotNull(actualOwningEntity.getRelationshipList().getRelationship().get(0));
138
139     }
140
141     private String getUrl(final String... urls) {
142         return TestUtils.getUrl(port, urls);
143     }
144
145
146 }