03455b84738b91219efc33f28f8bffb4fd76a18f
[policy/distribution.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.handling.file;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
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;
37
38 /**
39  * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
40  *
41  */
42 public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
43     @Rule
44     public TemporaryFolder tempFolder = new TemporaryFolder();
45
46     @Test
47     public void testFileSystemConfiguration() throws IOException {
48         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
49         String validPath = null;
50
51         validPath = tempFolder.getRoot().getAbsolutePath();
52
53         configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup();
54         configParameters.setWatchPath(validPath);
55         configParameters.setMaxThread(2);
56
57         final GroupValidationResult validationResult = configParameters.validate();
58         assertTrue(validationResult.isValid());
59         assertEquals(validPath, configParameters.getWatchPath());
60         assertEquals(2, configParameters.getMaxThread());
61     }
62
63     @Test
64     public void testInvalidFileSystemConfiguration() throws IOException {
65         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
66         final Gson gson = new GsonBuilder().create();
67         configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
68                 FileSystemReceptionHandlerConfigurationParameterGroup.class);
69         final GroupValidationResult validationResult = configParameters.validate();
70         assertFalse(validationResult.isValid());
71
72     }
73
74     @Test
75     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
76         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
77
78         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
79                 new FileSystemReceptionHandlerConfigurationParameterGroup();
80         configParameters.setWatchPath(invalidPath);
81
82         final GroupValidationResult validateResult = configParameters.validate();
83         assertFalse(validateResult.isValid());
84         assertTrue(validateResult.getResult().contains("must be a valid directory"));
85     }
86 }