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===================================
21 package org.oransc.policyagent;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertFalse;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.oransc.policyagent.configuration.ApplicationConfig;
30 import org.oransc.policyagent.exceptions.ServiceException;
31 import org.oransc.policyagent.repository.ImmutablePolicy;
32 import org.oransc.policyagent.repository.ImmutablePolicyType;
33 import org.oransc.policyagent.repository.Policies;
34 import org.oransc.policyagent.repository.Policy;
35 import org.oransc.policyagent.repository.PolicyType;
36 import org.oransc.policyagent.repository.PolicyTypes;
37 import org.oransc.policyagent.repository.Rics;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
41 import org.springframework.boot.test.context.TestConfiguration;
42 import org.springframework.boot.web.server.LocalServerPort;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.test.context.junit4.SpringRunner;
47 import org.springframework.web.client.RestTemplate;
49 @RunWith(SpringRunner.class)
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51 public class ApplicationTest {
56 static class MockApplicationConfig extends ApplicationConfig {
58 public void initialize() {
59 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
60 loadConfigurationFromFile(url.getFile());
67 public Rics getRics() {
72 public Policies getPolicies() {
73 return new Policies();
77 public PolicyTypes getPolicyTypes() {
78 return new PolicyTypes();
82 public ApplicationConfig getApplicationConfig() {
83 return new MockApplicationConfig();
90 private RestTemplate restTemplate = new RestTemplate();
93 public void getRics() throws Exception {
95 String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
96 assertThat(rsp).contains("kista_1");
100 public void getRic() throws Exception {
101 String cmd = "/ric?managedElementId=kista_1";
102 String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
103 assertThat(rsp).isEqualTo("ric1");
106 // managedElmentId -> nodeName
109 public void putPolicy() throws Exception {
110 String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
112 addPolicyType("type1");
114 this.restTemplate.put(url, json);
116 Policy policy = beans.getPolicies().get("instance1");
118 assertThat(policy).isNotNull();
119 assertThat(policy.id()).isEqualTo("instance1");
120 assertThat(policy.ownerServiceName()).isEqualTo("service1");
123 private PolicyType addPolicyType(String name) {
124 PolicyType type = ImmutablePolicyType.builder() //
129 beans.getPolicyTypes().put(type);
133 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
134 Policy p = ImmutablePolicy.builder().id(id) //
136 .ownerServiceName(service) //
137 .ric(beans.getRics().getRic("ric1")) //
138 .type(addPolicyType(typeName)) //
140 beans.getPolicies().put(p);
145 public void getPolicy() throws Exception {
146 String url = "http://localhost:" + port + "/policy?instance=id";
147 Policy policy = addPolicy("id", "typeName", "service1");
149 String rsp = this.restTemplate.getForObject(url, String.class);
150 assertThat(rsp).isEqualTo(policy.json());
153 beans.getPolicies().remove(policy);
154 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
155 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
160 public void getPolicies() throws Exception {
161 String url = "http://localhost:" + port + "/policies";
162 addPolicy("id1", "type1", "service1");
163 addPolicy("id2", "type2", "service2");
165 String rsp = this.restTemplate.getForObject(url, String.class);
166 System.out.println(rsp);
167 assertThat(rsp).contains("id1");
168 assertThat(rsp).contains("id2");
172 public void getPoliciesFilter() throws Exception {
173 addPolicy("id1", "type1", "service1");
174 addPolicy("id2", "type1", "service2");
175 addPolicy("id3", "type2", "service1");
177 String url = "http://localhost:" + port + "/policies?type=type1";
178 String rsp = this.restTemplate.getForObject(url, String.class);
179 System.out.println(rsp);
180 assertThat(rsp).contains("id1");
181 assertThat(rsp).contains("id2");
182 assertFalse(rsp.contains("id3"));
184 url = "http://localhost:" + port + "/policies?type=type1&service=service2";
185 rsp = this.restTemplate.getForObject(url, String.class);
186 System.out.println(rsp);
187 assertFalse(rsp.contains("id1"));
188 assertThat(rsp).contains("id2");
189 assertFalse(rsp.contains("id3"));