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;
31 import org.junit.Test;
33 public class NonConfigResourceTest {
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);
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()));
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()));
57 public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() {
59 Map<String, String> properties = Collections.singletonMap(
60 NODE_CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
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()));
70 public void testShouldLocateResourceWhenConfigPropertyIsSet2() {
72 Map<String, String> properties = Collections.singletonMap(
73 CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath());
75 NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get);
76 Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME);
77 assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath()));
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())));
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);