04c1a2816a8d9d6047f6b7072c385545a0e82096
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
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
26 import java.util.Optional;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.StandardYamlCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ToscaServiceTemplateServiceTest {
47
48     private static final String VERSION_1 = "1.0.0";
49
50     private static final String VERSION = "version";
51
52     private static final String NAME = "name";
53
54     private static final String INVALID_VERSION_ERR_MSG =
55         "parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
56
57     @Mock
58     private ToscaServiceTemplateRepository toscaRepository;
59
60     @InjectMocks
61     private ToscaServiceTemplateService toscaService;
62
63     private ToscaServiceTemplate serviceTemplate;
64
65     private StandardCoder coder = new StandardYamlCoder();
66
67     /**
68      * Set up for tests.
69      *
70      * @throws CoderException the exception
71      */
72     @Before
73     public void setup() throws CoderException {
74         ToscaServiceTemplate toscaPolicyType = coder
75             .decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"), ToscaServiceTemplate.class);
76         ToscaServiceTemplate toscaPolicy =
77             coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"), ToscaServiceTemplate.class);
78         serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
79         serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
80         Mockito
81             .when(toscaRepository.findById(
82                 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
83             .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
84     }
85
86
87     @Test
88     public void testGetPolicyList() throws PfModelException {
89         assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
90             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
91
92         assertThat(toscaService.getPolicyList(NAME, VERSION_1)).hasSize(0);
93
94         assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
95     }
96
97     @Test
98     public void testGetPolicyTypeList() throws PfModelException {
99         assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
100             .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
101
102         assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).hasSize(0);
103
104         assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
105         assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);
106     }
107 }