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.IOException;
 
  30 import java.nio.file.Files;
 
  31 import java.nio.file.Paths;
 
  33 import org.junit.After;
 
  34 import org.junit.Before;
 
  35 import org.junit.Rule;
 
  36 import org.junit.Test;
 
  37 import org.junit.rules.TemporaryFolder;
 
  38 import org.junit.runner.RunWith;
 
  39 import org.mockito.Mock;
 
  40 import org.mockito.Mockito;
 
  41 import org.mockito.runners.MockitoJUnitRunner;
 
  42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  43 import org.onap.policy.common.logging.flexlogger.Logger;
 
  44 import org.onap.policy.common.parameters.ParameterService;
 
  45 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 
  46 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  47 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
 
  50  * Class to perform unit test of {@link FileSystemReceptionHandler}.
 
  52 @RunWith(MockitoJUnitRunner.class)
 
  53 public class TestFileSystemReceptionHandler {
 
  55     private static final Logger LOGGER = FlexLogger.getLogger(TestFileSystemReceptionHandler.class);
 
  56     private static final String DUMMY_SERVICE_CSAR = "dummyService.csar";
 
  59     public TemporaryFolder tempFolder = new TemporaryFolder();
 
  61     private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
 
  62     private FileSystemReceptionHandler fileSystemHandler;
 
  66      * Setup for the test cases.
 
  68      * @throws IOException if it occurs
 
  69      * @throws SecurityException if it occurs
 
  70      * @throws NoSuchFieldException if it occurs
 
  71      * @throws IllegalAccessException if it occurs
 
  72      * @throws IllegalArgumentException if it occurs
 
  75     public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
 
  76             IllegalAccessException {
 
  77         DistributionStatisticsManager.resetAllStatistics();
 
  79         final Gson gson = new GsonBuilder().create();
 
  80         String json = "{ \"name\": \"parameterConfig9\", \"watchPath\": \"";
 
  81         json += tempFolder.getRoot().getAbsolutePath() + "\"}";
 
  82         pssdConfigParameters = gson.fromJson(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() {
 
  95         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
 
  96         Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
 
  97         sypHandler.initializeReception(pssdConfigParameters.getName());
 
  98         Mockito.verify(sypHandler, Mockito.times(1)).main(Mockito.isA(String.class));
 
 102     public final void testDestroy() {
 
 104             final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
 
 105             Mockito.doNothing().when(sypHandler).main(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 {
 
 118         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
 
 119         Mockito.doNothing().when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
 
 121         final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
 
 122         Thread th = new Thread(() -> {
 
 123             sypHandler.main(watchPath);
 
 128             // yield to main thread
 
 130             Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
 
 131                 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
 
 134             sypHandler.destroy();
 
 137         } catch (final InterruptedException ex) {
 
 140         Mockito.verify(sypHandler, Mockito.times(1))
 
 141             .createPolicyInputAndCallHandler(Mockito.isA(String.class));