2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.service;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
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;
49 class TestNodeTemplateService {
52 private NodeTemplateRepository nodeTemplateRepository;
55 private NodeTypeRepository nodeTypeRepository;
58 private NodeTemplateService nodeTemplateService;
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";
63 private static ToscaServiceTemplate updatedToscaServiceTemplate;
64 private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
65 ToscaServiceTemplate policyServiceTemplate;
67 AutoCloseable closeable;
72 * @throws CoderException if error in json parsing
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);
87 void tearDown() throws Exception {
92 void testVerifyNodeType() {
93 assertThatThrownBy(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(new JpaToscaNodeTemplate()))
94 .hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
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));
105 void testNodeTemplateUsedInPolicy() {
106 assertDoesNotThrow(() -> nodeTemplateService.assertNodeTemplateNotUsedInPolicy("dummyName", "1.0.0",
107 new JpaToscaServiceTemplate(policyServiceTemplate)));
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 "
117 void testNodeTemplateUpdate() {
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)));