2 * Copyright © 2018 Nokia
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.config;
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;
25 import java.net.URISyntaxException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Collections;
30 import java.util.HashMap;
32 import org.junit.Test;
34 public class NonConfigResourceTest {
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);
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()));
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()));
58 public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() {
59 final String path = new File(sampleResourcePath).getParentFile().getPath();
60 Map<String, String> properties = Collections.singletonMap(NODE_CONFIG_LOCATION, path);
62 NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
63 Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
65 NonConfigResource systemNonConfigResource = new NonConfigResource(System.getProperties()::getProperty);
66 System.setProperty(NODE_CONFIG_LOCATION, path);
67 Path systemFilePath = systemNonConfigResource.locate(RESOURCE_NAME);
69 Path testFilePath = sampleResourceFile.toPath();
70 assertEquals(0, thisFilePath.compareTo(testFilePath));
71 assertEquals(0, systemFilePath.compareTo(testFilePath));
75 public void testShouldLocateResourceWhenConfigPropertyIsSet2() {
77 Map<String, String> properties = Collections.singletonMap(
78 CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
80 NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
81 Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
82 assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath()));
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())));
95 public void testShouldNotLocateResource2() {
96 String badResource = "noexistingresource";
97 String badPath = "noexistingpath";
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);
104 properties.put(NODE_CONFIG_LOCATION, badPath);
105 // bad path in properties and bad resource
106 Path thisFilePath2 = testedObject.locate(badResource);
108 assertNull(thisFilePath);
109 assertNull(thisFilePath2);