2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Intel Corp. All rights reserved.
 
   4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
 
   5  *  Modifications Copyright (C) 2019 Nordix Foundation.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  19  * SPDX-License-Identifier: Apache-2.0
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.policy.distribution.forwarding.file;
 
  25 import static org.junit.Assert.assertTrue;
 
  26 import static org.junit.Assert.fail;
 
  28 import java.io.IOException;
 
  29 import java.nio.file.Files;
 
  30 import java.nio.file.Path;
 
  31 import java.nio.file.Paths;
 
  32 import java.util.ArrayList;
 
  33 import java.util.Collection;
 
  35 import org.junit.AfterClass;
 
  36 import org.junit.BeforeClass;
 
  37 import org.junit.ClassRule;
 
  38 import org.junit.Test;
 
  39 import org.junit.rules.TemporaryFolder;
 
  40 import org.junit.runner.RunWith;
 
  41 import org.mockito.Mockito;
 
  42 import org.mockito.runners.MockitoJUnitRunner;
 
  43 import org.onap.policy.common.parameters.ParameterService;
 
  44 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  48  * Class to perform unit test of {@link FilePolicyForwarder}.
 
  51 @RunWith(MockitoJUnitRunner.class)
 
  52 public class FilePolicyForwarderTest {
 
  54     public static TemporaryFolder tempFolder = new TemporaryFolder();
 
  56     private static final boolean VERBOSE = true;
 
  57     private static final String GROUP_NAME = "fileConfiguration";
 
  63     public static void setUp() {
 
  64         final FilePolicyForwarderParameterGroup configurationParameters = new FilePolicyForwarderParameterGroup();
 
  65         configurationParameters.setPath(tempFolder.getRoot().getAbsolutePath().toString());
 
  66         configurationParameters.setVerbose(VERBOSE);
 
  67         configurationParameters.setName(GROUP_NAME);
 
  68         ParameterService.register(configurationParameters);
 
  75     public static void tearDown() {
 
  76         ParameterService.deregister(GROUP_NAME);
 
  80     public void testForwardPolicy() {
 
  81         final Collection<ToscaEntity> policies = new ArrayList<>();
 
  82         final ToscaPolicy policy = createPolicy(policies, "test", "test");
 
  84         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
 
  85         forwarder.configure(GROUP_NAME);
 
  88             forwarder.forward(policies);
 
  89             final Path path = Paths.get(tempFolder.getRoot().getAbsolutePath().toString(), policy.getName());
 
  90             assertTrue(Files.exists(path));
 
  91         } catch (final Exception exp) {
 
  92             fail("Test must not throw an exception");
 
  97     @SuppressWarnings("unchecked")
 
  98     public void testForwardPolicyError() {
 
  99         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 100         final ToscaPolicy policy = createPolicy(policies, "test", "test");
 
 102         final ToscaPolicy spy = Mockito.spy(policy);
 
 103         Mockito.when(spy.toString()).thenThrow(IOException.class);
 
 106         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
 
 107         forwarder.configure(GROUP_NAME);
 
 110             forwarder.forward(policies);
 
 111             fail("Test must throw an exception");
 
 112         } catch (final Exception exp) {
 
 113             assertTrue(exp.getMessage().contains("Error sending policy"));
 
 118     public void testForwardUnsupportedPolicy() {
 
 119         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 120         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
 
 121         forwarder.configure(GROUP_NAME);
 
 123         final ToscaEntity policy = new UnsupportedPolicy();
 
 124         policies.add(policy);
 
 127             forwarder.forward(policies);
 
 128             fail("Test must throw an exception");
 
 129         } catch (final Exception exp) {
 
 130             assertTrue(exp.getMessage().contains("Cannot forward policy"));
 
 134     class UnsupportedPolicy extends ToscaEntity {
 
 137         public String getName() {
 
 138             return "unsupported";
 
 142     private ToscaPolicy createPolicy(final Collection<ToscaEntity> policies, final String name,
 
 143             final String description) {
 
 144         final ToscaPolicy policy = new ToscaPolicy();
 
 145         policy.setName("test");
 
 146         policy.setDescription("test");
 
 147         policies.add(policy);