+ assertThat(rsp.contains("id3")).isFalse();
+
+ url = "/policy_ids?type=type1&service=service1&ric=ric1";
+ rsp = restClient().get(url).block();
+ assertThat(rsp).isEqualTo("[\"id1\"]");
+
+ // Test get policy ids for non existing type
+ url = "/policy_ids?type=type1XXX";
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+
+ // Test get policy ids for non existing RIC
+ url = "/policy_ids?ric=XXX";
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+ }
+
+ @Test
+ public void testPutAndGetService() throws Exception {
+ // PUT
+ putService("name", 0, HttpStatus.CREATED);
+ putService("name", 0, HttpStatus.OK);
+
+ // GET one service
+ String url = "/services?name=name";
+ String rsp = restClient().get(url).block();
+ List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
+ assertThat(info.size()).isEqualTo(1);
+ ServiceStatus status = info.iterator().next();
+ assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
+ assertThat(status.serviceName).isEqualTo("name");
+
+ // GET (all)
+ url = "/services";
+ rsp = restClient().get(url).block();
+ assertThat(rsp.contains("name")).isTrue();
+ logger.info(rsp);
+
+ // Keep alive
+ url = "/services/keepalive?name=name";
+ ResponseEntity<String> entity = restClient().putForEntity(url).block();
+ assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
+
+ // DELETE service
+ assertThat(services.size()).isEqualTo(1);
+ url = "/services?name=name";
+ restClient().delete(url).block();
+ assertThat(services.size()).isEqualTo(0);
+
+ // Keep alive, no registerred service
+ testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
+
+ // PUT servive with bad payload
+ testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
+ testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
+ testErrorCode(restClient().put("/service", createServiceJson("name", -123)), HttpStatus.BAD_REQUEST);
+ testErrorCode(restClient().put("/service", createServiceJson("name", 0, "missing.portandprotocol.com")),
+ HttpStatus.BAD_REQUEST);
+
+ // GET non existing servive
+ testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
+ }
+
+ @Test
+ public void testServiceSupervision() throws Exception {
+ putService("service1", 1, HttpStatus.CREATED);
+ addPolicyType("type1", "ric1");
+
+ String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
+ final String policyBody = jsonString();
+ restClient().put(url, policyBody).block();
+
+ assertThat(policies.size()).isEqualTo(1);
+ assertThat(services.size()).isEqualTo(1);
+
+ // Timeout after ~1 second
+ await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
+ assertThat(services.size()).isEqualTo(0);
+ }
+
+ @Test
+ public void testGetPolicyStatus() throws Exception {
+ addPolicy("id", "typeName", "service1", "ric1");
+ assertThat(policies.size()).isEqualTo(1);
+
+ String url = "/policy_status?id=id";
+ String rsp = restClient().get(url).block();
+ assertThat(rsp.equals("OK")).isTrue();
+
+ // GET non existing policy status
+ url = "/policy_status?id=XXX";
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+ }
+
+ private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
+ addRic(ric);
+ Policy p = ImmutablePolicy.builder().id(id) //
+ .json(jsonString()) //
+ .ownerServiceName(service) //
+ .ric(rics.getRic(ric)) //
+ .type(addPolicyType(typeName, ric)) //
+ .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 createServiceJson(String name, long keepAliveIntervalSeconds) {
+ return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
+ }