e40ab3fe4b1d98484bf77525ec6f92d699b0d0c1
[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.distribution.model.Csar;
38 import org.onap.policy.distribution.model.PolicyAsString;
39
40 /**
41  * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}.
42  *
43  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class PolicyDecoderFileInCsarToPolicyTest {
47
48     private static final String POLICY_FILE_NAME = "SamplePolicyModelJAVASCRIPT";
49     private static final String POLICY_TYPE = "APEX";
50     private static final String GROUP_NAME = "apexPdpDecoderConfiguration";
51
52     /**
53      * Set up.
54      */
55     @BeforeClass
56     public static void setUp() {
57         final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters =
58                 new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE);
59         configurationParameters.setName(GROUP_NAME);
60         ParameterService.register(configurationParameters);
61     }
62
63     /**
64      * Tear down.
65      */
66     @AfterClass
67     public static void tearDown() {
68         ParameterService.deregister(GROUP_NAME);
69     }
70
71     @Test
72     public void testDecodePolicy() {
73
74         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
75         decoder.configure(GROUP_NAME);
76
77         final File file = new File("src/test/resources/sampleTestService.csar");
78         final Csar csar = new Csar(file.getAbsolutePath());
79
80         try {
81             decoder.canHandle(csar);
82             final Collection<PolicyAsString> policyHolders = decoder.decode(csar);
83             for (final PolicyAsString policy : policyHolders) {
84                 assertEquals(POLICY_FILE_NAME, policy.getPolicyName());
85                 assertEquals(POLICY_TYPE, policy.getPolicyType());
86             }
87         } catch (final Exception exp) {
88             fail("Test must not throw an exception");
89         }
90     }
91
92     @Test
93     public void testDecodePolicyError() throws IOException {
94
95         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
96         decoder.configure(GROUP_NAME);
97
98         final File file = new File("unknown.csar");
99         final Csar csar = new Csar(file.getAbsolutePath());
100
101         try {
102             decoder.canHandle(csar);
103             decoder.decode(csar);
104             fail("Test must throw an exception");
105         } catch (final Exception exp) {
106             assertTrue(exp.getMessage().contains("Failed decoding the policy"));
107         }
108     }
109 }