Java 17 / Spring 6 / Spring Boot 3 Upgrade
[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  *  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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================;
20  */
21
22 package org.onap.policy.api.main.service;
23
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;
27
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 class TestPdpGroupService {
47
48     @Mock
49     private PdpGroupRepository pdpGroupRepository;
50
51     @InjectMocks
52     private PdpGroupService pdpGroupService;
53
54     AutoCloseable closeable;
55
56     /**
57      * Test setup.
58      *
59      * @throws CoderException decode errors
60      */
61     @BeforeEach
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)));
68
69         when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
70     }
71
72     @AfterEach
73     void tearDown() throws Exception {
74         closeable.close();
75     }
76
77     @Test
78     void testAssertPolicyTypeNotSupportedInPdpGroup() {
79         assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
80             "1.0.0")).doesNotThrowAnyException();
81
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");
85     }
86
87     @Test
88     void testAssertPolicyNotDeployedInPdpGroup() {
89         assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
90             .doesNotThrowAnyException();
91
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");
95     }
96 }