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