2d0ba3ba28736c27bec827c2ab9ce43f280cd2a2
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.decoding.policy.file;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Collection;
30
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.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     private static final String POLICY_FILE_NAME = "apex_ddf_policy";
50     private static final String POLICY_TYPE_FILE_NAME = "apex_ddf_policy_type";
51     private static final String GROUP_NAME = "apexPdpDecoderConfiguration";
52
53     /**
54      * Set up.
55      */
56     @BeforeClass
57     public static void setUp() {
58         final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters =
59                 new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE_FILE_NAME);
60         configurationParameters.setName(GROUP_NAME);
61         ParameterService.register(configurationParameters);
62     }
63
64     /**
65      * Tear down.
66      */
67     @AfterClass
68     public static void tearDown() {
69         ParameterService.deregister(GROUP_NAME);
70     }
71
72     @Test
73     public void testDecodePolicy() {
74
75         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
76         decoder.configure(GROUP_NAME);
77
78         final File file = new File("src/test/resources/service-Sampleservice.csar");
79         final Csar csar = new Csar(file.getAbsolutePath());
80
81         try {
82             assertTrue(decoder.canHandle(csar));
83             final Collection<ToscaEntity> policyHolders = decoder.decode(csar);
84             assertEquals(2, policyHolders.size());
85         } catch (final Exception exp) {
86             fail("Test must not throw an exception");
87         }
88     }
89
90     @Test
91     public void testDecodePolicyZipError() {
92
93         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
94         decoder.configure(GROUP_NAME);
95
96         final File file = new File("unknown.csar");
97         final Csar csar = new Csar(file.getAbsolutePath());
98
99         try {
100             assertTrue(decoder.canHandle(csar));
101             decoder.decode(csar);
102             fail("Test must throw an exception");
103         } catch (final Exception exp) {
104             assertTrue(exp.getCause() instanceof IOException);
105             assertTrue(exp.getMessage().contains("Failed decoding the policy"));
106         }
107     }
108
109
110     @Test
111     public void testDecodePolicyCoderError() {
112
113         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
114         decoder.configure(GROUP_NAME);
115
116         final File file = new File("src/test/resources/service-Sampleservice-test.csar");
117         final Csar csar = new Csar(file.getAbsolutePath());
118
119         try {
120             assertTrue(decoder.canHandle(csar));
121             decoder.decode(csar);
122             fail("Test must throw an exception");
123         } catch (final Exception exp) {
124             assertTrue(exp.getCause() instanceof CoderException);
125             assertTrue(exp.getMessage().contains("Failed decoding the policy"));
126         }
127     }
128 }