Forward test output to console on demand
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / type / NonConfigResourceTest.java
1 package org.onap.config.type;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.junit.Test;
5 import org.onap.config.NonConfigResource;
6
7 import java.io.File;
8 import java.net.MalformedURLException;
9 import java.net.URISyntaxException;
10 import java.net.URL;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
13 import java.util.Map;
14
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17
18 public class NonConfigResourceTest {
19
20     private final URL sampleUrlResource = NonConfigResourceTest.class.getResource("NonConfigResourceTest.class");
21     private final String sampleResourcePath = sampleUrlResource.getPath();
22     private final File sampleResourceFile = new File(sampleResourcePath);
23
24     @Test
25     public void testShouldLocateResourceWhenAbsPathProvided2() {
26         Map<String, String> properties = ImmutableMap.of();
27         Path actualResourcePath = NonConfigResource.create(properties::get).locate(sampleResourceFile.toString());
28
29         assertTrue(actualResourcePath.compareTo(sampleResourceFile.toPath()) == 0);
30     }
31
32     @Test
33     public void testShouldLocateResourceWhenPresentInFiles2() {
34         Map<String, String> properties = ImmutableMap.of();
35         NonConfigResource testedObject = NonConfigResource.create(properties::get);
36         testedObject.add(sampleResourceFile);
37
38         Path thisFilePath = testedObject.locate("NonConfigResourceTest.class");
39
40         assertTrue(thisFilePath.compareTo(sampleResourceFile.toPath()) == 0);
41     }
42
43     @Test
44     public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() throws URISyntaxException, MalformedURLException {
45         Map<String, String> properties = ImmutableMap.of("node.config.location", new File(sampleResourcePath).getParentFile().getPath());
46         NonConfigResource testedNonConfigResource = NonConfigResource.create(properties::get);
47         System.getProperties().setProperty("node.config.location", new File(sampleResourcePath).getParentFile().getPath());
48         Path thisFilePath = testedNonConfigResource.locate("NonConfigResourceTest.class");
49
50         assertTrue(thisFilePath.compareTo(new File(sampleResourcePath).toPath()) == 0);
51     }
52
53     @Test
54     public void testShouldLocateResourceWhenConfigPropertyIsSet2() {
55         Map<String, String> properties = ImmutableMap.of("config.location", new File(sampleResourcePath).getParentFile().getPath());
56         NonConfigResource testedNonConfigResource = NonConfigResource.create(properties::get);
57         Path thisFilePath = testedNonConfigResource.locate("NonConfigResourceTest.class");
58
59         assertTrue(thisFilePath.compareTo(new File(sampleResourcePath).toPath()) == 0);
60     }
61
62     @Test
63     public void testShouldLocatePathWhenResourcePresentInUrls2() throws URISyntaxException {
64         Map<String, String> properties = ImmutableMap.of();
65         NonConfigResource testedObject = NonConfigResource.create(properties::get);
66         testedObject.add(sampleUrlResource);
67
68         Path thisFilePath = testedObject.locate("NonConfigResourceTest.class");
69
70         assertTrue(thisFilePath.compareTo(Paths.get(sampleUrlResource.toURI())) == 0);
71     }
72
73     @Test
74     public void testShouldNotLocateResource2() throws URISyntaxException {
75         Map<String, String> properties = ImmutableMap.of();
76         NonConfigResource testedObject = NonConfigResource.create(properties::get);
77
78         Path thisFilePath = testedObject.locate("nonexistingresource");
79
80         assertNull(thisFilePath);
81     }
82 }