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