Added a getPolicies query REST call
[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
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.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.test.context.TestConfiguration;
41 import org.springframework.boot.web.server.LocalServerPort;
42 import org.springframework.context.annotation.Bean;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.test.context.junit4.SpringRunner;
46 import org.springframework.web.client.RestTemplate;
47
48 @RunWith(SpringRunner.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50 public class ApplicationTest {
51
52     @Autowired
53     private Policies policies;
54
55     @Autowired
56     private PolicyTypes policyTypes;
57
58     @Autowired
59     ApplicationConfig appConfig;
60
61     static class MockApplicationConfig extends ApplicationConfig {
62         public void initialize() {
63             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
64             loadConfigurationFromFile(url.getFile());
65         }
66     }
67
68     @TestConfiguration
69     static class Beans {
70         @Bean
71         Policies getPolicies() {
72             return new Policies();
73         }
74
75         @Bean
76         PolicyTypes getPolicyTypes() {
77             return new PolicyTypes();
78         }
79
80         @Bean
81         ApplicationConfig getApplicationConfig() {
82             return new MockApplicationConfig();
83         }
84     }
85
86     @LocalServerPort
87     private int port;
88
89     private RestTemplate restTemplate = new RestTemplate();
90
91     @Test
92     public void getRics() throws Exception {
93         String cmd = "/rics";
94         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
95         assertThat(rsp).contains("kista_1");
96     }
97
98     @Test
99     public void getRic() throws Exception {
100         String cmd = "/ric?managedElementId=kista_1";
101         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
102         assertThat(rsp).isEqualTo("ric1");
103     }
104
105     // managedElmentId -> nodeName
106
107     @Test
108     public void putPolicy() throws Exception {
109         String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
110         String json = "{}";
111         addPolicyType("type1");
112
113         this.restTemplate.put(url, json);
114         Policy policy = this.policies.get("instance1");
115
116         assertThat(policy).isNotNull();
117         assertThat(policy.id()).isEqualTo("instance1");
118         assertThat(policy.ownerServiceName()).isEqualTo("service1");
119     }
120
121     private PolicyType addPolicyType(String name) {
122         PolicyType type = ImmutablePolicyType.builder() //
123             .jsonSchema("") //
124             .name(name) //
125             .build();
126
127         policyTypes.put(type);
128         return type;
129     }
130
131     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
132         Policy p = ImmutablePolicy.builder().id(id) //
133             .json("{}") //
134             .ownerServiceName(service) //
135             .ric(appConfig.getRic("ric1")) //
136             .type(addPolicyType(typeName)) //
137             .build();
138         this.policies.put(p);
139         return p;
140     }
141
142     @Test
143     public void getPolicy() throws Exception {
144         String url = "http://localhost:" + port + "/policy?instance=id";
145         Policy policy = addPolicy("id", "typeName", "service1");
146         {
147             String rsp = this.restTemplate.getForObject(url, String.class);
148             assertThat(rsp).isEqualTo(policy.json());
149         }
150         {
151             this.policies.remove(policy);
152             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
153             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
154         }
155     }
156
157     @Test
158     public void getPolicies() throws Exception {
159         String url = "http://localhost:" + port + "/policies";
160         addPolicy("id1", "type1", "service1");
161         addPolicy("id2", "type2", "service2");
162
163         String rsp = this.restTemplate.getForObject(url, String.class);
164         System.out.println(rsp);
165         assertThat(rsp).contains("id1");
166         assertThat(rsp).contains("id2");
167     }
168
169     @Test
170     public void getPoliciesFilter() throws Exception {
171         String url = "http://localhost:" + port + "/policies?type=type1";
172         addPolicy("id1", "type1", "service1");
173         addPolicy("id2", "type1", "service2");
174         addPolicy("id3", "type2", "service1");
175
176         String rsp = this.restTemplate.getForObject(url, String.class);
177         System.out.println(rsp);
178         assertThat(rsp).contains("id1");
179         assertThat(rsp).contains("id2");
180         assertFalse(rsp.contains("id3"));
181
182         url = "http://localhost:" + port + "/policies?type=type1&service=service2";
183         rsp = this.restTemplate.getForObject(url, String.class);
184         System.out.println(rsp);
185         assertFalse(rsp.contains("id1"));
186         assertThat(rsp).contains("id2");
187         assertFalse(rsp.contains("id3"));
188
189     }
190
191 }