X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2FApplicationTest.java;h=df26ba4f6bae76997a8c1381428401ccfb61e8eb;hb=f0273617b916cdc8633382291b9986e33cc13fa1;hp=beef58630c685c308f9373cd6edf1d689a05b448;hpb=ce1713127bd5445b3890efddf4609cb8b8d58054;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java index beef5863..df26ba4f 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -17,16 +17,30 @@ * limitations under the License. * ========================LICENSE_END=================================== */ - package org.oransc.policyagent; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + import java.net.URL; +import java.util.List; +import java.util.Vector; + import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; import org.junit.runner.RunWith; import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.configuration.ImmutableRicConfig; +import org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.controllers.ImmutableServiceRegistrationInfo; +import org.oransc.policyagent.controllers.ImmutableServiceStatus; +import org.oransc.policyagent.controllers.PolicyTypeInfo; +import org.oransc.policyagent.controllers.ServiceRegistrationInfo; +import org.oransc.policyagent.controllers.ServiceStatus; import org.oransc.policyagent.exceptions.ServiceException; import org.oransc.policyagent.repository.ImmutablePolicy; import org.oransc.policyagent.repository.ImmutablePolicyType; @@ -34,6 +48,7 @@ import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.PolicyType; import org.oransc.policyagent.repository.PolicyTypes; +import org.oransc.policyagent.repository.Ric; import org.oransc.policyagent.repository.Rics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -51,9 +66,19 @@ import org.springframework.web.client.RestTemplate; public class ApplicationTest { @Autowired - private Beans beans; + private Rics rics; + + @Autowired + private Policies policies; - static class MockApplicationConfig extends ApplicationConfig { + @Autowired + private PolicyTypes policyTypes; + + private static Gson gson = new GsonBuilder() // + .serializeNulls() // + .create(); // + + public static class MockApplicationConfig extends ApplicationConfig { @Override public void initialize() { URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json"); @@ -61,22 +86,11 @@ public class ApplicationTest { } } + /** + * overrides the BeanFactory + */ @TestConfiguration - static class Beans { - @Bean - public Rics getRics() { - return new Rics(); - } - - @Bean - public Policies getPolicies() { - return new Policies(); - } - - @Bean - public PolicyTypes getPolicyTypes() { - return new PolicyTypes(); - } + static class TestBeanFactory { @Bean public ApplicationConfig getApplicationConfig() { @@ -87,37 +101,51 @@ public class ApplicationTest { @LocalServerPort private int port; - private RestTemplate restTemplate = new RestTemplate(); + private final RestTemplate restTemplate = new RestTemplate(); + + @BeforeEach + public void reset() { + rics.clear(); + policies.clear(); + policyTypes.clear(); + assertThat(policies.size()).isEqualTo(0); + } @Test - public void getRics() throws Exception { - String cmd = "/rics"; - String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class); + public void testGetRics() throws Exception { + String url = baseUrl() + "/rics"; + String rsp = this.restTemplate.getForObject(url, String.class); assertThat(rsp).contains("kista_1"); } @Test - public void getRic() throws Exception { - String cmd = "/ric?managedElementId=kista_1"; - String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class); + public void testGetRic() throws Exception { + String url = baseUrl() + "/ric?managedElementId=kista_1"; + String rsp = this.restTemplate.getForObject(url, String.class); assertThat(rsp).isEqualTo("ric1"); } // managedElmentId -> nodeName @Test - public void putPolicy() throws Exception { - String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1"; + public void testPutPolicy() throws Exception { + putService("service1"); + + String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1"; String json = "{}"; addPolicyType("type1"); this.restTemplate.put(url, json); - Policy policy = beans.getPolicies().get("instance1"); + Policy policy = policies.get("instance1"); assertThat(policy).isNotNull(); assertThat(policy.id()).isEqualTo("instance1"); assertThat(policy.ownerServiceName()).isEqualTo("service1"); + + url = baseUrl() + "/policies"; + String rsp = this.restTemplate.getForObject(url, String.class); + System.out.println(rsp); } private PolicyType addPolicyType(String name) { @@ -126,39 +154,92 @@ public class ApplicationTest { .name(name) // .build(); - beans.getPolicyTypes().put(type); + policyTypes.put(type); return type; } - private Policy addPolicy(String id, String typeName, String service) throws ServiceException { + private Ric addRic(String name) { + Vector mes = new Vector<>(); + RicConfig conf = ImmutableRicConfig.builder().name(name).baseUrl("baseUrl").managedElementIds(mes).build(); + Ric ric = new Ric(conf); + this.rics.put(ric); + return ric; + } + + private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException { + addRic(ric); Policy p = ImmutablePolicy.builder().id(id) // .json("{}") // .ownerServiceName(service) // - .ric(beans.getRics().getRic("ric1")) // + .ric(rics.getRic(ric)) // .type(addPolicyType(typeName)) // - .build(); - beans.getPolicies().put(p); + .lastModified("lastModified").build(); + policies.put(p); return p; } + private Policy addPolicy(String id, String typeName, String service) throws ServiceException { + return addPolicy(id, typeName, service, "ric"); + } + + private String baseUrl() { + return "http://localhost:" + port; + } + @Test - public void getPolicy() throws Exception { - String url = "http://localhost:" + port + "/policy?instance=id"; - Policy policy = addPolicy("id", "typeName", "service1"); + public void testGetPolicy() throws Exception { + String url = baseUrl() + "/policy?instance=id"; + Policy policy = addPolicy("id", "typeName", "service1", "ric1"); { String rsp = this.restTemplate.getForObject(url, String.class); assertThat(rsp).isEqualTo(policy.json()); } { - beans.getPolicies().remove(policy); + policies.remove(policy); ResponseEntity rsp = this.restTemplate.getForEntity(url, String.class); assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value()); } } @Test - public void getPolicies() throws Exception { - String url = "http://localhost:" + port + "/policies"; + public void testDeletePolicy() throws Exception { + reset(); + String url = baseUrl() + "/policy?instance=id"; + addPolicy("id", "typeName", "service1", "ric1"); + assertThat(policies.size()).isEqualTo(1); + + this.restTemplate.delete(url); + + assertThat(policies.size()).isEqualTo(0); + } + + public static List parseList(String json, Class clazz) { + if (null == json) { + return null; + } + return gson.fromJson(json, new TypeToken() {}.getType()); + } + + @Test + public void testGetPolicyTypes() throws Exception { + String url = baseUrl() + "/policy_types"; + reset(); + addPolicy("id1", "type1", "service1"); + addPolicy("id2", "type2", "service2"); + + String rsp = this.restTemplate.getForObject(url, String.class); + System.out.println(rsp); + assertThat(rsp).contains("type1"); + assertThat(rsp).contains("type2"); + + List info = parseList(rsp, PolicyTypeInfo.class); + System.out.println(info.size()); + + } + + @Test + public void testGetPolicies() throws Exception { + String url = baseUrl() + "/policies"; addPolicy("id1", "type1", "service1"); addPolicy("id2", "type2", "service2"); @@ -169,25 +250,57 @@ public class ApplicationTest { } @Test - public void getPoliciesFilter() throws Exception { + public void testGetPoliciesFilter() throws Exception { addPolicy("id1", "type1", "service1"); addPolicy("id2", "type1", "service2"); addPolicy("id3", "type2", "service1"); - String url = "http://localhost:" + port + "/policies?type=type1"; + String url = baseUrl() + "/policies?type=type1"; String rsp = this.restTemplate.getForObject(url, String.class); System.out.println(rsp); assertThat(rsp).contains("id1"); assertThat(rsp).contains("id2"); assertFalse(rsp.contains("id3")); - url = "http://localhost:" + port + "/policies?type=type1&service=service2"; + url = baseUrl() + "/policies?type=type1&service=service2"; rsp = this.restTemplate.getForObject(url, String.class); System.out.println(rsp); assertFalse(rsp.contains("id1")); assertThat(rsp).contains("id2"); assertFalse(rsp.contains("id3")); + } + + private String createServiceJson(String name) { + ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() // + .keepAliveInterval(1) // + .name(name) // + .build(); + String json = gson.toJson(service); + return json; + } + + private void putService(String name) { + String url = baseUrl() + "/service"; + this.restTemplate.put(url, createServiceJson(name)); + } + + @Test + public void testPutAndGetService() throws Exception { + putService("name"); + + String url = baseUrl() + "/service?name=name"; + String rsp = this.restTemplate.getForObject(url, String.class); + ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class); + assertThat(status.keepAliveInterval() == 1); + assertThat(status.name().equals("name")); + + url = baseUrl() + "/services"; + rsp = this.restTemplate.getForObject(url, String.class); + assertThat(rsp.contains("name")); + System.out.println(rsp); + url = baseUrl() + "/service/ping"; + this.restTemplate.put(url, "name"); } }