c7d2e5579aaae6e26876f61ba1bd5d3f04b6bbc8
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / ApplicationTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.policyagent;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertFalse;
24
25 import java.net.URL;
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;
51
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 public class ApplicationTest {
55
56     @Autowired
57     private Beans beans;
58
59     static class MockApplicationConfig extends ApplicationConfig {
60         @Override
61         public void initialize() {
62             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
63             loadConfigurationFromFile(url.getFile());
64         }
65     }
66
67     @TestConfiguration
68     static class Beans {
69         @Bean
70         public Rics getRics() {
71             return new Rics();
72         }
73
74         @Bean
75         public Policies getPolicies() {
76             return new Policies();
77         }
78
79         @Bean
80         public PolicyTypes getPolicyTypes() {
81             return new PolicyTypes();
82         }
83
84         @Bean
85         public ApplicationConfig getApplicationConfig() {
86             return new MockApplicationConfig();
87         }
88     }
89
90     @LocalServerPort
91     private int port;
92
93     private RestTemplate restTemplate = new RestTemplate();
94
95     @Test
96     public void getRics() throws Exception {
97         String cmd = "/rics";
98         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
99         assertThat(rsp).contains("kista_1");
100     }
101
102     @Test
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");
107     }
108
109     // managedElmentId -> nodeName
110
111     @Test
112     public void putPolicy() throws Exception {
113         String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
114         String json = "{}";
115         addPolicyType("type1");
116         addRic(beans.getRics(), "ric1", url);
117
118         this.restTemplate.put(url, json);
119
120         Policy policy = beans.getPolicies().get("instance1");
121
122         assertThat(policy).isNotNull();
123         assertThat(policy.id()).isEqualTo("instance1");
124         assertThat(policy.ownerServiceName()).isEqualTo("service1");
125     }
126
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() //
131             .name(ric) //
132             .baseUrl(url) //
133             .managedElementIds(nodeNames) //
134             .build();
135         Ric ricObj = new Ric(ricConfig);
136
137         rics.put(ricObj);
138     }
139
140     private PolicyType addPolicyType(String name) {
141         PolicyType type = ImmutablePolicyType.builder() //
142             .jsonSchema("") //
143             .name(name) //
144             .build();
145
146         beans.getPolicyTypes().put(type);
147         return type;
148     }
149
150     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
151         Policy p = ImmutablePolicy.builder().id(id) //
152             .json("{}") //
153             .ownerServiceName(service) //
154             .ric(beans.getRics().getRic("ric1")) //
155             .type(addPolicyType(typeName)) //
156             .build();
157         beans.getPolicies().put(p);
158         return p;
159     }
160
161     @Test
162     public void getPolicy() throws Exception {
163         String url = "http://localhost:" + port + "/policy?instance=id";
164         Policy policy = addPolicy("id", "typeName", "service1");
165         {
166             String rsp = this.restTemplate.getForObject(url, String.class);
167             assertThat(rsp).isEqualTo(policy.json());
168         }
169         {
170             beans.getPolicies().remove(policy);
171             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
172             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
173         }
174     }
175
176     @Test
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");
182
183         String rsp = this.restTemplate.getForObject(url, String.class);
184         System.out.println(rsp);
185         assertThat(rsp).contains("id1");
186         assertThat(rsp).contains("id2");
187     }
188
189     @Test
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");
195
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"));
201
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"));
208
209     }
210
211 }