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.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;
48 * @author waqas.ikram@ericsson.com
51 @RunWith(SpringJUnit4ClassRunner.class)
52 @ActiveProfiles("test")
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
55 public class OwningEntityControllerTest {
57 private static final String OWN_ENTITY_ID_VALUE = "oe_1";
58 private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
64 private TestRestTemplateService testRestTemplateService;
67 private OwnEntityCacheServiceProvider cacheServiceProvider;
71 cacheServiceProvider.clearAll();
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);
80 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
82 final ResponseEntity<OwningEntity> actualResponse =
83 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
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());
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);
100 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
102 final ResponseEntity<Results> actualResponse = testRestTemplateService
103 .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
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));
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());
120 final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL;
122 final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl,
123 TestUtils.getOwningEntityRelationship(), Void.class);
125 assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
127 final ResponseEntity<OwningEntity> actualResponse =
128 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
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));
141 private String getUrl(final String... urls) {
142 return TestUtils.getUrl(port, urls);