640f472302b85dc8684a482ffbfd662eab041540
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020, 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.distribution.reception.decoding.policy.file;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.util.Collection;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.ParameterService;
34 import org.onap.policy.distribution.model.Csar;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
36 import org.onap.policy.distribution.reception.handling.sdc.CommonTestData;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
38
39 /**
40  * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
43  */
44 public class PolicyDecoderFileInCsarToPolicyTest {
45
46     /**
47      * Set up.
48      */
49     @BeforeClass
50     public static void setUp() {
51         final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = CommonTestData
52                 .getPolicyDecoderParameters("src/test/resources/parameters/FileInCsarPolicyDecoderParameters.json",
53                         PolicyDecoderFileInCsarToPolicyParameterGroup.class);
54         configurationParameters.setName(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
55         ParameterService.register(configurationParameters);
56     }
57
58     /**
59      * Tear down.
60      */
61     @AfterClass
62     public static void tearDown() {
63         ParameterService.deregister(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
64     }
65
66     @Test
67     public void testDecodePolicy() throws PolicyDecodingException {
68
69         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
70         decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
71
72         final File file = new File("src/test/resources/service-Sampleservice.csar");
73         final Csar csar = new Csar(file.getAbsolutePath());
74
75         assertTrue(decoder.canHandle(csar));
76         final Collection<ToscaEntity> policyHolders = decoder.decode(csar);
77         assertEquals(2, policyHolders.size());
78     }
79
80     @Test
81     public void testDecodeYamlPolicy() throws PolicyDecodingException {
82
83         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
84         decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
85
86         final File file = new File("src/test/resources/service-Sampleservice-yaml.csar");
87         final Csar csar = new Csar(file.getAbsolutePath());
88
89         assertTrue(decoder.canHandle(csar));
90         final Collection<ToscaEntity> policyHolders = decoder.decode(csar);
91         assertEquals(2, policyHolders.size());
92     }
93
94     @Test
95     public void testDecodePolicyZipError() {
96
97         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
98         decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
99
100         final File file = new File("unknown.csar");
101         final Csar csar = new Csar(file.getAbsolutePath());
102
103         assertTrue(decoder.canHandle(csar));
104         assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class)
105         .hasMessageContaining("Couldn't read the zipFile");
106     }
107
108
109     @Test
110     public void testDecodePolicyCoderError() {
111
112         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
113         decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
114
115         final File file = new File("src/test/resources/service-Sampleservice-test.csar");
116         final Csar csar = new Csar(file.getAbsolutePath());
117
118         assertTrue(decoder.canHandle(csar));
119         assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class)
120         .hasMessageContaining("Failed decoding the policy");
121     }
122 }