2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Intel. All rights reserved.
4 * Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.reception.handling.file;
24 import static org.junit.Assert.fail;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.concurrent.atomic.AtomicInteger;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.mockito.stubbing.Answer;
46 import org.onap.policy.common.logging.flexlogger.FlexLogger;
47 import org.onap.policy.common.logging.flexlogger.Logger;
48 import org.onap.policy.common.parameters.ParameterService;
49 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
50 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
53 * Class to perform unit test of {@link FileSystemReceptionHandler}.
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestFileSystemReceptionHandler {
58 private static final Logger LOGGER = FlexLogger.getLogger(TestFileSystemReceptionHandler.class);
61 public TemporaryFolder tempFolder = new TemporaryFolder();
63 private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
64 private FileSystemReceptionHandler fileSystemHandler;
68 * Setup for the test cases.
70 * @throws IOException if it occurs
71 * @throws SecurityException if it occurs
72 * @throws NoSuchFieldException if it occurs
73 * @throws IllegalAccessException if it occurs
74 * @throws IllegalArgumentException if it occurs
77 public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
78 IllegalAccessException {
79 DistributionStatisticsManager.resetAllStatistics();
81 final Gson gson = new GsonBuilder().create();
82 pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-filesystem.json"),
83 FileSystemReceptionHandlerConfigurationParameterGroup.class);
84 ParameterService.register(pssdConfigParameters);
85 fileSystemHandler = new FileSystemReceptionHandler();
89 public void teardown() {
90 ParameterService.deregister(pssdConfigParameters);
94 public final void testInit() throws IOException {
95 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
96 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class));
97 sypHandler.initializeReception(pssdConfigParameters.getName());
98 Mockito.verify(sypHandler, Mockito.times(1)).initFileWatcher(Mockito.isA(String.class));
102 public final void testDestroy() throws IOException {
104 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
105 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class));
106 sypHandler.initializeReception(pssdConfigParameters.getName());
107 sypHandler.destroy();
108 } catch (final Exception exp) {
110 fail("Test should not throw any exception");
116 public void testMain() throws IOException, PolicyDecodingException {
117 final Object lock = new Object();
118 final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
121 public boolean processed = false;
124 final Processed cond = new Processed();
126 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
127 Mockito.doAnswer(new Answer<Object>() {
129 public Object answer(final InvocationOnMock invocation) {
130 synchronized (lock) {
131 cond.processed = true;
136 }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
138 final Thread th = new Thread(() -> {
140 sypHandler.initFileWatcher(watchPath);
141 } catch (final IOException ex) {
148 // wait until internal watch service started or counter reached
149 final AtomicInteger counter = new AtomicInteger();
151 synchronized (lock) {
152 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
156 Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
157 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
158 // wait until mock method triggered or counter reached
160 synchronized (lock) {
161 while (!cond.processed && counter.getAndIncrement() < 10) {
165 sypHandler.destroy();
168 } catch (final InterruptedException ex) {
171 Mockito.verify(sypHandler, Mockito.times(1)).createPolicyInputAndCallHandler(Mockito.isA(String.class));