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