7566a29749a01e8bd025fcbacdc90d294357d362
[sdc.git] /
1 /*
2  * Copyright © 2018 Nokia
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.config;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.onap.config.NonConfigResource.CONFIG_LOCATION;
22 import static org.onap.config.NonConfigResource.NODE_CONFIG_LOCATION;
23
24 import java.io.File;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Collections;
30 import java.util.Map;
31 import org.junit.Test;
32
33 public class NonConfigResourceTest {
34
35     private static final String RESOURCE_NAME = NonConfigResourceTest.class.getSimpleName() + ".class";
36     private final URL sampleUrlResource = NonConfigResourceTest.class.getResource(RESOURCE_NAME);
37     private final String sampleResourcePath = sampleUrlResource.getPath();
38     private final File sampleResourceFile = new File(sampleResourcePath);
39
40     @Test
41     public void testShouldLocateResourceWhenAbsPathProvided2() {
42         Map<String, String> properties = Collections.emptyMap();
43         Path actualResourcePath = new NonConfigResource(properties::get).locate(sampleResourceFile.toString());
44         assertEquals(0, actualResourcePath.compareTo(sampleResourceFile.toPath()));
45     }
46
47     @Test
48     public void testShouldLocateResourceWhenPresentInFiles2() {
49         Map<String, String> properties = Collections.emptyMap();
50         NonConfigResource testedObject = new NonConfigResource(properties::get);
51         testedObject.add(sampleResourceFile);
52         Path thisFilePath = testedObject.locate(RESOURCE_NAME);
53         assertEquals(0, thisFilePath.compareTo(sampleResourceFile.toPath()));
54     }
55
56     @Test
57     public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() {
58
59         Map<String, String> properties = Collections.singletonMap(
60                 NODE_CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
61
62         NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
63         System.getProperties().setProperty(NODE_CONFIG_LOCATION,
64                 new File(sampleResourcePath).getParentFile().getPath());
65         Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
66         assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath()));
67     }
68
69     @Test
70     public void testShouldLocateResourceWhenConfigPropertyIsSet2() {
71
72         Map<String, String> properties = Collections.singletonMap(
73                 CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
74
75         NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
76         Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
77         assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath()));
78     }
79
80     @Test
81     public void testShouldLocatePathWhenResourcePresentInUrls2() throws URISyntaxException {
82         Map<String, String> properties = Collections.emptyMap();
83         NonConfigResource testedObject = new NonConfigResource(properties::get);
84         testedObject.add(sampleUrlResource);
85         Path thisFilePath = testedObject.locate(RESOURCE_NAME);
86         assertEquals(0, thisFilePath.compareTo(Paths.get(sampleUrlResource.toURI())));
87     }
88
89     @Test
90     public void testShouldNotLocateResource2() {
91         Map<String, String> properties = Collections.emptyMap();
92         NonConfigResource testedObject = new NonConfigResource(properties::get);
93         Path thisFilePath = testedObject.locate("nonexistingresource");
94         assertNull(thisFilePath);
95     }
96 }