Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / service / ToscaServiceTemplateServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada. All rights reserved.
4  *  Modifications Copyright (C) 2022-2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
27 import jakarta.ws.rs.core.Response;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.coder.StandardYamlCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfModelException;
44 import org.onap.policy.models.base.PfModelRuntimeException;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
49 import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
50
51 class ToscaServiceTemplateServiceTest {
52
53     private static final String VERSION_1 = "1.0.0";
54
55     private static final String VERSION = "version";
56
57     private static final String NAME = "name";
58
59     private static final String INVALID_VERSION_ERR_MSG =
60         "parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
61
62     private static final String NODE_TEMPLATE_NAME = "tca_metadata";
63     private static final String NODE_TEMPLATE_VERSION = "1.0.0";
64
65     @Mock
66     private ToscaServiceTemplateRepository toscaRepository;
67
68     @InjectMocks
69     private ToscaServiceTemplateService toscaService;
70
71     @Mock
72     private ToscaNodeTemplateService nodeTemplateService;
73
74     private ToscaNodeTemplate nodeTemplate;
75
76     private final StandardCoder coder = new StandardYamlCoder();
77
78     AutoCloseable autoCloseable;
79
80     /**
81      * Set up for tests.
82      *
83      * @throws CoderException the exception
84      */
85     @BeforeEach
86     public void setup() throws CoderException {
87         autoCloseable = MockitoAnnotations.openMocks(this);
88         coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
89                 ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
90             .forEach((key, value) -> nodeTemplate = value);
91
92         ToscaServiceTemplate toscaPolicyType =
93             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"),
94                 ToscaServiceTemplate.class);
95         ToscaServiceTemplate toscaPolicy =
96             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"),
97                 ToscaServiceTemplate.class);
98         // Add metadataSet reference to the policy metadata
99         toscaPolicy.getToscaTopologyTemplate().getPolicies().forEach(e -> e.entrySet().iterator().next().getValue()
100             .getMetadata().putAll(Map.of("metadataSetName", NODE_TEMPLATE_NAME,
101                 "metadataSetVersion", NODE_TEMPLATE_VERSION)));
102         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
103         serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
104         Mockito
105             .when(toscaRepository.findById(
106                 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
107             .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
108
109         Mockito
110             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
111             .thenReturn(nodeTemplate);
112     }
113
114     @AfterEach
115     public void tearDown() throws Exception {
116         autoCloseable.close();
117     }
118
119     @Test
120     void testGetPolicyList() throws PfModelException {
121         assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
122             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
123
124         assertThat(toscaService.getPolicyList(NAME, VERSION_1)).isEmpty();
125
126         assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
127     }
128
129     @Test
130     void testPolicyForMetadataSet() throws PfModelException {
131         List<ToscaPolicy> policies = toscaService.getPolicyList("onap.restart.tca", VERSION_1);
132
133         assertThat(policies.get(0).getMetadata()).containsEntry("metadataSet", nodeTemplate.getMetadata());
134
135         Mockito
136             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
137             .thenThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
138                 "node template for onap.restart.tca:1.0.0 do not exist in the database"));
139
140         assertThatThrownBy(() -> toscaService.getPolicyList("onap.restart.tca", VERSION_1))
141             .isInstanceOf(PfModelRuntimeException.class)
142             .hasMessage("node template for onap.restart.tca:1.0.0 do not exist in the database");
143     }
144
145     @Test
146     void testGetPolicyTypeList() throws PfModelException {
147         assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
148             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
149
150         assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).isEmpty();
151
152         assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
153         assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);
154     }
155 }