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.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.policy.api.main.repository.NodeTemplateRepository;
40 import org.onap.policy.api.main.repository.NodeTypeRepository;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
48 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType;
49 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
51 @RunWith(MockitoJUnitRunner.class)
52 class TestNodeTemplateService {
55 private NodeTemplateRepository nodeTemplateRepository;
58 private NodeTypeRepository nodeTypeRepository;
61 private NodeTemplateService nodeTemplateService;
63 private static final String POLICY_WITH_METADATA_SET_REF = "nodetemplates/dummy.apex.decisionmaker.policy.yaml";
64 private static final String UPDATED_NODE_TEMPLATE_JSON = "nodetemplates/nodetemplates.metadatasets.update.json";
66 private static ToscaServiceTemplate updatedToscaServiceTemplate;
67 private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
68 ToscaServiceTemplate policyServiceTemplate;
70 AutoCloseable closeable;
75 * @throws CoderException if error in json parsing
78 public void setUp() throws CoderException {
79 closeable = MockitoAnnotations.openMocks(this);
80 StandardCoder standardCoder = new StandardCoder();
81 policyServiceTemplate =
82 yamlJsonTranslator.fromYaml(ResourceUtils.getResourceAsString(POLICY_WITH_METADATA_SET_REF),
83 ToscaServiceTemplate.class);
84 updatedToscaServiceTemplate =
85 standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_NODE_TEMPLATE_JSON),
86 ToscaServiceTemplate.class);
90 void tearDown() throws Exception {
95 void testVerifyNodeType() {
96 assertThatThrownBy(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(new JpaToscaNodeTemplate()))
97 .hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
99 JpaToscaNodeTemplate jpaToscaNodeTemplate = new JpaToscaNodeTemplate();
100 PfConceptKey nodeType = new PfConceptKey("dummyType", "1.0.0");
101 jpaToscaNodeTemplate.setType(nodeType);
102 jpaToscaNodeTemplate.setKey(new PfConceptKey("dummyName", "1.0.0"));
103 Mockito.when(nodeTypeRepository.findById(nodeType)).thenReturn(Optional.of(new JpaToscaNodeType()));
104 assertDoesNotThrow(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(jpaToscaNodeTemplate));
108 void testNodeTemplateUsedInPolicy() {
109 assertDoesNotThrow(() -> nodeTemplateService.assertNodeTemplateNotUsedInPolicy("dummyName", "1.0.0",
110 new JpaToscaServiceTemplate(policyServiceTemplate)));
112 assertThatThrownBy(() -> nodeTemplateService
113 .assertNodeTemplateNotUsedInPolicy("apexMetadata_decisionMaker", "1.0.0",
114 new JpaToscaServiceTemplate(policyServiceTemplate)))
115 .hasMessage("Node template is in use, it is referenced in Tosca Policy operational.apex.decisionMaker "
120 void testNodeTemplateUpdate() {
122 Mockito.when(nodeTypeRepository.findById(Mockito.any())).thenReturn(Optional.of(new JpaToscaNodeType()));
123 Mockito.when(nodeTemplateRepository.findById(Mockito.any())).thenReturn(Optional.of(
124 new JpaToscaNodeTemplate()));
125 assertDoesNotThrow(() -> nodeTemplateService.updateToscaNodeTemplates(
126 new JpaToscaServiceTemplate(updatedToscaServiceTemplate)));