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.distribution.model.Csar;
 
  38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  41  * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}.
 
  43  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  45 @RunWith(MockitoJUnitRunner.class)
 
  46 public class PolicyDecoderFileInCsarToPolicyTest {
 
  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";
 
  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);
 
  67     public static void tearDown() {
 
  68         ParameterService.deregister(GROUP_NAME);
 
  72     public void testDecodePolicy() {
 
  74         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
 
  75         decoder.configure(GROUP_NAME);
 
  77         final File file = new File("src/test/resources/sampleTestService.csar");
 
  78         final Csar csar = new Csar(file.getAbsolutePath());
 
  81             decoder.canHandle(csar);
 
  82             final Collection<ToscaPolicy> policyHolders = decoder.decode(csar);
 
  83             for (final ToscaPolicy policy : policyHolders) {
 
  84                 assertEquals(POLICY_FILE_NAME, policy.getName());
 
  85                 assertEquals(POLICY_TYPE, policy.getType());
 
  87         } catch (final Exception exp) {
 
  88             fail("Test must not throw an exception");
 
  93     public void testDecodePolicyError() throws IOException {
 
  95         final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
 
  96         decoder.configure(GROUP_NAME);
 
  98         final File file = new File("unknown.csar");
 
  99         final Csar csar = new Csar(file.getAbsolutePath());
 
 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"));