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=3b98ef561c20aa2c92a20d1d8f4008e66b34a8c7;hb=8b92754714856090ad81f5a18aa9c93e6b19fe99;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..3b98ef56 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,23 @@ * 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 java.net.URL; + import org.junit.Test; import org.junit.runner.RunWith; import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.controllers.ImmutableServiceRegistrationInfo; +import org.oransc.policyagent.controllers.ImmutableServiceStatus; +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; @@ -51,7 +58,17 @@ import org.springframework.web.client.RestTemplate; public class ApplicationTest { @Autowired - private Beans beans; + private Rics rics; + + @Autowired + private Policies policies; + + @Autowired + private PolicyTypes policyTypes; + + private static Gson gson = new GsonBuilder() // + .serializeNulls() // + .create(); // static class MockApplicationConfig extends ApplicationConfig { @Override @@ -61,27 +78,17 @@ 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 BeanFactory { @Bean public ApplicationConfig getApplicationConfig() { return new MockApplicationConfig(); } + } @LocalServerPort @@ -91,15 +98,15 @@ public class ApplicationTest { @Test public void getRics() throws Exception { - String cmd = "/rics"; - String rsp = this.restTemplate.getForObject("http://localhost:" + port + cmd, String.class); + 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); + String url = baseUrl() + "/ric?managedElementId=kista_1"; + String rsp = this.restTemplate.getForObject(url, String.class); assertThat(rsp).isEqualTo("ric1"); } @@ -107,13 +114,15 @@ public class ApplicationTest { @Test public void putPolicy() throws Exception { - String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1"; + 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"); @@ -126,7 +135,7 @@ public class ApplicationTest { .name(name) // .build(); - beans.getPolicyTypes().put(type); + policyTypes.put(type); return type; } @@ -134,23 +143,27 @@ public class ApplicationTest { Policy p = ImmutablePolicy.builder().id(id) // .json("{}") // .ownerServiceName(service) // - .ric(beans.getRics().getRic("ric1")) // + .ric(rics.getRic("ric1")) // .type(addPolicyType(typeName)) // .build(); - beans.getPolicies().put(p); + policies.put(p); return p; } + private String baseUrl() { + return "http://localhost:" + port; + } + @Test public void getPolicy() throws Exception { - String url = "http://localhost:" + port + "/policy?instance=id"; + String url = baseUrl() + "/policy?instance=id"; Policy policy = addPolicy("id", "typeName", "service1"); { 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()); } @@ -158,7 +171,7 @@ public class ApplicationTest { @Test public void getPolicies() throws Exception { - String url = "http://localhost:" + port + "/policies"; + String url = baseUrl() + "/policies"; addPolicy("id1", "type1", "service1"); addPolicy("id2", "type2", "service2"); @@ -174,20 +187,49 @@ public class ApplicationTest { 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 void putService(String name) { + String url = baseUrl() + "/service"; + + ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() // + .keepAliveInterval(1) // + .name(name) // + .build(); + String json = gson.toJson(service); + this.restTemplate.put(url, json); + } + + @Test + public void putAndGetService() 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"); } }