2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 package org.oransc.policyagent;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertFalse;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.oransc.policyagent.configuration.ApplicationConfig;
33 import org.oransc.policyagent.controllers.ImmutableServiceRegistrationInfo;
34 import org.oransc.policyagent.controllers.ImmutableServiceStatus;
35 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
36 import org.oransc.policyagent.controllers.ServiceStatus;
37 import org.oransc.policyagent.exceptions.ServiceException;
38 import org.oransc.policyagent.repository.ImmutablePolicy;
39 import org.oransc.policyagent.repository.ImmutablePolicyType;
40 import org.oransc.policyagent.repository.Policies;
41 import org.oransc.policyagent.repository.Policy;
42 import org.oransc.policyagent.repository.PolicyType;
43 import org.oransc.policyagent.repository.PolicyTypes;
44 import org.oransc.policyagent.repository.Rics;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.test.context.TestConfiguration;
49 import org.springframework.boot.web.server.LocalServerPort;
50 import org.springframework.context.annotation.Bean;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.test.context.junit4.SpringRunner;
54 import org.springframework.web.client.RestTemplate;
56 @RunWith(SpringRunner.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 public class ApplicationTest {
64 private Policies policies;
67 private PolicyTypes policyTypes;
69 private static Gson gson = new GsonBuilder() //
73 static class MockApplicationConfig extends ApplicationConfig {
75 public void initialize() {
76 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
77 loadConfigurationFromFile(url.getFile());
82 * overrides the BeanFactory
85 static class BeanFactory {
88 public ApplicationConfig getApplicationConfig() {
89 return new MockApplicationConfig();
97 private RestTemplate restTemplate = new RestTemplate();
100 public void getRics() throws Exception {
101 String url = baseUrl() + "/rics";
102 String rsp = this.restTemplate.getForObject(url, String.class);
103 assertThat(rsp).contains("kista_1");
107 public void getRic() throws Exception {
108 String url = baseUrl() + "/ric?managedElementId=kista_1";
109 String rsp = this.restTemplate.getForObject(url, String.class);
110 assertThat(rsp).isEqualTo("ric1");
113 // managedElmentId -> nodeName
116 public void putPolicy() throws Exception {
117 putService("service1");
119 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
121 addPolicyType("type1");
123 this.restTemplate.put(url, json);
125 Policy policy = policies.get("instance1");
127 assertThat(policy).isNotNull();
128 assertThat(policy.id()).isEqualTo("instance1");
129 assertThat(policy.ownerServiceName()).isEqualTo("service1");
132 private PolicyType addPolicyType(String name) {
133 PolicyType type = ImmutablePolicyType.builder() //
138 policyTypes.put(type);
142 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
143 Policy p = ImmutablePolicy.builder().id(id) //
145 .ownerServiceName(service) //
146 .ric(rics.getRic("ric1")) //
147 .type(addPolicyType(typeName)) //
153 private String baseUrl() {
154 return "http://localhost:" + port;
158 public void getPolicy() throws Exception {
159 String url = baseUrl() + "/policy?instance=id";
160 Policy policy = addPolicy("id", "typeName", "service1");
162 String rsp = this.restTemplate.getForObject(url, String.class);
163 assertThat(rsp).isEqualTo(policy.json());
166 policies.remove(policy);
167 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
168 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
173 public void getPolicies() throws Exception {
174 String url = baseUrl() + "/policies";
175 addPolicy("id1", "type1", "service1");
176 addPolicy("id2", "type2", "service2");
178 String rsp = this.restTemplate.getForObject(url, String.class);
179 System.out.println(rsp);
180 assertThat(rsp).contains("id1");
181 assertThat(rsp).contains("id2");
185 public void getPoliciesFilter() throws Exception {
186 addPolicy("id1", "type1", "service1");
187 addPolicy("id2", "type1", "service2");
188 addPolicy("id3", "type2", "service1");
190 String url = baseUrl() + "/policies?type=type1";
191 String rsp = this.restTemplate.getForObject(url, String.class);
192 System.out.println(rsp);
193 assertThat(rsp).contains("id1");
194 assertThat(rsp).contains("id2");
195 assertFalse(rsp.contains("id3"));
197 url = baseUrl() + "/policies?type=type1&service=service2";
198 rsp = this.restTemplate.getForObject(url, String.class);
199 System.out.println(rsp);
200 assertFalse(rsp.contains("id1"));
201 assertThat(rsp).contains("id2");
202 assertFalse(rsp.contains("id3"));
205 private void putService(String name) {
206 String url = baseUrl() + "/service";
208 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
209 .keepAliveInterval(1) //
212 String json = gson.toJson(service);
213 this.restTemplate.put(url, json);
217 public void putAndGetService() throws Exception {
220 String url = baseUrl() + "/service?name=name";
221 String rsp = this.restTemplate.getForObject(url, String.class);
222 ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
223 assertThat(status.keepAliveInterval() == 1);
224 assertThat(status.name().equals("name"));
226 url = baseUrl() + "/services";
227 rsp = this.restTemplate.getForObject(url, String.class);
228 assertThat(rsp.contains("name"));
229 System.out.println(rsp);
231 url = baseUrl() + "/service/ping";
232 this.restTemplate.put(url, "name");