Continue work with PolicyControl
[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
24 import java.net.URL;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.oransc.policyagent.configuration.ApplicationConfig;
31 import org.oransc.policyagent.repository.ImmutablePolicyType;
32 import org.oransc.policyagent.repository.Policies;
33 import org.oransc.policyagent.repository.Policy;
34 import org.oransc.policyagent.repository.PolicyType;
35 import org.oransc.policyagent.repository.PolicyTypes;
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.HttpEntity;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.MediaType;
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 getPolicy() throws Exception {
93         String cmd = "/policy?type=type3&instance=xxx";
94         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
95         System.out.println("*** rsp " + rsp);
96         assertThat(rsp).contains("type3");
97     }
98
99     @Test
100     public void getRics() throws Exception {
101         String cmd = "/rics";
102         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
103         System.out.println("*** rsp " + rsp);
104         assertThat(rsp).contains("kista_1");
105     }
106
107     @Test
108     public void getRic() throws Exception {
109         String cmd = "/ric?managedElementId=kista_1";
110         String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class);
111         assertThat(rsp).isEqualTo("ric1");
112     }
113
114     // managedElmentId -> nodeName
115
116     @Test
117     public void putPolicy() throws Exception {
118         // types.putType("type1", ImmutablePolicyType.builder().name("").jsonSchema("").build());
119
120         String url = "http://localhost:" + port + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
121
122         Map<String, String> uriVariables = new HashMap<String, String>();
123         uriVariables.put("type", "type1");
124         uriVariables.put("instance", "instance1");
125         uriVariables.put("ric", "ric1");
126         uriVariables.put("service", "service");
127         HttpHeaders headers = new HttpHeaders();
128         headers.setContentType(MediaType.APPLICATION_JSON);
129         String json = "{}";
130         HttpEntity<String> entity = new HttpEntity<String>(json);
131
132         addPolicyType(policyTypes, "type1");
133
134         this.restTemplate.put(url, entity, uriVariables);
135         Policy policy = this.policies.get("instance1");
136         assertThat(policy).isNotNull();
137         assertThat(policy.id()).isEqualTo("instance1");
138         assertThat(policy.ownerServiceName()).isEqualTo("service");
139     }
140
141     private void addPolicyType(PolicyTypes policyTypes, String name) {
142         PolicyType type = ImmutablePolicyType.builder() //
143             .jsonSchema("") //
144             .name(name) //
145             .build();
146
147         policyTypes.putType(name, type);
148     }
149
150 }