2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.policyagent;
22 import static org.assertj.core.api.Assertions.assertThat;
25 import java.util.HashMap;
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;
48 @RunWith(SpringRunner.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50 public class ApplicationTest {
53 private Policies policies;
56 private PolicyTypes policyTypes;
59 ApplicationConfig appConfig;
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());
71 Policies getPolicies() {
72 return new Policies();
76 PolicyTypes getPolicyTypes() {
77 return new PolicyTypes();
81 ApplicationConfig getApplicationConfig() {
82 return new MockApplicationConfig();
89 private RestTemplate restTemplate = new RestTemplate();
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");
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");
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");
114 // managedElmentId -> nodeName
117 public void putPolicy() throws Exception {
118 // types.putType("type1", ImmutablePolicyType.builder().name("").jsonSchema("").build());
120 String url = "http://localhost:" + port + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
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);
130 HttpEntity<String> entity = new HttpEntity<String>(json);
132 addPolicyType(policyTypes, "type1");
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");
141 private void addPolicyType(PolicyTypes policyTypes, String name) {
142 PolicyType type = ImmutablePolicyType.builder() //
147 policyTypes.putType(name, type);