2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Intel. All rights reserved.
4 * Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Nordix Foundation
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.distribution.reception.handling.file;
25 import static org.assertj.core.api.Assertions.assertThatCode;
27 import com.google.gson.Gson;
28 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;
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.parameters.ParameterService;
46 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
47 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Class to perform unit test of {@link FileSystemReceptionHandler}.
54 @RunWith(MockitoJUnitRunner.class)
55 public class TestFileSystemReceptionHandler {
57 private static final Logger LOGGER = LoggerFactory.getLogger(TestFileSystemReceptionHandler.class);
60 public TemporaryFolder tempFolder = new TemporaryFolder();
62 private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
63 private FileSystemReceptionHandler fileSystemHandler;
67 * Setup for the test cases.
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
76 public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
77 IllegalAccessException {
78 DistributionStatisticsManager.resetAllStatistics();
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();
88 public void teardown() {
89 ParameterService.deregister(pssdConfigParameters);
93 public final void testInit() throws IOException, InterruptedException {
94 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
95 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
97 assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName()))
98 .doesNotThrowAnyException();
102 public final void testDestroy() throws IOException {
103 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
104 Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class),
106 assertThatCode(() -> {
107 sypHandler.initializeReception(pssdConfigParameters.getName());
108 sypHandler.destroy();
109 }).doesNotThrowAnyException();
113 public void testMain() throws IOException, PolicyDecodingException {
114 final Object lock = new Object();
115 final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
118 public boolean processed = false;
121 final Processed cond = new Processed();
123 final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
124 Mockito.doAnswer(new Answer<Object>() {
126 public Object answer(final InvocationOnMock invocation) {
127 synchronized (lock) {
128 cond.processed = true;
133 }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
135 final Thread th = new Thread(() -> {
137 sypHandler.initFileWatcher(watchPath, 2);
138 } catch (final IOException ex) {
139 LOGGER.error("testMain failed", ex);
145 // wait until internal watch service started or counter reached
146 final AtomicInteger counter = new AtomicInteger();
148 synchronized (lock) {
149 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
153 Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
154 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
155 // wait until mock method triggered or counter reached
157 synchronized (lock) {
158 while (!cond.processed && counter.getAndIncrement() < 10) {
162 sypHandler.destroy();
165 } catch (final InterruptedException ex) {
166 LOGGER.error("testMain failed", ex);
168 Mockito.verify(sypHandler, Mockito.times(1)).createPolicyInputAndCallHandler(Mockito.isA(String.class));