fc2a2b6a30f0af26dbef2a816f5903c78bb89856
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
23 import static org.junit.Assert.fail;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.concurrent.atomic.AtomicInteger;
34
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Rule;
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.invocation.InvocationOnMock;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.mockito.stubbing.Answer;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47 import org.onap.policy.common.parameters.ParameterService;
48 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
49 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
50
51 /**
52  * Class to perform unit test of {@link FileSystemReceptionHandler}.
53  */
54 @RunWith(MockitoJUnitRunner.class)
55 public class TestFileSystemReceptionHandler {
56
57     private static final Logger LOGGER = FlexLogger.getLogger(TestFileSystemReceptionHandler.class);
58
59     @Rule
60     public TemporaryFolder tempFolder = new TemporaryFolder();
61
62     private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
63     private FileSystemReceptionHandler fileSystemHandler;
64
65
66     /**
67      * Setup for the test cases.
68      *
69      * @throws IOException if it occurs
70      * @throws SecurityException if it occurs
71      * @throws NoSuchFieldException if it occurs
72      * @throws IllegalAccessException if it occurs
73      * @throws IllegalArgumentException if it occurs
74      */
75     @Before
76     public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
77             IllegalAccessException {
78         DistributionStatisticsManager.resetAllStatistics();
79
80         final Gson gson = new GsonBuilder().create();
81         pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-filesystem.json"),
82                 FileSystemReceptionHandlerConfigurationParameterGroup.class);
83         ParameterService.register(pssdConfigParameters);
84         fileSystemHandler = new FileSystemReceptionHandler();
85     }
86
87     @After
88     public void teardown() {
89         ParameterService.deregister(pssdConfigParameters);
90     }
91
92     @Test
93     public final void testInit() throws IOException {
94         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
95         Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
96         sypHandler.initializeReception(pssdConfigParameters.getName());
97         Mockito.verify(sypHandler, Mockito.times(1)).main(Mockito.isA(String.class));
98     }
99
100     @Test
101     public final void testDestroy() throws IOException {
102         try {
103             final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
104             Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
105             sypHandler.initializeReception(pssdConfigParameters.getName());
106             sypHandler.destroy();
107         } catch (final Exception exp) {
108             LOGGER.error(exp);
109             fail("Test should not throw any exception");
110         }
111
112     }
113
114     @Test
115     public void testMain() throws IOException, PolicyDecodingException {
116         final Object lock = new Object();
117         final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
118
119         class Processed {
120             public boolean processed = false;
121         }
122
123         Processed cond = new Processed();
124
125         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
126         Mockito.doAnswer(new Answer<Object>() {
127             public Object answer(InvocationOnMock invocation) {
128                 synchronized (lock) {
129                     cond.processed = true;
130                     lock.notifyAll();
131                 }
132                 return null;
133             }
134         }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
135
136         Thread th = new Thread(() -> {
137             try {
138                 sypHandler.main(watchPath);
139             } catch (IOException ex) {
140                 LOGGER.error(ex);
141             }
142         });
143
144         th.start();
145         try {
146             //wait until internal watch service started or counter reached
147             AtomicInteger counter = new AtomicInteger();
148             counter.set(0);
149             synchronized (lock) {
150                 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
151                     lock.wait(1000);
152                 }
153             }
154             Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
155                 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
156             //wait until mock method triggered or counter reached
157             counter.set(0);
158             synchronized (lock) {
159                 while (!cond.processed && counter.getAndIncrement() < 10) {
160                     lock.wait(1000);
161                 }
162             }
163             sypHandler.destroy();
164             th.interrupt();
165             th.join();
166         } catch (final InterruptedException ex) {
167             LOGGER.error(ex);
168         }
169         Mockito.verify(sypHandler, Mockito.times(1))
170             .createPolicyInputAndCallHandler(Mockito.isA(String.class));
171
172     }
173 }
174