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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.handling.sdc;
23 import static org.junit.Assert.fail;
25 import com.google.gson.Gson;
26 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;
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;
52 * Class to perform unit test of {@link FileSystemReceptionHandler}.
54 @RunWith(MockitoJUnitRunner.class)
55 public class TestFileSystemReceptionHandler {
57 private static final Logger LOGGER = FlexLogger.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 {
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));
101 public final void testDestroy() throws IOException {
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) {
109 fail("Test should not throw any exception");
115 public void testMain() throws IOException, PolicyDecodingException {
116 final Object lock = new Object();
117 final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
120 public boolean processed = false;
123 Processed cond = new Processed();
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;
134 }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
136 Thread th = new Thread(() -> {
138 sypHandler.main(watchPath);
139 } catch (IOException ex) {
146 //wait until internal watch service started or counter reached
147 AtomicInteger counter = new AtomicInteger();
149 synchronized (lock) {
150 while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) {
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
158 synchronized (lock) {
159 while (!cond.processed && counter.getAndIncrement() < 10) {
163 sypHandler.destroy();
166 } catch (final InterruptedException ex) {
169 Mockito.verify(sypHandler, Mockito.times(1))
170 .createPolicyInputAndCallHandler(Mockito.isA(String.class));