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;
26 import java.util.Vector;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.oransc.policyagent.configuration.ApplicationConfig;
30 import org.oransc.policyagent.configuration.ImmutableRicConfig;
31 import org.oransc.policyagent.configuration.RicConfig;
32 import org.oransc.policyagent.exceptions.ServiceException;
33 import org.oransc.policyagent.repository.ImmutablePolicy;
34 import org.oransc.policyagent.repository.ImmutablePolicyType;
35 import org.oransc.policyagent.repository.Policies;
36 import org.oransc.policyagent.repository.Policy;
37 import org.oransc.policyagent.repository.PolicyType;
38 import org.oransc.policyagent.repository.PolicyTypes;
39 import org.oransc.policyagent.repository.Ric;
40 import org.oransc.policyagent.repository.Rics;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.boot.test.context.TestConfiguration;
45 import org.springframework.boot.web.server.LocalServerPort;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.test.context.junit4.SpringRunner;
50 import org.springframework.web.client.RestTemplate;
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 public class ApplicationTest {
59 static class MockApplicationConfig extends ApplicationConfig {
61 public void initialize() {
62 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
63 loadConfigurationFromFile(url.getFile());
70 public Rics getRics() {
75 public Policies getPolicies() {
76 return new Policies();
80 public PolicyTypes getPolicyTypes() {
81 return new PolicyTypes();
85 public ApplicationConfig getApplicationConfig() {
86 return new MockApplicationConfig();
93 private RestTemplate restTemplate = new RestTemplate();
96 public void getRics() throws Exception {
98 String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
99 assertThat(rsp).contains("kista_1");
103 public void getRic() throws Exception {
104 String cmd = "/ric?managedElementId=kista_1";
105 String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
106 assertThat(rsp).isEqualTo("ric1");
109 // managedElmentId -> nodeName
112 public void putPolicy() throws Exception {
113 String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
115 addPolicyType("type1");
116 addRic(beans.getRics(), "ric1", url);
118 this.restTemplate.put(url, json);
120 Policy policy = beans.getPolicies().get("instance1");
122 assertThat(policy).isNotNull();
123 assertThat(policy.id()).isEqualTo("instance1");
124 assertThat(policy.ownerServiceName()).isEqualTo("service1");
127 private void addRic(Rics rics, String ric, String url) {
128 Vector<String> nodeNames = new Vector<>(1);
129 nodeNames.add("node1");
130 RicConfig ricConfig = ImmutableRicConfig.builder() //
133 .managedElementIds(nodeNames) //
135 Ric ricObj = new Ric(ricConfig);
140 private PolicyType addPolicyType(String name) {
141 PolicyType type = ImmutablePolicyType.builder() //
146 beans.getPolicyTypes().put(type);
150 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
151 Policy p = ImmutablePolicy.builder().id(id) //
153 .ownerServiceName(service) //
154 .ric(beans.getRics().getRic("ric1")) //
155 .type(addPolicyType(typeName)) //
157 beans.getPolicies().put(p);
162 public void getPolicy() throws Exception {
163 String url = "http://localhost:" + port + "/policy?instance=id";
164 Policy policy = addPolicy("id", "typeName", "service1");
166 String rsp = this.restTemplate.getForObject(url, String.class);
167 assertThat(rsp).isEqualTo(policy.json());
170 beans.getPolicies().remove(policy);
171 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
172 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
177 public void getPolicies() throws Exception {
178 String url = "http://localhost:" + port + "/policies";
179 addRic(beans.getRics(), "ric1", url);
180 addPolicy("id1", "type1", "service1");
181 addPolicy("id2", "type2", "service2");
183 String rsp = this.restTemplate.getForObject(url, String.class);
184 System.out.println(rsp);
185 assertThat(rsp).contains("id1");
186 assertThat(rsp).contains("id2");
190 public void getPoliciesFilter() throws Exception {
191 String url = "http://localhost:" + port + "/policies?type=type1";
192 addPolicy("id1", "type1", "service1");
193 addPolicy("id2", "type1", "service2");
194 addPolicy("id3", "type2", "service1");
196 String rsp = this.restTemplate.getForObject(url, String.class);
197 System.out.println(rsp);
198 assertThat(rsp).contains("id1");
199 assertThat(rsp).contains("id2");
200 assertFalse(rsp.contains("id3"));
202 url = "http://localhost:" + port + "/policies?type=type1&service=service2";
203 rsp = this.restTemplate.getForObject(url, String.class);
204 System.out.println(rsp);
205 assertFalse(rsp.contains("id1"));
206 assertThat(rsp).contains("id2");
207 assertFalse(rsp.contains("id3"));