+ assertThat(policy.id()).isEqualTo(policyInstanceId);
+ assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
+ assertThat(policy.ric().name()).isEqualTo("ric1");
+ assertThat(policy.isTransient()).isEqualTo(true);
+
+ // Put a non transient policy
+ url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
+ restClient().put(url, policyBody).block();
+ policy = policies.getPolicy(policyInstanceId);
+ assertThat(policy.isTransient()).isEqualTo(false);
+
+ url = "/policies";
+ String rsp = restClient().get(url).block();
+ assertThat(rsp.contains(policyInstanceId)).as("Response contains policy instance ID.").isTrue();
+
+ url = "/policy?id=" + policyInstanceId;
+ rsp = restClient().get(url).block();
+ assertThat(rsp).isEqualTo(policyBody);
+
+ // Test of error codes
+ url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
+ testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
+
+ url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
+ testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
+
+ url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
+ this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
+ testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
+ this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
+ }
+
+ @Test
+ /**
+ * Test that HttpStatus and body from failing REST call to A1 is passed on to
+ * the caller.
+ *
+ * @throws ServiceException
+ */
+ void testErrorFromRIC() throws ServiceException {
+ putService("service1");
+ addPolicyType("type1", "ric1");
+
+ String url = putPolicyUrl("service1", "ric1", "type1", "id1");
+ MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
+ HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
+ String responseBody = "Refused";
+ byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
+
+ WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
+ responseBodyBytes, StandardCharsets.UTF_8, null);
+ doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
+
+ // PUT Policy
+ testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
+
+ // DELETE POLICY
+ this.addPolicy("instance1", "type1", "service1", "ric1");
+ doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
+ testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
+
+ // GET STATUS
+ this.addPolicy("instance1", "type1", "service1", "ric1");
+ doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
+ testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
+
+ // Check that empty response body is OK
+ a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
+ doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
+ testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
+ }
+
+ @Test
+ void testPutTypelessPolicy() throws Exception {
+ putService("service1");
+ addPolicyType("", "ric1");
+ String url = putPolicyUrl("service1", "ric1", "", "id1");
+ restClient().put(url, jsonString()).block();
+
+ String rsp = restClient().get("/policies").block();
+ List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
+ assertThat(info.size()).isEqualTo(1);
+ PolicyInfo policyInfo = info.get(0);
+ assertThat(policyInfo.id).isEqualTo("id1");
+ assertThat(policyInfo.type).isEqualTo("");
+ }
+
+ @Test
+ void testRefuseToUpdatePolicy() throws Exception {
+ // Test that only the json can be changed for a already created policy
+ // In this case service is attempted to be changed
+ this.addRic("ric1");
+ this.addRic("ricXXX");
+ this.addPolicy("instance1", "type1", "service1", "ric1");
+ this.addPolicy("instance2", "type1", "service1", "ricXXX");
+
+ // Try change ric1 -> ricXXX
+ String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
+ testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
+ }
+
+ @Test
+ void testGetPolicy() throws Exception {
+ String url = "/policy?id=id";
+ Policy policy = addPolicy("id", "typeName", "service1", "ric1");
+ {
+ String rsp = restClient().get(url).block();
+ assertThat(rsp).isEqualTo(policy.json());
+ }
+ {
+ policies.remove(policy);
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+ }
+ }
+
+ @Test
+ void testDeletePolicy() throws Exception {
+ addPolicy("id", "typeName", "service1", "ric1");
+ assertThat(policies.size()).isEqualTo(1);
+
+ String url = "/policy?id=id";
+ ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
+
+ assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
+ assertThat(policies.size()).isEqualTo(0);
+
+ // Delete a non existing policy
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+ }
+
+ @Test
+ void testGetPolicySchemas() throws Exception {
+ addPolicyType("type1", "ric1");
+ addPolicyType("type2", "ric2");
+
+ String url = "/policy_schemas";
+ String rsp = this.restClient().get(url).block();
+ assertThat(rsp).contains("type1");
+ assertThat(rsp).contains("[{\"title\":\"type2\"}");
+
+ List<String> info = parseSchemas(rsp);
+ assertThat(info.size()).isEqualTo(2);
+
+ url = "/policy_schemas?ric=ric1";
+ rsp = restClient().get(url).block();
+ assertThat(rsp).contains("type1");
+ info = parseSchemas(rsp);
+ assertThat(info.size()).isEqualTo(1);
+
+ // Get schema for non existing RIC
+ url = "/policy_schemas?ric=ric1XXX";
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
+ }
+
+ @Test
+ void testGetPolicySchema() throws Exception {
+ addPolicyType("type1", "ric1");
+ addPolicyType("type2", "ric2");
+
+ String url = "/policy_schema?id=type1";
+ String rsp = restClient().get(url).block();
+ logger.info(rsp);
+ assertThat(rsp).contains("type1");
+ assertThat(rsp).contains("title");
+
+ // Get non existing schema
+ url = "/policy_schema?id=type1XX";
+ testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);