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.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.policy.api.main.repository.PdpGroupRepository;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.pdp.concepts.PdpGroups;
43 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
45 @RunWith(MockitoJUnitRunner.class)
46 class TestPdpGroupService {
49 private PdpGroupRepository pdpGroupRepository;
52 private PdpGroupService pdpGroupService;
54 AutoCloseable closeable;
59 * @throws CoderException decode errors
62 public void setUp() throws CoderException {
63 closeable = MockitoAnnotations.openMocks(this);
64 var pdpGroups = new StandardCoder().decode(ResourceUtils.getResourceAsString("pdpgroups/PdpGroups.json"),
65 PdpGroups.class).getGroups();
66 List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
67 pdpGroups.forEach(pdpGroup -> jpaPdpGroupList.add(new JpaPdpGroup(pdpGroup)));
69 when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
73 void tearDown() throws Exception {
78 void testAssertPolicyTypeNotSupportedInPdpGroup() {
79 assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
80 "1.0.0")).doesNotThrowAnyException();
82 assertThatThrownBy(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup(
83 "onap.policies.controlloop.guard.common.FrequencyLimiter", "1.0.0"))
84 .hasMessage("policy type is in use, it is referenced in PDP group defaultGroup subgroup xacml");
88 void testAssertPolicyNotDeployedInPdpGroup() {
89 assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
90 .doesNotThrowAnyException();
92 assertThatThrownBy(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup(
93 "onap.policies.controlloop.operational.common.apex.SampleDomain", "1.0.0"))
94 .hasMessage("policy is in use, it is deployed in PDP group defaultGroup subgroup apex");