92d9443e7b79d3ecc17a316d571b153f5b84366b
[policy/distribution.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.reception.handling.file;
23
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;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31
32 import java.io.FileReader;
33 import java.io.IOException;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.onap.policy.common.parameters.GroupValidationResult;
39
40 /**
41  * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
42  *
43  */
44 public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
45     @Rule
46     public TemporaryFolder tempFolder = new TemporaryFolder();
47
48     @Test
49     public void testFileSystemConfiguration() throws IOException {
50         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
51         String validPath = null;
52         try {
53             validPath = tempFolder.getRoot().getAbsolutePath();
54
55             final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
56                     new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(validPath);
57             configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
58         } catch (final Exception e) {
59             fail("test should not thrown an exception here: " + e.getMessage());
60         }
61         final GroupValidationResult validationResult = configParameters.validate();
62         assertTrue(validationResult.isValid());
63         assertEquals(validPath, configParameters.getWatchPath());
64     }
65
66     @Test
67     public void testInvalidFileSystemConfiguration() throws IOException {
68         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
69         try {
70             final Gson gson = new GsonBuilder().create();
71             configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
72                     FileSystemReceptionHandlerConfigurationParameterGroup.class);
73         } catch (final Exception e) {
74             fail("test should not thrown an exception here: " + e.getMessage());
75         }
76         final GroupValidationResult validationResult = configParameters.validate();
77         assertFalse(validationResult.isValid());
78     }
79
80     @Test
81     public void testFileSystemReceptionHandlerConfigurationParameterBuilder() {
82
83         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
84                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath("/foo/bar");
85         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
86                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
87
88         assertEquals("/foo/bar", configParameters.getWatchPath());
89     }
90
91     @Test
92     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
93         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
94
95         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
96                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(invalidPath);
97         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
98                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
99
100         final GroupValidationResult validateResult = configParameters.validate();
101         assertFalse(validateResult.isValid());
102         assertTrue(validateResult.getResult().contains("must be a valid directory"));
103     }
104 }