72dba64dde03c4bc4b8d3748ddd2d85f2a1a4fc3
[policy/distribution.git] /
1 /*-
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 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.forwarding.file;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
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.models.tosca.authorative.concepts.ToscaEntity;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45
46
47 /**
48  * Class to perform unit test of {@link FilePolicyForwarder}.
49  *
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class FilePolicyForwarderTest {
53     @ClassRule
54     public static TemporaryFolder tempFolder = new TemporaryFolder();
55
56     private static final boolean VERBOSE = true;
57     private static final String GROUP_NAME = "fileConfiguration";
58
59     /**
60      * Set up.
61      */
62     @BeforeClass
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);
69     }
70
71     /**
72      * Tear down.
73      */
74     @AfterClass
75     public static void tearDown() {
76         ParameterService.deregister(GROUP_NAME);
77     }
78
79     @Test
80     public void testForwardPolicy() {
81         final Collection<ToscaEntity> policies = new ArrayList<>();
82         final ToscaPolicy policy = createPolicy(policies, "test", "test");
83
84         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
85         forwarder.configure(GROUP_NAME);
86
87         try {
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");
93         }
94     }
95
96     @Test
97     public void testForwardPolicyError() {
98         final Collection<ToscaEntity> policies = new ArrayList<>();
99         final ToscaPolicy policy = createPolicy(policies, "test", "test");
100
101         final ToscaPolicy spy = Mockito.spy(policy);
102         Mockito.when(spy.toString()).thenAnswer(invocation -> {
103             throw new IOException();
104         });
105         policies.add(spy);
106
107         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
108         forwarder.configure(GROUP_NAME);
109
110         try {
111             forwarder.forward(policies);
112             fail("Test must throw an exception");
113         } catch (final Exception exp) {
114             assertTrue(exp.getMessage().contains("Error sending policy"));
115         }
116     }
117
118     @Test
119     public void testForwardUnsupportedPolicy() {
120         final Collection<ToscaEntity> policies = new ArrayList<>();
121         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
122         forwarder.configure(GROUP_NAME);
123
124         final ToscaEntity policy = new UnsupportedPolicy();
125         policies.add(policy);
126
127         try {
128             forwarder.forward(policies);
129             fail("Test must throw an exception");
130         } catch (final Exception exp) {
131             assertTrue(exp.getMessage().contains("Cannot forward policy"));
132         }
133     }
134
135     class UnsupportedPolicy extends ToscaEntity {
136
137         @Override
138         public String getName() {
139             return "unsupported";
140         }
141     }
142
143     private ToscaPolicy createPolicy(final Collection<ToscaEntity> policies, final String name,
144             final String description) {
145         final ToscaPolicy policy = new ToscaPolicy();
146         policy.setName("test");
147         policy.setDescription("test");
148         policies.add(policy);
149         return policy;
150     }
151 }