Allow semantic versioning in all templates in pap
[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.PfKey;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
50 import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
51
52 class ToscaServiceTemplateServiceTest {
53
54     private static final String VERSION_1 = "1.0.0";
55
56     private static final String VERSION_2 = "1.0.0+456";
57     private static final String VERSION_3 = "1.0.0-46";
58     private static final String VERSION = "version";
59
60     private static final String NAME = "name";
61
62     private static final String INVALID_VERSION_ERR_MSG =
63         "parameter \"version\": value \"version\", does not match regular expression \"" + PfKey.VERSION_REGEXP + "\"";
64
65     private static final String NODE_TEMPLATE_NAME = "tca_metadata";
66     private static final String NODE_TEMPLATE_VERSION = "1.0.0";
67
68     @Mock
69     private ToscaServiceTemplateRepository toscaRepository;
70
71     @InjectMocks
72     private ToscaServiceTemplateService toscaService;
73
74     @Mock
75     private ToscaNodeTemplateService nodeTemplateService;
76
77     private ToscaNodeTemplate nodeTemplate;
78
79     private final StandardCoder coder = new StandardYamlCoder();
80
81     AutoCloseable autoCloseable;
82
83     /**
84      * Set up for tests.
85      *
86      * @throws CoderException the exception
87      */
88     @BeforeEach
89     public void setup() throws CoderException {
90         autoCloseable = MockitoAnnotations.openMocks(this);
91         coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
92                 ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
93             .forEach((key, value) -> nodeTemplate = value);
94
95         ToscaServiceTemplate toscaPolicyType =
96             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"),
97                 ToscaServiceTemplate.class);
98         ToscaServiceTemplate toscaPolicy =
99             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"),
100                 ToscaServiceTemplate.class);
101         // Add metadataSet reference to the policy metadata
102         toscaPolicy.getToscaTopologyTemplate().getPolicies().forEach(e -> e.entrySet().iterator().next().getValue()
103             .getMetadata().putAll(Map.of("metadataSetName", NODE_TEMPLATE_NAME,
104                 "metadataSetVersion", NODE_TEMPLATE_VERSION)));
105         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
106         serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
107         Mockito
108             .when(toscaRepository.findById(
109                 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
110             .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
111
112         Mockito
113             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
114             .thenReturn(nodeTemplate);
115     }
116
117     @AfterEach
118     public void tearDown() throws Exception {
119         autoCloseable.close();
120     }
121
122     @Test
123     void testGetPolicyList() throws PfModelException {
124         assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
125             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
126
127         assertThat(toscaService.getPolicyList(NAME, VERSION_1)).isEmpty();
128
129         assertThat(toscaService.getPolicyList(NAME, VERSION_2)).isEmpty();
130
131         assertThat(toscaService.getPolicyList(NAME, VERSION_3)).isEmpty();
132
133         assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
134     }
135
136     @Test
137     void testPolicyForMetadataSet() throws PfModelException {
138         List<ToscaPolicy> policies = toscaService.getPolicyList("onap.restart.tca", VERSION_1);
139
140         assertThat(policies.get(0).getMetadata()).containsEntry("metadataSet", nodeTemplate.getMetadata());
141
142         Mockito
143             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
144             .thenThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
145                 "node template for onap.restart.tca:1.0.0 do not exist in the database"));
146
147         assertThatThrownBy(() -> toscaService.getPolicyList("onap.restart.tca", VERSION_1))
148             .isInstanceOf(PfModelRuntimeException.class)
149             .hasMessage("node template for onap.restart.tca:1.0.0 do not exist in the database");
150     }
151
152     @Test
153     void testGetPolicyTypeList() throws PfModelException {
154         assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
155             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
156
157         assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).isEmpty();
158
159         assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
160         assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);
161     }
162 }