Added oparent to sdc main
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / NonConfigResourceTest.java
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.HashMap;
31 import java.util.Map;
32 import org.junit.Test;
33
34 public class NonConfigResourceTest {
35
36     private static final String RESOURCE_NAME = NonConfigResourceTest.class.getSimpleName() + ".class";
37     private final URL sampleUrlResource = NonConfigResourceTest.class.getResource(RESOURCE_NAME);
38     private final String sampleResourcePath = sampleUrlResource.getPath();
39     private final File sampleResourceFile = new File(sampleResourcePath);
40
41     @Test
42     public void testShouldLocateResourceWhenAbsPathProvided2() {
43         Map<String, String> properties = Collections.emptyMap();
44         Path actualResourcePath = new NonConfigResource(properties::get).locate(sampleResourceFile.toString());
45         assertEquals(0, actualResourcePath.compareTo(sampleResourceFile.toPath()));
46     }
47
48     @Test
49     public void testShouldLocateResourceWhenPresentInFiles2() {
50         Map<String, String> properties = Collections.emptyMap();
51         NonConfigResource testedObject = new NonConfigResource(properties::get);
52         testedObject.add(sampleResourceFile);
53         Path thisFilePath = testedObject.locate(RESOURCE_NAME);
54         assertEquals(0, thisFilePath.compareTo(sampleResourceFile.toPath()));
55     }
56
57     @Test
58     public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() {
59         final String path = new File(sampleResourcePath).getParentFile().getPath();
60         Map<String, String> properties = Collections.singletonMap(NODE_CONFIG_LOCATION, path);
61
62         NonConfigResource testedNonConfigResource =  new NonConfigResource(properties::get);
63         Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
64
65         NonConfigResource systemNonConfigResource = new NonConfigResource(System.getProperties()::getProperty);
66         System.setProperty(NODE_CONFIG_LOCATION, path);
67         Path systemFilePath = systemNonConfigResource.locate(RESOURCE_NAME);
68
69         Path testFilePath = sampleResourceFile.toPath();
70         assertEquals(0, thisFilePath.compareTo(testFilePath));
71         assertEquals(0, systemFilePath.compareTo(testFilePath));
72     }
73
74     @Test
75     public void testShouldLocateResourceWhenConfigPropertyIsSet2() {
76
77         Map<String, String> properties = Collections.singletonMap(
78                 CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
79
80         NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
81         Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
82         assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath()));
83     }
84
85     @Test
86     public void testShouldLocatePathWhenResourcePresentInUrls2() throws URISyntaxException {
87         Map<String, String> properties = Collections.emptyMap();
88         NonConfigResource testedObject = new NonConfigResource(properties::get);
89         testedObject.add(sampleUrlResource);
90         Path thisFilePath = testedObject.locate(RESOURCE_NAME);
91         assertEquals(0, thisFilePath.compareTo(Paths.get(sampleUrlResource.toURI())));
92     }
93
94     @Test
95     public void testShouldNotLocateResource2() {
96         String badResource = "noexistingresource";
97         String badPath = "noexistingpath";
98
99         Map<String, String> properties = new HashMap<>();
100         NonConfigResource testedObject = new NonConfigResource(properties::get);
101         // empty properties and bad resource
102         Path thisFilePath = testedObject.locate(badResource);
103
104         properties.put(NODE_CONFIG_LOCATION, badPath);
105         // bad path in properties and bad resource
106         Path thisFilePath2 = testedObject.locate(badResource);
107
108         assertNull(thisFilePath);
109         assertNull(thisFilePath2);
110     }
111 }