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