System dependent separators in JU tests
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_GetAccess.java
1 /*******************************************************************************
2  * * org.onap.aaf
3  * * ===========================================================================
4  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  * * ===========================================================================
6  * * Licensed under the Apache License, Version 2.0 (the "License");
7  * * you may not use this file except in compliance with the License.
8  * * You may obtain a copy of the License at
9  * *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  * *
12  *  * Unless required by applicable law or agreed to in writing, software
13  * * distributed under the License is distributed on an "AS IS" BASIS,
14  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * * See the License for the specific language governing permissions and
16  * * limitations under the License.
17  * * ============LICENSE_END====================================================
18  * *
19  * *
20  ******************************************************************************/
21
22 package org.onap.aaf.cadi.config.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import org.junit.*;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintStream;
32
33 import org.onap.aaf.cadi.PropAccess;
34 import org.onap.aaf.cadi.config.Get;
35 import org.onap.aaf.cadi.config.GetAccess;
36
37 public class JU_GetAccess {
38
39         private String defaultVal = "some default value";
40
41         private ByteArrayOutputStream outStream;
42
43         private PropAccess access;
44         private Get.AccessGet accessGet;
45         private File file;
46         private String filePath;
47
48         @Before
49         public void setup() throws IOException {
50                 outStream = new ByteArrayOutputStream();
51                 System.setOut(new PrintStream(outStream));
52
53                 file = File.createTempFile("GetAccess_test", "");
54                 filePath = file.getAbsolutePath();
55
56                 access = new PropAccess();
57         access.setProperty("cadi_prop_files", filePath);
58                 accessGet = new Get.AccessGet(access);
59
60         }
61
62         @After
63         public void tearDown() {
64                 System.setOut(System.out);
65
66                 file.delete();
67         }
68
69     @Test
70     public void constructorTest() {
71         String output;
72
73         @SuppressWarnings("unused")
74                 GetAccess getAccess = new GetAccess(accessGet);
75                 String[] lines = outStream.toString().split(System.lineSeparator());
76                 assertThat(lines.length, is(2));
77         output = lines[0].split(" ", 2)[1];
78         assertThat(output, is("INIT [cadi] cadi_prop_files is set to " + filePath));
79                 output = lines[1].split(" ", 2)[1];
80         assertThat(output, is("INIT [cadi] Loading CADI Properties from " + filePath));
81         }
82
83     @Test
84     public void getPropertyTest1() {
85         GetAccess getAccess = new GetAccess(accessGet);
86
87                 getAccess.setProperty("tag", "value");
88                 assertThat(getAccess.getProperty("tag", defaultVal), is("value"));
89                 assertThat(getAccess.getProperty("not_a_tag", defaultVal), is(defaultVal));
90         }
91
92     @Test
93     public void getPropertyTest2() {
94         GetAccess getAccess = new GetAccess(accessGet);
95
96                 getAccess.setProperty("tag", "value");
97                 assertThat(getAccess.getProperty("tag"), is("value"));
98                 assertThat(getAccess.getProperty("not_a_tag"), is(nullValue()));
99         }
100
101         @Test
102         public void getTest() {
103         GetAccess getAccess = new GetAccess(accessGet);
104                 assertThat((Get.AccessGet)getAccess.get(), is(accessGet));
105         }
106
107 }