+
+ @Test
+ public void testGroupsUpdate() throws Exception {
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpGroups(null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpGroups(null, new ArrayList<>());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpGroups(pfDao, null);
+ }).hasMessageMatching("pdpGroups is marked .*ull but is null");
+
+ String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
+ PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
+
+ PdpGroups createdPdpGroups0 = new PdpGroups();
+ createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
+ String createdJson = standardCoder.encode(createdPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
+
+ PdpGroups gotPdpGroups0 = new PdpGroups();
+ gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
+
+ String gotJson = standardCoder.encode(gotPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
+
+ String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
+ PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
+
+ PdpGroups updatedPdpGroups0 = new PdpGroups();
+ updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
+
+ List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
+ List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
+ assertTrue(beforePdpInstances.containsAll(afterPdpInstances));
+
+ pdpGroups0.getGroups().get(0).setPdpGroupState(null);
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpGroups(pfDao, pdpGroups0.getGroups());
+ }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
+ .hasMessageContaining(Validated.IS_NULL);
+ }
+
+ @Test
+ public void testPoliciesDelete() throws Exception {
+ assertThatThrownBy(() -> {
+ new PdpProvider().deletePdpGroup(null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().deletePdpGroup(null, "name");
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().deletePdpGroup(pfDao, null);
+ }).hasMessageMatching("name is marked .*ull but is null");
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().deletePdpGroup(pfDao, "name");
+ }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
+
+ String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
+ PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
+
+ PdpGroups createdPdpGroups0 = new PdpGroups();
+ createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
+ String createdJson = standardCoder.encode(createdPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
+
+ PdpGroups gotPdpGroups0 = new PdpGroups();
+ gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
+
+ String gotJson = standardCoder.encode(gotPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
+
+ PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
+
+ assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
+
+ assertEquals(0, new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0).size());
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
+ }).hasMessage("delete of PDP group \"PdpGroup0:0.0.0\" failed, PDP group does not exist");
+ }
+
+ @Test
+ public void testPdpSubgroupUpdate() throws Exception {
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(null, null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(null, null, new PdpSubGroup());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(null, "name", null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(null, "name", new PdpSubGroup());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(pfDao, null, null);
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(pfDao, null, new PdpSubGroup());
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(pfDao, "name", null);
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(pfDao, "name", new PdpSubGroup());
+ }).hasMessage("parameter \"localName\" is null");
+
+ String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
+ PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
+
+ PdpGroups createdPdpGroups0 = new PdpGroups();
+ createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
+ String createdJson = standardCoder.encode(createdPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
+
+ PdpGroups gotPdpGroups0 = new PdpGroups();
+ gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
+
+ String gotJson = standardCoder.encode(gotPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
+
+ PdpSubGroup existingSubGroup = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0);
+ existingSubGroup.setCurrentInstanceCount(10);
+ existingSubGroup.setDesiredInstanceCount(10);
+ new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
+
+ List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
+ assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getCurrentInstanceCount());
+ assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
+
+ existingSubGroup.setDesiredInstanceCount(-1);
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
+ }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
+ .hasMessageContaining("below the minimum value");
+ existingSubGroup.setDesiredInstanceCount(10);
+ }
+
+ @Test
+ public void testPdpUpdate() throws Exception {
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, null, null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, null, null, new Pdp());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, null, "TYPE", null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, null, "TYPE", new Pdp());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, "name", null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, "name", null, new Pdp());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, "name", "TYPE", null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(null, "name", "TYPE", new Pdp());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, null, null, null);
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, null, null, new Pdp());
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, null, "TYPE", null);
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, null, "TYPE", new Pdp());
+ }).hasMessageMatching(GROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, "name", null, null);
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, "name", null, new Pdp());
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, "name", "TYPE", null);
+ }).hasMessageMatching("pdp is marked .*ull but is null");
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, "name", "TYPE", new Pdp());
+ }).hasMessage("parameter \"localName\" is null");
+
+ String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
+ PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
+
+ PdpGroups createdPdpGroups0 = new PdpGroups();
+ createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
+ String createdJson = standardCoder.encode(createdPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
+
+ PdpGroups gotPdpGroups0 = new PdpGroups();
+ gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
+
+ String gotJson = standardCoder.encode(gotPdpGroups0);
+ assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
+
+ Pdp existingPdp = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances().get(0);
+ existingPdp.setPdpState(PdpState.TEST);
+ existingPdp.setHealthy(PdpHealthStatus.TEST_IN_PROGRESS);
+ new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
+
+ List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
+ assertEquals(PdpState.TEST,
+ afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
+ assertEquals(PdpHealthStatus.TEST_IN_PROGRESS,
+ afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
+
+ existingPdp.setMessage("");
+ assertThatThrownBy(() -> {
+ new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
+ }).hasMessageContaining("PDP").hasMessageContaining("message").hasMessageContaining(Validated.IS_BLANK);
+ existingPdp.setMessage("A Message");
+ }
+
+ @Test
+ public void testGetAllPolicyStatusPfDao() throws PfModelException {
+ assertThatThrownBy(() -> {
+ new PdpProvider().getAllPolicyStatus(null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty();
+
+ PdpProvider provider = loadDeployments();
+ assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5);
+ }
+
+ private PdpProvider loadDeployments() {
+ PdpProvider provider = new PdpProvider();
+
+ // same name, different version
+ final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30");
+
+ PdpPolicyStatus id1 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp1").policy(MY_POLICY).build();
+ PdpPolicyStatus id2 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp2").policy(MY_POLICY2).build();
+ PdpPolicyStatus id3 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp3").policy(policy3).build();
+ PdpPolicyStatus id4 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp4").policy(MY_POLICY).build();
+ PdpPolicyStatus id5 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp5").policy(MY_POLICY2).build();
+ provider.cudPolicyStatus(pfDao, List.of(id1, id2, id3, id4, id5), null, null);
+
+ return provider;
+ }
+
+ @Test
+ public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException {
+ assertThatThrownBy(() -> {
+ new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null));
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().getAllPolicyStatus(pfDao, null);
+ }).hasMessageContaining("policy").hasMessageContaining("null");
+
+ assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null)))
+ .isEmpty();
+
+ PdpProvider provider = loadDeployments();
+ assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2);
+ assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null)))
+ .hasSize(3);
+ }
+
+ @Test
+ public void testGetGroupPolicyStatus() throws PfModelException {
+ assertThatThrownBy(() -> {
+ new PdpProvider().getGroupPolicyStatus(null, "someGroup");
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new PdpProvider().getGroupPolicyStatus(pfDao, null);
+ }).hasMessageContaining("group").hasMessageContaining("null");
+
+ assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
+
+ PdpProvider provider = loadDeployments();
+ assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3);
+ }
+
+ @Test
+ public void cudPolicyStatus() throws PfModelException {
+ PdpProvider prov = new PdpProvider();
+
+ assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
+ .hasMessageMatching(DAO_IS_NULL);
+
+ // null collections should be OK
+ assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
+ }
+
+ @Test
+ public void cudPolicyStatus_Create() throws PfModelException {
+ PdpProvider prov = new PdpProvider();
+
+ PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
+ PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
+ PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_B).pdpId("idZ").build();
+ prov.cudPolicyStatus(pfDao, List.of(idx, idy), null, null);
+ prov.cudPolicyStatus(pfDao, List.of(idz), null, null);
+
+ List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
+ assertThat(records).hasSize(2);
+
+ Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
+ assertThat(records.get(0)).isEqualTo(idx);
+ assertThat(records.get(1)).isEqualTo(idy);
+
+ records = prov.getGroupPolicyStatus(pfDao, GROUP_B);
+ assertThat(records).hasSize(1);
+ assertThat(records.get(0)).isEqualTo(idz);
+ }
+
+ @Test
+ public void cudPolicyStatus_Update() throws PfModelException {
+ PdpProvider prov = new PdpProvider();
+
+ PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("wId").build();
+ PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("xId").build();
+ PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("yId").build();
+ PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("zId").build();
+ prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
+
+ assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
+
+ /*
+ * Now update some records.
+ */
+ idx.setState(State.FAILURE);
+ idz.setState(State.WAITING);
+ prov.cudPolicyStatus(pfDao, null, List.of(idx, idz), null);
+ List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
+ assertThat(records).hasSize(4);
+
+ Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
+ assertThat(records.get(0)).isEqualTo(idw);
+ assertThat(records.get(1)).isEqualTo(idx);
+ assertThat(records.get(2)).isEqualTo(idy);
+ assertThat(records.get(3)).isEqualTo(idz);
+ }
+
+ @Test
+ public void cudPolicyStatus_Delete() throws PfModelException {
+ PdpProvider prov = new PdpProvider();
+
+ PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("idW").build();
+ PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
+ PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
+ PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("idZ").build();
+ prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
+
+ assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
+
+ /*
+ * Delete some records and then check again.
+ */
+ prov.cudPolicyStatus(pfDao, null, null, List.of(idw, idy));
+
+ List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
+ assertThat(records).hasSize(2);
+
+ Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
+ assertThat(records.get(0)).isEqualTo(idx);
+ assertThat(records.get(1)).isEqualTo(idz);
+ }
+
+ @Test
+ public void testFromAuthorativeStatus() throws PfModelException {
+ PdpProvider prov = new PdpProvider();
+
+ assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
+
+ PdpPolicyStatus ida = statusBuilder.pdpGroup(GROUP_A).pdpId("idA").build();
+ PdpPolicyStatus idb = statusBuilder.pdpGroup(GROUP_A).pdpId("idB").build();
+ PdpPolicyStatus idc = statusBuilder.pdpGroup(GROUP_A).pdpId("idC").build();
+ PdpPolicyStatus idd = statusBuilder.pdpGroup(GROUP_A).pdpId("idD").build();
+
+ // make a couple invalid records
+ idb.setState(null);
+ idd.setState(null);
+
+ List<PdpPolicyStatus> list = List.of(ida, idb, idc, idd);
+
+ // @formatter:off
+ assertThatCode(() -> prov.cudPolicyStatus(pfDao, list, null, null))
+ .isInstanceOf(PfModelRuntimeException.class)
+ .hasMessageContaining("1").hasMessageContaining("3")
+ .hasMessageNotContaining("0").hasMessageNotContaining("2");
+ // @formatter:on
+
+ assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).isEmpty();
+ }