e7a97a24618db530b622f8e5d68deb368cbc5069
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. All rights reserved.
4  *  Copyright (C) 2019-2020, 2022 Nordix Foundation.
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.reception.handling.file;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
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 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
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.mockito.stubbing.Answer;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Class to perform unit test of {@link FileSystemReceptionHandler}.
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class TestFileSystemReceptionHandler {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(TestFileSystemReceptionHandler.class);
55
56     @Rule
57     public TemporaryFolder tempFolder = new TemporaryFolder();
58
59     private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
60     private FileSystemReceptionHandler fileSystemHandler;
61
62
63     /**
64      * Setup for the test cases.
65      *
66      * @throws IOException if it occurs
67      * @throws SecurityException if it occurs
68      * @throws IllegalArgumentException if it occurs
69      */
70     @Before
71     public final void init() throws IOException, SecurityException, IllegalArgumentException {
72         DistributionStatisticsManager.resetAllStatistics();
73
74         final Gson gson = new GsonBuilder().create();
75         pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-filesystem.json"),
76                 FileSystemReceptionHandlerConfigurationParameterGroup.class);
77         ParameterService.register(pssdConfigParameters);
78         fileSystemHandler = new FileSystemReceptionHandler();
79     }
80
81     @After
82     public void teardown() {
83         ParameterService.deregister(pssdConfigParameters);
84     }
85
86     @Test
87     public final void testInit() throws IOException {
88         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
89         Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
90                 Mockito.anyInt());
91         assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName()))
92             .doesNotThrowAnyException();
93     }
94
95     @Test
96     public final void testDestroy() throws IOException {
97         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
98         Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
99                 Mockito.anyInt());
100         assertThatCode(() -> {
101             sypHandler.initializeReception(pssdConfigParameters.getName());
102             sypHandler.destroy();
103         }).doesNotThrowAnyException();
104     }
105
106     @Test
107     public void testMain() throws IOException {
108         final Object lock = new Object();
109         final String watchPath = tempFolder.getRoot().getAbsolutePath();
110
111         class Processed {
112             public boolean processed = false;
113         }
114
115         final Processed cond = new Processed();
116
117         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
118         Mockito.doAnswer((Answer<Object>) invocation -> {
119             synchronized (lock) {
120                 cond.processed = true;
121                 lock.notifyAll();
122             }
123             return null;
124         }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
125
126         final Thread th = new Thread(() -> {
127             try {
128                 sypHandler.initFileWatcher(watchPath, 2);
129             } catch (final IOException ex) {
130                 LOGGER.error("testMain failed", ex);
131             }
132         });
133
134         th.start();
135         try {
136             // wait until internal watch service started or counter reached
137             final AtomicInteger counter = new AtomicInteger();
138             counter.set(0);
139             synchronized (lock) {
140                 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
141                     lock.wait(1000);
142                 }
143             }
144             Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
145                     Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
146             // wait until mock method triggered or counter reached
147             counter.set(0);
148             synchronized (lock) {
149                 while (!cond.processed && counter.getAndIncrement() < 10) {
150                     lock.wait(1000);
151                 }
152             }
153             sypHandler.destroy();
154             th.interrupt();
155             th.join();
156         } catch (final InterruptedException ex) {
157             LOGGER.error("testMain failed", ex);
158         }
159         Mockito.verify(sypHandler, Mockito.times(1)).createPolicyInputAndCallHandler(Mockito.isA(String.class));
160
161     }
162 }
163