e474f693b6849a13408a571047bb12a303beeb0c
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Intel Corp. All rights reserved.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.file;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Date;
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.runners.MockitoJUnitRunner;
42 import org.onap.policy.common.parameters.ParameterGroup;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.distribution.model.OptimizationPolicy;
45 import org.onap.policy.distribution.model.Policy;
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 FilePolicyForwarderParameterBuilder builder = new FilePolicyForwarderParameterBuilder();
65         builder.setPath(tempFolder.getRoot().getAbsolutePath().toString()).setVerbose(VERBOSE);
66         final ParameterGroup parameterGroup = new FilePolicyForwarderParameterGroup(builder);
67         parameterGroup.setName(GROUP_NAME);
68         ParameterService.register(parameterGroup);
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<Policy> policies = new ArrayList<>();
82         final OptimizationPolicy policy = new OptimizationPolicy();
83
84         policy.setPolicyName("test");
85         policy.setPolicyDescription("test");
86         policy.setOnapName("");
87         policy.setConfigBody("");
88         policy.setConfigBodyType("");
89         policy.setTimetolive(new Date());
90         policy.setGuard("");
91         policy.setRiskLevel("");
92         policy.setRiskType("");
93         policies.add(policy);
94
95         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
96         forwarder.configure(GROUP_NAME);
97
98         try {
99             forwarder.forward(policies);
100             Path path = Paths.get(tempFolder.getRoot().getAbsolutePath().toString(), policy.getPolicyName());
101             assertTrue(Files.exists(path));
102         } catch (final Exception exp) {
103             fail("Test must not throw an exception");
104         }
105     }
106
107     @Test
108     @SuppressWarnings("unchecked")
109     public void testForwardPolicyError() {
110         final Collection<Policy> policies = new ArrayList<>();
111         OptimizationPolicy policy = new OptimizationPolicy();
112         policy.setPolicyName("test");
113         policy.setPolicyDescription("test");
114         policy.setOnapName("");
115         policy.setConfigBody("");
116         policy.setConfigBodyType("");
117         policy.setTimetolive(new Date());
118         policy.setGuard("");
119         policy.setRiskLevel("");
120         policy.setRiskType("");
121
122         OptimizationPolicy spy = Mockito.spy(policy);
123         Mockito.when(spy.getRiskType()).thenThrow(IOException.class);
124         policies.add(spy);
125
126         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
127         forwarder.configure(GROUP_NAME);
128
129         try {
130             forwarder.forward(policies);
131             fail("Test must throw an exception");
132         } catch (final Exception exp) {
133             assertTrue(exp.getMessage().contains("Error sending policy"));
134         }
135     }
136
137     @Test
138     public void testForwardUnsupportedPolicy() {
139         final Collection<Policy> policies = new ArrayList<>();
140         final FilePolicyForwarder forwarder = new FilePolicyForwarder();
141         forwarder.configure(GROUP_NAME);
142
143         final Policy policy = new UnsupportedPolicy();
144         policies.add(policy);
145
146         try {
147             forwarder.forward(policies);
148             fail("Test must throw an exception");
149         } catch (final Exception exp) {
150             assertTrue(exp.getMessage().contains("Cannot forward policy"));
151         }
152     }
153
154     class UnsupportedPolicy implements Policy {
155
156         @Override
157         public String getPolicyName() {
158             return "unsupported";
159         }
160
161         @Override
162         public String getPolicyType() {
163             return "unsupported";
164         }
165     }
166 }