2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Intel Corp. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2019-2020 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.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertTrue;
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;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.ClassRule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.common.parameters.ParameterService;
43 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 * Class to perform unit test of {@link FilePolicyForwarder}.
52 @RunWith(MockitoJUnitRunner.class)
53 public class FilePolicyForwarderTest {
55 public static TemporaryFolder tempFolder = new TemporaryFolder();
57 private static final boolean VERBOSE = true;
58 private static final String GROUP_NAME = "fileConfiguration";
64 public static void setUp() {
65 final FilePolicyForwarderParameterGroup configurationParameters = new FilePolicyForwarderParameterGroup();
66 configurationParameters.setPath(tempFolder.getRoot().getAbsolutePath().toString());
67 configurationParameters.setVerbose(VERBOSE);
68 configurationParameters.setName(GROUP_NAME);
69 ParameterService.register(configurationParameters);
76 public static void tearDown() {
77 ParameterService.deregister(GROUP_NAME);
81 public void testForwardPolicy() throws PolicyForwardingException {
82 final Collection<ToscaEntity> policies = new ArrayList<>();
83 final ToscaPolicy policy = createPolicy(policies, "test", "test");
85 final FilePolicyForwarder forwarder = new FilePolicyForwarder();
86 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));
94 public void testForwardPolicyError() {
95 final Collection<ToscaEntity> policies = new ArrayList<>();
96 final ToscaPolicy policy = createPolicy(policies, "test", "test");
98 final ToscaPolicy spy = Mockito.spy(policy);
99 Mockito.when(spy.toString()).thenAnswer(invocation -> {
100 throw new IOException();
104 final FilePolicyForwarder forwarder = new FilePolicyForwarder();
105 forwarder.configure(GROUP_NAME);
107 assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
108 .hasMessageContaining("Error sending policy");
112 public void testForwardUnsupportedPolicy() {
113 final Collection<ToscaEntity> policies = new ArrayList<>();
114 final FilePolicyForwarder forwarder = new FilePolicyForwarder();
115 forwarder.configure(GROUP_NAME);
117 final ToscaEntity policy = new UnsupportedPolicy();
118 policies.add(policy);
120 assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
121 .hasMessageContaining("Cannot forward policy");
124 class UnsupportedPolicy extends ToscaEntity {
127 public String getName() {
128 return "unsupported";
132 private ToscaPolicy createPolicy(final Collection<ToscaEntity> policies, final String name,
133 final String description) {
134 final ToscaPolicy policy = new ToscaPolicy();
135 policy.setName("test");
136 policy.setDescription("test");
137 policies.add(policy);