279bb9767574b2779f10ee31784bd24a1bd47be0
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2022 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.onap.policy.common.parameters.ParameterService;
34 import org.onap.policy.distribution.model.Csar;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
36 import org.onap.policy.distribution.reception.handling.sdc.CommonTestData;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
38
39 /**
40  * Class to perform unit test of {@link AutomationCompositionDecoderFileInCsar}.
41  *
42  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
43  */
44 public class AutomationCompositionDecoderFileInCsarTest {
45
46     /**
47      * Set up.
48      */
49     @BeforeClass
50     public static void setUp() {
51         final AutomationCompositionDecoderFileInCsarParameterGroup configurationParameters = CommonTestData
52                 .getPolicyDecoderParameters(
53                     "src/test/resources/parameters/FileInCsarAutomationCompositionDecoderParameters.json",
54                     AutomationCompositionDecoderFileInCsarParameterGroup.class);
55         configurationParameters.setName(AutomationCompositionDecoderFileInCsarParameterGroup.class.getSimpleName());
56         ParameterService.register(configurationParameters);
57     }
58
59     /**
60      * Tear down.
61      */
62     @AfterClass
63     public static void tearDown() {
64         ParameterService.deregister(AutomationCompositionDecoderFileInCsarParameterGroup.class.getSimpleName());
65     }
66
67     @Test
68     public void testDecodeAutomationComposition() throws PolicyDecodingException {
69
70         final AutomationCompositionDecoderFileInCsar decoder = new AutomationCompositionDecoderFileInCsar();
71         decoder.configure(AutomationCompositionDecoderFileInCsarParameterGroup.class.getSimpleName());
72
73         final File file = new File("src/test/resources/service-Sampleservice-acm.csar");
74         final Csar csar = new Csar(file.getAbsolutePath());
75
76         assertTrue(decoder.canHandle(csar));
77         final Collection<ToscaEntity> automationCompositionHolders = decoder.decode(csar);
78         assertEquals(1, automationCompositionHolders.size());
79     }
80
81     @Test
82     public void testDecodeAutomationCompositionZipError() {
83
84         final AutomationCompositionDecoderFileInCsar decoder = new AutomationCompositionDecoderFileInCsar();
85         decoder.configure(AutomationCompositionDecoderFileInCsarParameterGroup.class.getSimpleName());
86
87         final File file = new File("unknown.csar");
88         final Csar csar = new Csar(file.getAbsolutePath());
89
90         assertTrue(decoder.canHandle(csar));
91         assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class)
92         .hasMessageContaining("Couldn't read the zipFile");
93     }
94 }