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