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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.reception.decoding.policy.file;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertTrue;
 
  25 import static org.junit.Assert.fail;
 
  28 import java.io.IOException;
 
  29 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.models.tosca.authorative.concepts.ToscaEntity;
 
  42  * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}.
 
  44  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  46 @RunWith(MockitoJUnitRunner.class)
 
  47 public class PolicyDecoderFileInCsarToPolicyTest {
 
  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";
 
  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);
 
  68     public static void tearDown() {
 
  69         ParameterService.deregister(GROUP_NAME);
 
  73     public void testDecodePolicy() {
 
  75         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
 
  76         decoder.configure(GROUP_NAME);
 
  78         final File file = new File("src/test/resources/service-Sampleservice.csar");
 
  79         final Csar csar = new Csar(file.getAbsolutePath());
 
  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");
 
  91     public void testDecodePolicyZipError() {
 
  93         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
 
  94         decoder.configure(GROUP_NAME);
 
  96         final File file = new File("unknown.csar");
 
  97         final Csar csar = new Csar(file.getAbsolutePath());
 
 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"));
 
 111     public void testDecodePolicyCoderError() {
 
 113         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
 
 114         decoder.configure(GROUP_NAME);
 
 116         final File file = new File("src/test/resources/service-Sampleservice-test.csar");
 
 117         final Csar csar = new Csar(file.getAbsolutePath());
 
 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"));