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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27 import java.util.Optional;
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.coder.StandardYamlCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
44 import org.onap.policy.pap.main.repository.ToscaNodeTemplateRepository;
46 class ToscaNodeTemplateServiceTest {
48 private static final String NODE_TEMPLATE_NAME = "tca_metadata";
49 private static final String NODE_TEMPLATE_VERSION = "1.0.0";
52 private ToscaNodeTemplateRepository nodeTemplateRepository;
55 private ToscaNodeTemplateService nodeTemplateService;
57 private ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
59 private final StandardCoder coder = new StandardYamlCoder();
61 AutoCloseable autoCloseable;
67 public void setup() throws CoderException {
68 autoCloseable = MockitoAnnotations.openMocks(this);
69 coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
70 ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
71 .forEach((key, value) -> nodeTemplate = value);
73 Mockito.when(nodeTemplateRepository.findById(new PfConceptKey(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION)))
74 .thenReturn(Optional.of(new JpaToscaNodeTemplate(nodeTemplate)));
78 public void tearDown() throws Exception {
79 autoCloseable.close();
83 void testGetToscaNodeTemplate() {
84 assertDoesNotThrow(() -> nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION));
86 assertThat(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION).getMetadata())
87 .containsEntry("policyModel", nodeTemplate.getMetadata().get("policyModel"));
89 assertThatThrownBy(() -> nodeTemplateService.getToscaNodeTemplate("invalid_name", "1.0.0"))
90 .isInstanceOf(PfModelRuntimeException.class)
91 .hasMessage("node template for invalid_name:1.0.0 do not exist in the database");