2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022, 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.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.junit.MockitoJUnitRunner;
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 @RunWith(MockitoJUnitRunner.class)
47 public class ToscaNodeTemplateServiceTest {
49 private static final String NODE_TEMPLATE_NAME = "tca_metadata";
50 private static final String NODE_TEMPLATE_VERSION = "1.0.0";
53 private ToscaNodeTemplateRepository nodeTemplateRepository;
56 private ToscaNodeTemplateService nodeTemplateService;
58 private ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
60 private StandardCoder coder = new StandardYamlCoder();
67 public void setup() throws CoderException {
68 coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
69 ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
70 .forEach((key, value) -> nodeTemplate = value);
72 Mockito.when(nodeTemplateRepository.findById(new PfConceptKey(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION)))
73 .thenReturn(Optional.of(new JpaToscaNodeTemplate(nodeTemplate)));
77 public void testGetToscaNodeTemplate() {
78 assertDoesNotThrow(() -> nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION));
80 assertThat(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION).getMetadata())
81 .containsEntry("policyModel", nodeTemplate.getMetadata().get("policyModel"));
83 assertThatThrownBy(() -> nodeTemplateService.getToscaNodeTemplate("invalid_name", "1.0.0"))
84 .isInstanceOf(PfModelRuntimeException.class)
85 .hasMessage("node template for invalid_name:1.0.0 do not exist in the database");