Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / service / TestCommonToscaServiceTemplateService.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 java.util.Optional;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.onap.policy.api.main.repository.ToscaServiceTemplateRepository;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
34
35 /**
36  * This class offers common mock utility methods for uni testing {@link ToscaServiceTemplateService}.
37  */
38 public class TestCommonToscaServiceTemplateService {
39
40     protected enum Operation {
41         CREATE_POLICY_TYPE,
42         DELETE_POLICY_TYPE,
43         CREATE_POLICY,
44         DELETE_POLICY;
45     }
46
47     @Mock
48     protected ToscaServiceTemplateRepository toscaServiceTemplateRepository;
49     @Mock
50     protected PolicyTypeService policyTypeService;
51     @Mock
52     protected PolicyService policyService;
53     @Mock
54     protected NodeTemplateService nodeTemplateService;
55
56     AutoCloseable autoCloseable;
57
58     /**
59      * Set up the DB TOSCA service template object post create, and delete request.
60      *
61      * @param dbSvcTemplate       ToscaServiceTemplate object to update
62      * @param svcTemplateFragment the CRUD operation response ToscaServiceTemplate object
63      * @param operation           the CRUD operation performed
64      */
65     protected void mockDbServiceTemplate(ToscaServiceTemplate dbSvcTemplate, ToscaServiceTemplate svcTemplateFragment,
66                                          TestToscaServiceTemplateServiceForPolicyCrud.Operation operation) {
67         if (operation != null) {
68             switch (operation) {
69                 case CREATE_POLICY_TYPE -> {
70                     dbSvcTemplate.getPolicyTypes().putAll(svcTemplateFragment.getPolicyTypes());
71                     if (svcTemplateFragment.getDataTypes() != null) {
72                         if (dbSvcTemplate.getDataTypes() == null) {
73                             dbSvcTemplate.setDataTypes(svcTemplateFragment.getDataTypes());
74                         } else {
75                             dbSvcTemplate.getDataTypes().putAll(svcTemplateFragment.getDataTypes());
76                         }
77                     }
78                 }
79                 case DELETE_POLICY_TYPE ->
80                     dbSvcTemplate.getPolicyTypes().keySet().removeAll(svcTemplateFragment.getPolicyTypes().keySet());
81                 case CREATE_POLICY ->
82                     dbSvcTemplate.setToscaTopologyTemplate(svcTemplateFragment.getToscaTopologyTemplate());
83                 case DELETE_POLICY -> dbSvcTemplate.getToscaTopologyTemplate().setPolicies(null);
84                 default -> {
85                 }
86             }
87         }
88         Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
89                 JpaToscaServiceTemplate.DEFAULT_VERSION)))
90             .thenReturn(Optional.of(new JpaToscaServiceTemplate(dbSvcTemplate)));
91     }
92
93     /**
94      * Setup to return empty DB service template.
95      */
96     @BeforeEach
97     public void setUp() {
98         autoCloseable = MockitoAnnotations.openMocks(this);
99         Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
100             JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.of(new JpaToscaServiceTemplate()));
101     }
102
103     @AfterEach
104     public void tearDown() throws Exception {
105         autoCloseable.close();
106     }
107 }