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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ToscaServiceTemplateServiceTest {
48 private static final String VERSION_1 = "1.0.0";
50 private static final String VERSION = "version";
52 private static final String NAME = "name";
54 private static final String INVALID_VERSION_ERR_MSG =
55 "parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
58 private ToscaServiceTemplateRepository toscaRepository;
61 private ToscaServiceTemplateService toscaService;
63 private ToscaServiceTemplate serviceTemplate;
65 private StandardCoder coder = new StandardYamlCoder();
70 * @throws CoderException the exception
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());
81 .when(toscaRepository.findById(
82 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
83 .thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
88 public void testGetPolicyList() throws PfModelException {
89 assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
90 .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
92 assertThat(toscaService.getPolicyList(NAME, VERSION_1)).hasSize(0);
94 assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
98 public void testGetPolicyTypeList() throws PfModelException {
99 assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
100 .isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
102 assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).hasSize(0);
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);