4c04a08e7e2a49256011edc56aa8fd6813e2eed3
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / service / TestPdpGroupService.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *   Copyright (C) 2022 Bell Canada. All rights reserved.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================;
19  */
20
21 package org.onap.policy.api.main.service;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.policy.api.main.repository.PdpGroupRepository;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.pdp.concepts.PdpGroups;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class TestPdpGroupService {
44
45     @Mock
46     private PdpGroupRepository pdpGroupRepository;
47
48     @InjectMocks
49     private PdpGroupService pdpGroupService;
50
51     /**
52      * Test setup.
53      * @throws CoderException decode errors
54      */
55     @Before
56     public void setUp() throws CoderException {
57         var pdpGroups = new StandardCoder().decode(ResourceUtils.getResourceAsString("pdpgroups/PdpGroups.json"),
58             PdpGroups.class).getGroups();
59         List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
60         pdpGroups.forEach(pdpGroup -> jpaPdpGroupList.add(new JpaPdpGroup(pdpGroup)));
61
62         when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
63     }
64
65     @Test
66     public void testAssertPolicyTypeNotSupportedInPdpGroup() {
67         assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
68             "1.0.0")).doesNotThrowAnyException();
69
70         assertThatThrownBy(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup(
71             "onap.policies.controlloop.guard.common.FrequencyLimiter", "1.0.0"))
72             .hasMessage("policy type is in use, it is referenced in PDP group defaultGroup subgroup xacml");
73     }
74
75     @Test
76     public void testAssertPolicyNotDeployedInPdpGroup() {
77         assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
78             .doesNotThrowAnyException();
79
80         assertThatThrownBy(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup(
81             "onap.policies.controlloop.operational.common.apex.SampleDomain", "1.0.0"))
82             .hasMessage("policy is in use, it is deployed in PDP group defaultGroup subgroup apex");
83     }
84 }