aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org
diff options
context:
space:
mode:
authora.sreekumar <ajith.sreekumar@bell.ca>2022-01-25 15:19:40 +0000
committera.sreekumar <ajith.sreekumar@bell.ca>2022-01-28 11:26:15 +0000
commit618ab3870d9ac3902edbc64cb2a69ef76ea88dfb (patch)
tree5fa67917a78e3f7805cad4a82eddc94769ecf1e5 /main/src/test/java/org
parent518e56befaeb44df719c2d1db7daf7b0de6f3c1d (diff)
Create spring repository layer for PAP
This review adds the spring repository and service layers to PAP. Once this is merged, the next work will be to actually use these spring boot based services to talk to the db directly, and not using policy-models-provider in PAP. Change-Id: Ib086b8b405020cb452e51d6359dd1a69d0000f74 Issue-ID: POLICY-3869 Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
Diffstat (limited to 'main/src/test/java/org')
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/PdpGroupServiceTest.java199
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java188
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/PolicyAuditServiceTest.java120
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/PolicyStatusServiceTest.java155
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java107
5 files changed, 769 insertions, 0 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/PdpGroupServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/PdpGroupServiceTest.java
new file mode 100644
index 00000000..e7090fcf
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/service/PdpGroupServiceTest.java
@@ -0,0 +1,199 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Collections;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.pdp.concepts.Pdp;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
+import org.onap.policy.models.pdp.concepts.PdpGroups;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.enums.PdpHealthStatus;
+import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pap.main.rest.CommonPapRestServer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class PdpGroupServiceTest extends CommonPapRestServer {
+
+ private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
+
+ private static final String DEFAULT_GROUP = "defaultGroup";
+
+ private static final String CREATE_GROUPS = "createGroups";
+
+ private static final String TYPE = "type";
+
+ private static final String NAME = "name";
+
+ private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
+
+ @Autowired
+ private PdpGroupService pdpGroupService;
+
+ private PdpGroups groupsToCreate;
+
+ private StandardCoder coder = new StandardCoder();
+
+ /**
+ * Setup before tests.
+ *
+ * @throws Exception the exception
+ */
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ PdpGroups defaultGroup = coder.decode(ResourceUtils.getResourceAsString("e2e/PdpGroups.json"), PdpGroups.class);
+ pdpGroupService.savePdpGroups(defaultGroup.getGroups());
+ groupsToCreate = coder.decode(ResourceUtils.getResourceAsString("e2e/createGroups.json"), PdpGroups.class);
+ }
+
+ @Test
+ public void testPdpGroupsCrudSuccess() {
+
+ List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
+ assertThat(pdpGroups).hasSize(1);
+ assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
+
+ pdpGroupService.savePdpGroups(groupsToCreate.getGroups());
+
+ assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
+
+ pdpGroups = pdpGroupService.getPdpGroupByName(CREATE_GROUPS);
+ assertThat(pdpGroups).hasSize(1);
+ assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
+
+ assertThat(pdpGroupService.getPdpGroupByState(PdpState.PASSIVE)).isEqualTo(pdpGroups);
+
+ List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroupByState(PdpState.ACTIVE);
+ assertThat(activePdpGroups).hasSize(1);
+ assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
+
+ assertThat(pdpGroupService.getPdpGroupByNameAndState(CREATE_GROUPS, PdpState.PASSIVE)).hasSize(1);
+ assertThat(pdpGroupService.getPdpGroupByNameAndState("invalid-group", PdpState.PASSIVE)).hasSize(0);
+ assertThat(pdpGroupService.getPdpGroupByNameAndState(DEFAULT_GROUP, PdpState.ACTIVE)).hasSize(1);
+
+ PdpGroupFilter filter = PdpGroupFilter.builder()
+ .policyTypeList(
+ Collections.singletonList(new ToscaConceptIdentifier("onap.policies.native.Xacml", "1.0.0")))
+ .groupState(PdpState.ACTIVE).build();
+ List<PdpGroup> filteredGroups = pdpGroupService.getFilteredPdpGroups(filter);
+ assertThat(filteredGroups).hasSize(1);
+ assertThat(filteredGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
+
+ pdpGroupService.deletePdpGroup(CREATE_GROUPS);
+ pdpGroups = pdpGroupService.getPdpGroups();
+ assertThat(pdpGroups).hasSize(1);
+ assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
+ }
+
+ @Test
+ public void testPdpGroupsCrudFailure() {
+ assertThatThrownBy(() -> pdpGroupService.getPdpGroupByState(null))
+ .hasMessage(String.format(FIELD_IS_NULL, "pdpState"));
+ pdpGroupService.savePdpGroups(groupsToCreate.getGroups());
+ assertThatThrownBy(() -> pdpGroupService.deletePdpGroup("invalid-group"))
+ .hasMessage("delete of PDP group \"invalid-group\" failed, PDP group does not exist");
+ assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
+
+ assertThatThrownBy(() -> pdpGroupService.savePdpGroups(null))
+ .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
+
+ PdpGroup invalidPdpGroup = new PdpGroup(groupsToCreate.getGroups().get(0));
+ invalidPdpGroup.setName("invalidPdpGroup");
+ invalidPdpGroup.setPdpGroupState(null);
+ assertThatThrownBy(() -> pdpGroupService.savePdpGroups(List.of(invalidPdpGroup)))
+ .hasMessageContaining("Failed saving PdpGroup.")
+ .hasMessageContaining("item \"pdpGroupState\" value \"null\" INVALID, is null");
+ pdpGroupService.deletePdpGroup(CREATE_GROUPS);
+ }
+
+ @Test
+ public void testUpdatePdp() {
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdp(null, null, new Pdp());
+ }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdp(NAME, null, new Pdp());
+ }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdp(NAME, TYPE, null);
+ }).hasMessage(String.format(FIELD_IS_NULL, "pdp"));
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdp(NAME, TYPE, new Pdp());
+ }).hasMessage(LOCALNAME_IS_NULL);
+
+ pdpGroupService.savePdpGroups(groupsToCreate.getGroups());
+ assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
+ PdpGroup pdpGroup = pdpGroupService.getPdpGroupByName(CREATE_GROUPS).get(0);
+ Pdp pdp = pdpGroup.getPdpSubgroups().get(0).getPdpInstances().get(0);
+ assertThat(pdp.getHealthy()).isEqualTo(PdpHealthStatus.HEALTHY);
+
+ // now update and test
+ pdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
+ pdpGroupService.updatePdp(CREATE_GROUPS, "pdpTypeA", pdp);
+ PdpGroup updatGroup = pdpGroupService.getPdpGroupByName(CREATE_GROUPS).get(0);
+ assertThat(updatGroup.getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy())
+ .isEqualTo(PdpHealthStatus.NOT_HEALTHY);
+ pdpGroupService.deletePdpGroup(CREATE_GROUPS);
+ }
+
+ @Test
+ public void testUpdateSubGroup() {
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdpSubGroup(null, null);
+ }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdpSubGroup(NAME, null);
+ }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
+
+ assertThatThrownBy(() -> {
+ pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup());
+ }).hasMessage(LOCALNAME_IS_NULL);
+
+ pdpGroupService.savePdpGroups(groupsToCreate.getGroups());
+ assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
+ PdpGroup pdpGroup = pdpGroupService.getPdpGroupByName(CREATE_GROUPS).get(0);
+ PdpSubGroup pdpSubGroup = pdpGroup.getPdpSubgroups().get(0);
+ assertThat(pdpSubGroup.getDesiredInstanceCount()).isEqualTo(2);
+
+ // now update and test
+ pdpSubGroup.setDesiredInstanceCount(1);
+ pdpGroupService.updatePdpSubGroup(CREATE_GROUPS, pdpSubGroup);
+ PdpGroup updatGroup = pdpGroupService.getPdpGroupByName(CREATE_GROUPS).get(0);
+ assertThat(updatGroup.getPdpSubgroups().get(0).getDesiredInstanceCount()).isEqualTo(1);
+ pdpGroupService.deletePdpGroup(CREATE_GROUPS);
+ }
+}
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java
new file mode 100644
index 00000000..86fd9b04
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java
@@ -0,0 +1,188 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.models.base.Validated;
+import org.onap.policy.models.pdp.concepts.PdpStatistics;
+import org.onap.policy.pap.main.rest.CommonPapRestServer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class PdpStatisticsServiceTest extends CommonPapRestServer {
+
+ private static final String NAME3 = "name3";
+ private static final String NAME1 = "name1";
+ private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
+ private static final String GROUP0 = "group0";
+ private static final String GROUP = "group";
+ private static final String SUBGROUP = "subgroup";
+ private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
+ private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
+ private static final Integer NUMBER_RECORDS = 10;
+
+ @Autowired
+ private PdpStatisticsService pdpStatisticsService;
+
+ private PdpStatistics pdpStatistics1;
+ private PdpStatistics pdpStatistics2;
+ private PdpStatistics pdpStatistics3;
+ private PdpStatistics pdpStatistics4;
+
+ /**
+ * Setup before tests.
+ *
+ * @throws Exception the exception
+ */
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ pdpStatistics1 = generatePdpStatistics(NAME1, TIMESTAMP1, 1L, GROUP, SUBGROUP);
+ pdpStatistics2 = generatePdpStatistics("name2", TIMESTAMP1, 2L, GROUP, SUBGROUP);
+ pdpStatistics3 = generatePdpStatistics(NAME1, TIMESTAMP2, 3L, GROUP, SUBGROUP);
+ pdpStatistics4 = generatePdpStatistics(NAME3, TIMESTAMP2, 4L, GROUP0, SUBGROUP);
+ }
+
+ @Test
+ public void testCreatePdpStatisticsSuccess() {
+ List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
+ List<PdpStatistics> createdPdpStatisticsList = pdpStatisticsService.createPdpStatistics(createList);
+ // these should match AND be in the same order
+ assertThat(createdPdpStatisticsList).isEqualTo(createList);
+ }
+
+ @Test
+ public void testCreatePdpStatisticsFailure() {
+
+ assertThatThrownBy(() -> {
+ pdpStatisticsService.createPdpStatistics(null);
+ }).hasMessageMatching(LIST_IS_NULL);
+
+ PdpStatistics pdpStatisticsErr = new PdpStatistics();
+ pdpStatisticsErr.setPdpInstanceId("NULL");
+ pdpStatisticsErr.setPdpGroupName(GROUP);
+
+ assertThatThrownBy(() -> {
+ pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr));
+ }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
+ .hasMessageContaining(Validated.IS_A_NULL_KEY);
+ }
+
+ @Test
+ public void testFetchDatabaseStatistics() {
+
+ List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
+ pdpStatisticsService.createPdpStatistics(createList);
+ Map<String, Map<String, List<PdpStatistics>>> created =
+ pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, null);
+ assertThat(created).hasSize(2);
+ assertThat(created.get(GROUP0)).hasSize(1);
+ assertThat(created.get(GROUP0).get(SUBGROUP)).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
+ assertThat(created).hasSize(2);
+ assertThat(created.get(GROUP0)).hasSize(1);
+ assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, TIMESTAMP1);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
+ assertThat(created).hasSize(2);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP0, NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP0)).hasSize(1);
+ assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, null, TIMESTAMP1);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
+ assertThat(created).hasSize(1);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP1, TIMESTAMP2);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, null, TIMESTAMP1);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP2, null);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
+
+ created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
+ TIMESTAMP2);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
+
+ created =
+ pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, null, TIMESTAMP1);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP)).hasSize(1);
+ assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(1);
+
+ created =
+ pdpStatisticsService.fetchDatabaseStatistics(GROUP0, SUBGROUP, NAME3, NUMBER_RECORDS, TIMESTAMP2, null);
+ assertThat(created).hasSize(1);
+ assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
+ }
+
+ private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, Long id, String group,
+ String subgroup) {
+ PdpStatistics pdpStatistics11 = new PdpStatistics();
+ pdpStatistics11.setPdpInstanceId(pdpInstanceId);
+ pdpStatistics11.setTimeStamp(date);
+ pdpStatistics11.setGeneratedId(id);
+ pdpStatistics11.setPdpGroupName(group);
+ pdpStatistics11.setPdpSubGroupName(subgroup);
+ pdpStatistics11.setPolicyDeployCount(2);
+ pdpStatistics11.setPolicyDeployFailCount(1);
+ pdpStatistics11.setPolicyDeploySuccessCount(1);
+ pdpStatistics11.setPolicyExecutedCount(2);
+ pdpStatistics11.setPolicyExecutedFailCount(1);
+ pdpStatistics11.setPolicyExecutedSuccessCount(1);
+ pdpStatistics11.setEngineStats(new ArrayList<>());
+
+ return pdpStatistics11;
+ }
+}
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/PolicyAuditServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/PolicyAuditServiceTest.java
new file mode 100644
index 00000000..980cdd1a
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/service/PolicyAuditServiceTest.java
@@ -0,0 +1,120 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertThrows;
+
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.List;
+import org.junit.Test;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.pap.concepts.PolicyAudit;
+import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pap.main.rest.CommonPapRestServer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class PolicyAuditServiceTest extends CommonPapRestServer {
+
+ private static final String FIELD_IS_NULL = "%s is marked .*ull but is null";
+ private static final String GROUP_A = "groupA";
+ private static final String GROUP_B = "groupB";
+ private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
+ private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
+ private static final Integer NUMBER_RECORDS = 10;
+
+ @Autowired
+ private PolicyAuditService policyAuditService;
+
+ @Test
+ public void testCreateAuditRecordsSuccess() {
+ policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_A, MY_POLICY));
+
+ assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
+ }
+
+ @Test
+ public void testCreatePolicyAuditFailure() {
+ List<PolicyAudit> audits = List.of(
+ PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).build());
+
+ assertThrows(PfModelRuntimeException.class, () -> policyAuditService.createAuditRecords(audits));
+ assertThatThrownBy(() -> {
+ policyAuditService.createAuditRecords(audits);
+ }).isInstanceOf(PfModelRuntimeException.class)
+ .hasMessageContaining("\"createAuditRecords\" INVALID, item has status INVALID");
+
+ assertThatThrownBy(() -> {
+ policyAuditService.createAuditRecords(null);
+ }).hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
+ }
+
+ @Test
+ public void testGetAuditRecords() {
+ Instant startDate1 = Instant.now();
+
+ policyAuditService.createAuditRecords(generatePolicyAudits(startDate1, GROUP_A, MY_POLICY));
+
+ assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
+
+ assertThat(policyAuditService.getAuditRecords(1, null, null)).hasSize(1);
+
+ // as the start date is 10 min ahead of first record, shouldn't return any records
+ assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, Instant.now().plusSeconds(600), null)).isEmpty();
+
+ policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_B, MY_POLICY2));
+
+ assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
+ assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
+
+ assertThat(policyAuditService.getAuditRecords(GROUP_A, NUMBER_RECORDS, null, null)).hasSize(2);
+ assertThat(policyAuditService.getAuditRecords(GROUP_B, NUMBER_RECORDS, null, null)).hasSize(2);
+
+ assertThat(policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), MY_POLICY.getVersion(),
+ NUMBER_RECORDS, null, null)).hasSize(2);
+ assertThat(
+ policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), "9.9.9", NUMBER_RECORDS, null, null))
+ .hasSize(0);
+ assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY.getName(), MY_POLICY.getVersion(),
+ NUMBER_RECORDS, null, null)).hasSize(0);
+ assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY2.getName(), MY_POLICY2.getVersion(),
+ NUMBER_RECORDS, null, null)).hasSize(2);
+ assertThat(policyAuditService.getAuditRecords(MY_POLICY2.getName(), MY_POLICY2.getVersion(), NUMBER_RECORDS,
+ null, null)).hasSize(2);
+ assertThat(
+ policyAuditService.getAuditRecords(MY_POLICY.getName(), MY_POLICY.getVersion(), NUMBER_RECORDS, null, null))
+ .hasSize(2);
+
+ }
+
+ private List<PolicyAudit> generatePolicyAudits(Instant date, String group, ToscaConceptIdentifier policy) {
+ PolicyAudit deploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy).auditId(1L)
+ .action(AuditAction.DEPLOYMENT).timestamp(date.truncatedTo(ChronoUnit.SECONDS)).build();
+
+ PolicyAudit undeploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy).auditId(2L)
+ .action(AuditAction.UNDEPLOYMENT).timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS)).build();
+
+ return List.of(deploy, undeploy);
+ }
+}
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/PolicyStatusServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/PolicyStatusServiceTest.java
new file mode 100644
index 00000000..2a354036
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/service/PolicyStatusServiceTest.java
@@ -0,0 +1,155 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
+import org.onap.policy.pap.main.rest.CommonPapRestServer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class PolicyStatusServiceTest extends CommonPapRestServer {
+
+ private static final String GROUP_A = "groupA";
+ private static final String GROUP_B = "groupB";
+ private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
+ private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
+
+ @Autowired
+ private PolicyStatusService policyStatusService;
+
+ private PdpPolicyStatus.PdpPolicyStatusBuilder statusBuilder;
+
+ /**
+ * Setup before tests.
+ *
+ * @throws Exception the exception
+ */
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
+
+ statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY)
+ .policyType(policyType).state(PdpPolicyStatus.State.SUCCESS);
+ }
+
+ @Test
+ public void testGetAllPolicyStatus() {
+ assertThat(policyStatusService.getAllPolicyStatus()).isEmpty();
+
+ var statusList = createStatusList();
+ policyStatusService.cudPolicyStatus(statusList, null, null);
+ assertThat(policyStatusService.getAllPolicyStatus()).hasSize(5);
+ policyStatusService.cudPolicyStatus(null, null, statusList);
+ }
+
+ @Test
+ public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() {
+
+ assertThatThrownBy(() -> {
+ policyStatusService.getAllPolicyStatus(null);
+ }).hasMessageContaining("policy").hasMessageContaining("null");
+
+ assertThat(policyStatusService.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion("somePdp", null)))
+ .isEmpty();
+
+ var statusList = createStatusList();
+ policyStatusService.cudPolicyStatus(statusList, null, null);
+
+ assertThat(policyStatusService.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2);
+ assertThat(
+ policyStatusService.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null)))
+ .hasSize(3);
+ policyStatusService.cudPolicyStatus(null, null, statusList);
+ }
+
+ @Test
+ public void testGetGroupPolicyStatus() {
+
+ assertThatThrownBy(() -> {
+ policyStatusService.getGroupPolicyStatus(null);
+ }).hasMessage("pdpGroup is marked non-null but is null");
+
+ assertThat(policyStatusService.getGroupPolicyStatus("PdpGroup0")).isEmpty();
+
+ var statusList = createStatusList();
+ policyStatusService.cudPolicyStatus(statusList, null, null);
+ assertThat(policyStatusService.getGroupPolicyStatus(GROUP_A)).hasSize(3);
+ policyStatusService.cudPolicyStatus(null, null, statusList);
+ }
+
+ @Test
+ public void testCudPolicyStatus() {
+ assertThatCode(() -> policyStatusService.cudPolicyStatus(null, null, null)).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ policyStatusService.cudPolicyStatus(List.of(new PdpPolicyStatus()), null, null);
+ }).isInstanceOf(PfModelRuntimeException.class);
+ PdpPolicyStatus invalidStatus = statusBuilder.state(PdpPolicyStatus.State.WAITING).deploy(false).pdpGroup(null)
+ .pdpId("pdp1").policy(MY_POLICY).build();
+ assertThatThrownBy(() -> {
+ policyStatusService.cudPolicyStatus(List.of(invalidStatus), null, null);
+ }).isInstanceOf(PfModelRuntimeException.class)
+ .hasMessageContaining("item \"pdpGroup\" value \"null\" INVALID, is null");
+
+ // Test create
+ PdpPolicyStatus status = statusBuilder.state(PdpPolicyStatus.State.WAITING).deploy(false).pdpGroup(GROUP_A)
+ .pdpId("pdp1").policy(MY_POLICY).build();
+ policyStatusService.cudPolicyStatus(List.of(status), null, null);
+ List<PdpPolicyStatus> createdStatusList = policyStatusService.getAllPolicyStatus();
+ assertThat(createdStatusList).hasSize(1);
+ assertThat(createdStatusList.get(0).getState()).isEqualTo(PdpPolicyStatus.State.WAITING);
+
+ // Test update
+ status.setDeploy(true);
+ status.setState(PdpPolicyStatus.State.SUCCESS);
+ policyStatusService.cudPolicyStatus(null, List.of(status), null);
+ createdStatusList = policyStatusService.getAllPolicyStatus();
+ assertThat(createdStatusList).hasSize(1);
+ assertThat(createdStatusList.get(0).getState()).isEqualTo(PdpPolicyStatus.State.SUCCESS);
+
+ // Test delete
+ policyStatusService.cudPolicyStatus(null, null, List.of(status));
+ assertThat(policyStatusService.getAllPolicyStatus()).hasSize(0);
+ }
+
+ private List<PdpPolicyStatus> createStatusList() {
+ // 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();
+ return List.of(id1, id2, id3, id4, id5);
+ }
+}
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
new file mode 100644
index 00000000..04c1a281
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
@@ -0,0 +1,107 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Optional;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ToscaServiceTemplateServiceTest {
+
+ private static final String VERSION_1 = "1.0.0";
+
+ private static final String VERSION = "version";
+
+ private static final String NAME = "name";
+
+ private static final String INVALID_VERSION_ERR_MSG =
+ "parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
+
+ @Mock
+ private ToscaServiceTemplateRepository toscaRepository;
+
+ @InjectMocks
+ private ToscaServiceTemplateService toscaService;
+
+ private ToscaServiceTemplate serviceTemplate;
+
+ private StandardCoder coder = new StandardYamlCoder();
+
+ /**
+ * Set up for tests.
+ *
+ * @throws CoderException the exception
+ */
+ @Before
+ public void setup() throws CoderException {
+ ToscaServiceTemplate toscaPolicyType = coder
+ .decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"), ToscaServiceTemplate.class);
+ ToscaServiceTemplate toscaPolicy =
+ coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"), ToscaServiceTemplate.class);
+ serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
+ serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
+ Mockito
+ .when(toscaRepository.findById(
+ new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
+ .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
+ }
+
+
+ @Test
+ public void testGetPolicyList() throws PfModelException {
+ assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
+ .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
+
+ assertThat(toscaService.getPolicyList(NAME, VERSION_1)).hasSize(0);
+
+ assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
+ }
+
+ @Test
+ public void testGetPolicyTypeList() throws PfModelException {
+ assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
+ .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
+
+ assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).hasSize(0);
+
+ assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
+ assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);
+ }
+}