Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / service / ToscaNodeTemplateServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.service;
22
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;
26
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;
45
46 class ToscaNodeTemplateServiceTest {
47
48     private static final String NODE_TEMPLATE_NAME = "tca_metadata";
49     private static final String NODE_TEMPLATE_VERSION = "1.0.0";
50
51     @Mock
52     private ToscaNodeTemplateRepository nodeTemplateRepository;
53
54     @InjectMocks
55     private ToscaNodeTemplateService nodeTemplateService;
56
57     private ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
58
59     private final StandardCoder coder = new StandardYamlCoder();
60
61     AutoCloseable autoCloseable;
62
63     /**
64      * Set up for tests.
65      */
66     @BeforeEach
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);
72
73         Mockito.when(nodeTemplateRepository.findById(new PfConceptKey(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION)))
74             .thenReturn(Optional.of(new JpaToscaNodeTemplate(nodeTemplate)));
75     }
76
77     @AfterEach
78     public void tearDown() throws Exception {
79         autoCloseable.close();
80     }
81
82     @Test
83     void testGetToscaNodeTemplate() {
84         assertDoesNotThrow(() -> nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION));
85
86         assertThat(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION).getMetadata())
87             .containsEntry("policyModel", nodeTemplate.getMetadata().get("policyModel"));
88
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");
92
93     }
94
95 }