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
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.assertj.core.api.Assertions.assertThatCode;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
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.mockito.Mockito;
40 import org.mockito.stubbing.Answer;
41 import org.onap.policy.common.parameters.ParameterService;
42 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Class to perform unit test of {@link FileSystemReceptionHandler}.
49 public class TestFileSystemReceptionHandler {
51 private static final Logger LOGGER = LoggerFactory.getLogger(TestFileSystemReceptionHandler.class);
54 public TemporaryFolder tempFolder = new TemporaryFolder();
56 private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
57 private FileSystemReceptionHandler fileSystemHandler;
61 * Setup for the test cases.
63 * @throws IOException if it occurs
64 * @throws SecurityException if it occurs
65 * @throws IllegalArgumentException if it occurs
68 public final void init() throws IOException, SecurityException, IllegalArgumentException {
69 DistributionStatisticsManager.resetAllStatistics();
71 final Gson gson = new GsonBuilder().create();
72 pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-filesystem.json"),
73 FileSystemReceptionHandlerConfigurationParameterGroup.class);
74 ParameterService.register(pssdConfigParameters);
75 fileSystemHandler = new FileSystemReceptionHandler();
79 public void teardown() {
80 ParameterService.deregister(pssdConfigParameters);
84 public final void testInit() throws IOException {
85 FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
86 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
88 assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName()))
89 .doesNotThrowAnyException();
93 public final void testDestroy() throws IOException {
94 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
95 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
97 assertThatCode(() -> {
98 sypHandler.initializeReception(pssdConfigParameters.getName());
100 }).doesNotThrowAnyException();
104 public void testMain() throws IOException {
105 final Object lock = new Object();
106 final String watchPath = tempFolder.getRoot().getAbsolutePath();
109 public boolean processed = false;
112 final Processed cond = new Processed();
114 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
115 Mockito.doAnswer((Answer<Object>) invocation -> {
116 synchronized (lock) {
117 cond.processed = true;
121 }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
123 final Thread th = new Thread(() -> {
125 sypHandler.initFileWatcher(watchPath, 2);
126 } catch (final IOException ex) {
127 LOGGER.error("testMain failed", ex);
133 // wait until internal watch service started or counter reached
134 final AtomicInteger counter = new AtomicInteger();
136 synchronized (lock) {
137 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
141 Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
142 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
143 // wait until mock method triggered or counter reached
145 synchronized (lock) {
146 while (!cond.processed && counter.getAndIncrement() < 10) {
150 sypHandler.destroy();
153 } catch (final InterruptedException ex) {
154 LOGGER.error("testMain failed", ex);
156 Mockito.verify(sypHandler, Mockito.times(1)).createPolicyInputAndCallHandler(Mockito.isA(String.class));