b64ad287878673d9e1a052fca07f5be195b63040
[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 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 java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import javax.ws.rs.core.Response;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.junit.MockitoJUnitRunner;
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 @RunWith(MockitoJUnitRunner.class)
52 public class ToscaServiceTemplateServiceTest {
53
54     private static final String VERSION_1 = "1.0.0";
55
56     private static final String VERSION = "version";
57
58     private static final String NAME = "name";
59
60     private static final String INVALID_VERSION_ERR_MSG =
61         "parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
62
63     private static final String NODE_TEMPLATE_NAME = "tca_metadata";
64     private static final String NODE_TEMPLATE_VERSION = "1.0.0";
65
66     @Mock
67     private ToscaServiceTemplateRepository toscaRepository;
68
69     @InjectMocks
70     private ToscaServiceTemplateService toscaService;
71
72     @Mock
73     private ToscaNodeTemplateService nodeTemplateService;
74
75     private ToscaServiceTemplate serviceTemplate;
76
77     private ToscaNodeTemplate nodeTemplate;
78
79     private StandardCoder coder = new StandardYamlCoder();
80
81     /**
82      * Set up for tests.
83      *
84      * @throws CoderException the exception
85      */
86     @Before
87     public void setup() throws CoderException {
88
89         coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
90             ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
91             .forEach((key, value) -> nodeTemplate = value);
92
93         ToscaServiceTemplate toscaPolicyType =
94             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"),
95                 ToscaServiceTemplate.class);
96         ToscaServiceTemplate toscaPolicy =
97             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"),
98                 ToscaServiceTemplate.class);
99         // Add metadataSet reference to the policy metadata
100         toscaPolicy.getToscaTopologyTemplate().getPolicies().forEach(e -> e.entrySet().iterator().next().getValue()
101             .getMetadata().putAll(Map.of("metadataSetName", NODE_TEMPLATE_NAME,
102                 "metadataSetVersion", NODE_TEMPLATE_VERSION)));
103         serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
104         serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
105         Mockito
106             .when(toscaRepository.findById(
107                 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
108             .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
109
110         Mockito
111             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
112             .thenReturn(nodeTemplate);
113     }
114
115
116     @Test
117     public void testGetPolicyList() throws PfModelException {
118         assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
119             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
120
121         assertThat(toscaService.getPolicyList(NAME, VERSION_1)).isEmpty();
122
123         assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
124     }
125
126     @Test
127     public void testPolicyForMetadataSet() throws PfModelException {
128         List<ToscaPolicy> policies = toscaService.getPolicyList("onap.restart.tca", VERSION_1);
129
130         assertThat(policies.get(0).getMetadata()).containsEntry("metadataSet", nodeTemplate.getMetadata());
131
132         Mockito
133             .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
134             .thenThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
135                 "node template for onap.restart.tca:1.0.0 do not exist in the database"));
136
137         assertThatThrownBy(() -> toscaService.getPolicyList("onap.restart.tca", VERSION_1))
138             .isInstanceOf(PfModelRuntimeException.class)
139             .hasMessage("node template for onap.restart.tca:1.0.0 do not exist in the database");
140     }
141
142     @Test
143     public void testGetPolicyTypeList() throws PfModelException {
144         assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
145             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
146
147         assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).isEmpty();
148
149         assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
150         assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);
151     }
152 }