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.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;
42
43 class TestPdpGroupService {
44
45     @Mock
46     private PdpGroupRepository pdpGroupRepository;
47
48     @InjectMocks
49     private PdpGroupService pdpGroupService;
50
51     AutoCloseable closeable;
52
53     /**
54      * Test setup.
55      *
56      * @throws CoderException decode errors
57      */
58     @BeforeEach
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)));
65
66         when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
67     }
68
69     @AfterEach
70     void tearDown() throws Exception {
71         closeable.close();
72     }
73
74     @Test
75     void testAssertPolicyTypeNotSupportedInPdpGroup() {
76         assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
77             "1.0.0")).doesNotThrowAnyException();
78
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");
82     }
83
84     @Test
85     void testAssertPolicyNotDeployedInPdpGroup() {
86         assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
87             .doesNotThrowAnyException();
88
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");
92     }
93 }