2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================;
22 package org.onap.policy.api.main.service;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.mockito.Mockito.when;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.api.main.repository.PdpGroupRepository;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.pdp.concepts.PdpGroups;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 class TestPdpGroupService {
46 private PdpGroupRepository pdpGroupRepository;
49 private PdpGroupService pdpGroupService;
51 AutoCloseable closeable;
56 * @throws CoderException decode errors
59 public void setUp() throws CoderException {
60 closeable = MockitoAnnotations.openMocks(this);
61 var pdpGroups = new StandardCoder().decode(ResourceUtils.getResourceAsString("pdpgroups/PdpGroups.json"),
62 PdpGroups.class).getGroups();
63 List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
64 pdpGroups.forEach(pdpGroup -> jpaPdpGroupList.add(new JpaPdpGroup(pdpGroup)));
66 when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
70 void tearDown() throws Exception {
75 void testAssertPolicyTypeNotSupportedInPdpGroup() {
76 assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
77 "1.0.0")).doesNotThrowAnyException();
79 assertThatThrownBy(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup(
80 "onap.policies.controlloop.guard.common.FrequencyLimiter", "1.0.0"))
81 .hasMessage("policy type is in use, it is referenced in PDP group defaultGroup subgroup xacml");
85 void testAssertPolicyNotDeployedInPdpGroup() {
86 assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
87 .doesNotThrowAnyException();
89 assertThatThrownBy(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup(
90 "onap.policies.controlloop.operational.common.apex.SampleDomain", "1.0.0"))
91 .hasMessage("policy is in use, it is deployed in PDP group defaultGroup subgroup apex");