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.assertEquals;
 
  25 import static org.junit.Assert.assertFalse;
 
  26 import static org.junit.Assert.assertTrue;
 
  27 import static org.junit.Assert.fail;
 
  29 import com.google.gson.Gson;
 
  30 import com.google.gson.GsonBuilder;
 
  31 import java.io.FileReader;
 
  32 import java.io.IOException;
 
  33 import org.junit.Rule;
 
  34 import org.junit.Test;
 
  35 import org.junit.rules.TemporaryFolder;
 
  36 import org.onap.policy.common.parameters.GroupValidationResult;
 
  39  * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
 
  42 public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
 
  44     public TemporaryFolder tempFolder = new TemporaryFolder();
 
  47     public void testFileSystemConfiguration() throws IOException {
 
  48         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
 
  49         String validPath = null;
 
  51             validPath = tempFolder.getRoot().getAbsolutePath();
 
  53             configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup();
 
  54             configParameters.setWatchPath(validPath);
 
  55             configParameters.setMaxThread(2);
 
  56         } catch (final Exception e) {
 
  57             fail("test should not thrown an exception here: " + e.getMessage());
 
  59         final GroupValidationResult validationResult = configParameters.validate();
 
  60         assertTrue(validationResult.isValid());
 
  61         assertEquals(validPath, configParameters.getWatchPath());
 
  62         assertEquals(2, configParameters.getMaxThread());
 
  66     public void testInvalidFileSystemConfiguration() throws IOException {
 
  67         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
 
  69             final Gson gson = new GsonBuilder().create();
 
  70             configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
 
  71                     FileSystemReceptionHandlerConfigurationParameterGroup.class);
 
  72         } catch (final Exception e) {
 
  73             fail("test should not thrown an exception here: " + e.getMessage());
 
  75         final GroupValidationResult validationResult = configParameters.validate();
 
  76         assertFalse(validationResult.isValid());
 
  80     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
 
  81         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
 
  83         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
 
  84                 new FileSystemReceptionHandlerConfigurationParameterGroup();
 
  85         configParameters.setWatchPath(invalidPath);
 
  87         final GroupValidationResult validateResult = configParameters.validate();
 
  88         assertFalse(validateResult.isValid());
 
  89         assertTrue(validateResult.getResult().contains("must be a valid directory"));