Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / service / TestNodeTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2022-2023 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.service;
24
25
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28
29 import java.util.Optional;
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.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.api.main.repository.NodeTemplateRepository;
38 import org.onap.policy.api.main.repository.NodeTypeRepository;
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.coder.YamlJsonTranslator;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
46 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType;
47 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
48
49 class TestNodeTemplateService {
50
51     @Mock
52     private NodeTemplateRepository nodeTemplateRepository;
53
54     @Mock
55     private NodeTypeRepository nodeTypeRepository;
56
57     @InjectMocks
58     private NodeTemplateService nodeTemplateService;
59
60     private static final String POLICY_WITH_METADATA_SET_REF = "nodetemplates/dummy.apex.decisionmaker.policy.yaml";
61     private static final String UPDATED_NODE_TEMPLATE_JSON = "nodetemplates/nodetemplates.metadatasets.update.json";
62
63     private static ToscaServiceTemplate updatedToscaServiceTemplate;
64     private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
65     ToscaServiceTemplate policyServiceTemplate;
66
67     AutoCloseable closeable;
68
69     /**
70      * Set up for tests.
71      *
72      * @throws CoderException if error in json parsing
73      */
74     @BeforeEach
75     public void setUp() throws CoderException {
76         closeable = MockitoAnnotations.openMocks(this);
77         StandardCoder standardCoder = new StandardCoder();
78         policyServiceTemplate =
79             yamlJsonTranslator.fromYaml(ResourceUtils.getResourceAsString(POLICY_WITH_METADATA_SET_REF),
80                 ToscaServiceTemplate.class);
81         updatedToscaServiceTemplate =
82             standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_NODE_TEMPLATE_JSON),
83                 ToscaServiceTemplate.class);
84     }
85
86     @AfterEach
87     void tearDown() throws Exception {
88         closeable.close();
89     }
90
91     @Test
92     void testVerifyNodeType() {
93         assertThatThrownBy(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(new JpaToscaNodeTemplate()))
94             .hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
95
96         JpaToscaNodeTemplate jpaToscaNodeTemplate = new JpaToscaNodeTemplate();
97         PfConceptKey nodeType = new PfConceptKey("dummyType", "1.0.0");
98         jpaToscaNodeTemplate.setType(nodeType);
99         jpaToscaNodeTemplate.setKey(new PfConceptKey("dummyName", "1.0.0"));
100         Mockito.when(nodeTypeRepository.findById(nodeType)).thenReturn(Optional.of(new JpaToscaNodeType()));
101         assertDoesNotThrow(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(jpaToscaNodeTemplate));
102     }
103
104     @Test
105     void testNodeTemplateUsedInPolicy() {
106         assertDoesNotThrow(() -> nodeTemplateService.assertNodeTemplateNotUsedInPolicy("dummyName", "1.0.0",
107             new JpaToscaServiceTemplate(policyServiceTemplate)));
108
109         assertThatThrownBy(() -> nodeTemplateService
110             .assertNodeTemplateNotUsedInPolicy("apexMetadata_decisionMaker", "1.0.0",
111                 new JpaToscaServiceTemplate(policyServiceTemplate)))
112             .hasMessage("Node template is in use, it is referenced in Tosca Policy operational.apex.decisionMaker "
113                 + "version 1.0.0");
114     }
115
116     @Test
117     void testNodeTemplateUpdate() {
118
119         Mockito.when(nodeTypeRepository.findById(Mockito.any())).thenReturn(Optional.of(new JpaToscaNodeType()));
120         Mockito.when(nodeTemplateRepository.findById(Mockito.any())).thenReturn(Optional.of(
121             new JpaToscaNodeTemplate()));
122         assertDoesNotThrow(() -> nodeTemplateService.updateToscaNodeTemplates(
123             new JpaToscaServiceTemplate(updatedToscaServiceTemplate)));
124     }
125 }