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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================;
22 package org.onap.policy.api.main.service;
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;
36 * This class offers common mock utility methods for uni testing {@link ToscaServiceTemplateService}.
38 public class TestCommonToscaServiceTemplateService {
40 protected enum Operation {
48 protected ToscaServiceTemplateRepository toscaServiceTemplateRepository;
50 protected PolicyTypeService policyTypeService;
52 protected PolicyService policyService;
54 protected NodeTemplateService nodeTemplateService;
56 AutoCloseable autoCloseable;
59 * Set up the DB TOSCA service template object post create, and delete request.
61 * @param dbSvcTemplate ToscaServiceTemplate object to update
62 * @param svcTemplateFragment the CRUD operation response ToscaServiceTemplate object
63 * @param operation the CRUD operation performed
65 protected void mockDbServiceTemplate(ToscaServiceTemplate dbSvcTemplate, ToscaServiceTemplate svcTemplateFragment,
66 TestToscaServiceTemplateServiceForPolicyCrud.Operation operation) {
67 if (operation != null) {
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());
75 dbSvcTemplate.getDataTypes().putAll(svcTemplateFragment.getDataTypes());
79 case DELETE_POLICY_TYPE ->
80 dbSvcTemplate.getPolicyTypes().keySet().removeAll(svcTemplateFragment.getPolicyTypes().keySet());
82 dbSvcTemplate.setToscaTopologyTemplate(svcTemplateFragment.getToscaTopologyTemplate());
83 case DELETE_POLICY -> dbSvcTemplate.getToscaTopologyTemplate().setPolicies(null);
88 Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
89 JpaToscaServiceTemplate.DEFAULT_VERSION)))
90 .thenReturn(Optional.of(new JpaToscaServiceTemplate(dbSvcTemplate)));
94 * Setup to return empty DB service template.
98 autoCloseable = MockitoAnnotations.openMocks(this);
99 Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
100 JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.of(new JpaToscaServiceTemplate()));
104 public void tearDown() throws Exception {
105 autoCloseable.close();