Fix build failure in pap
[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, 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.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;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class ToscaNodeTemplateServiceTest {
48
49     private static final String NODE_TEMPLATE_NAME = "tca_metadata";
50     private static final String NODE_TEMPLATE_VERSION = "1.0.0";
51
52     @Mock
53     private ToscaNodeTemplateRepository nodeTemplateRepository;
54
55     @InjectMocks
56     private ToscaNodeTemplateService nodeTemplateService;
57
58     private ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
59
60     private StandardCoder coder = new StandardYamlCoder();
61
62     /**
63      * Set up for tests.
64      *
65      */
66     @Before
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);
71
72         Mockito.when(nodeTemplateRepository.findById(new PfConceptKey(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION)))
73             .thenReturn(Optional.of(new JpaToscaNodeTemplate(nodeTemplate)));
74     }
75
76     @Test
77     public void testGetToscaNodeTemplate() {
78         assertDoesNotThrow(() -> nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION));
79
80         assertThat(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION).getMetadata())
81             .containsEntry("policyModel", nodeTemplate.getMetadata().get("policyModel"));
82
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");
86
87     }
88
89 }