Added ServiceController and Service supervision
[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 com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27
28 import java.net.URL;
29
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;
55
56 @RunWith(SpringRunner.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 public class ApplicationTest {
59
60     @Autowired
61     private Rics rics;
62
63     @Autowired
64     private Policies policies;
65
66     @Autowired
67     private PolicyTypes policyTypes;
68
69     private static Gson gson = new GsonBuilder() //
70         .serializeNulls() //
71         .create(); //
72
73     static class MockApplicationConfig extends ApplicationConfig {
74         @Override
75         public void initialize() {
76             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
77             loadConfigurationFromFile(url.getFile());
78         }
79     }
80
81     /**
82      * overrides the BeanFactory
83      */
84     @TestConfiguration
85     static class BeanFactory {
86
87         @Bean
88         public ApplicationConfig getApplicationConfig() {
89             return new MockApplicationConfig();
90         }
91
92     }
93
94     @LocalServerPort
95     private int port;
96
97     private RestTemplate restTemplate = new RestTemplate();
98
99     @Test
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");
104     }
105
106     @Test
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");
111     }
112
113     // managedElmentId -> nodeName
114
115     @Test
116     public void putPolicy() throws Exception {
117         putService("service1");
118
119         String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
120         String json = "{}";
121         addPolicyType("type1");
122
123         this.restTemplate.put(url, json);
124
125         Policy policy = policies.get("instance1");
126
127         assertThat(policy).isNotNull();
128         assertThat(policy.id()).isEqualTo("instance1");
129         assertThat(policy.ownerServiceName()).isEqualTo("service1");
130     }
131
132     private PolicyType addPolicyType(String name) {
133         PolicyType type = ImmutablePolicyType.builder() //
134             .jsonSchema("") //
135             .name(name) //
136             .build();
137
138         policyTypes.put(type);
139         return type;
140     }
141
142     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
143         Policy p = ImmutablePolicy.builder().id(id) //
144             .json("{}") //
145             .ownerServiceName(service) //
146             .ric(rics.getRic("ric1")) //
147             .type(addPolicyType(typeName)) //
148             .build();
149         policies.put(p);
150         return p;
151     }
152
153     private String baseUrl() {
154         return "http://localhost:" + port;
155     }
156
157     @Test
158     public void getPolicy() throws Exception {
159         String url = baseUrl() + "/policy?instance=id";
160         Policy policy = addPolicy("id", "typeName", "service1");
161         {
162             String rsp = this.restTemplate.getForObject(url, String.class);
163             assertThat(rsp).isEqualTo(policy.json());
164         }
165         {
166             policies.remove(policy);
167             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
168             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
169         }
170     }
171
172     @Test
173     public void getPolicies() throws Exception {
174         String url = baseUrl() + "/policies";
175         addPolicy("id1", "type1", "service1");
176         addPolicy("id2", "type2", "service2");
177
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     }
183
184     @Test
185     public void getPoliciesFilter() throws Exception {
186         addPolicy("id1", "type1", "service1");
187         addPolicy("id2", "type1", "service2");
188         addPolicy("id3", "type2", "service1");
189
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"));
196
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"));
203     }
204
205     private void putService(String name) {
206         String url = baseUrl() + "/service";
207
208         ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
209             .keepAliveInterval(1) //
210             .name(name) //
211             .build();
212         String json = gson.toJson(service);
213         this.restTemplate.put(url, json);
214     }
215
216     @Test
217     public void putAndGetService() throws Exception {
218         putService("name");
219
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"));
225
226         url = baseUrl() + "/services";
227         rsp = this.restTemplate.getForObject(url, String.class);
228         assertThat(rsp.contains("name"));
229         System.out.println(rsp);
230
231         url = baseUrl() + "/service/ping";
232         this.restTemplate.put(url, "name");
233     }
234
235 }