Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / env / test / JU_AuthzEnv.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22
23 package org.onap.aaf.auth.env.test;
24
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.mock;
27 import org.junit.*;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.PrintStream;
33 import java.util.Properties;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.aaf.auth.env.AuthzEnv;
39 import org.onap.aaf.cadi.Access;
40 import org.onap.aaf.cadi.PropAccess;
41 import org.onap.aaf.cadi.config.Config;
42
43 public class JU_AuthzEnv {
44
45     AuthzEnv authzEnv;
46     ByteArrayOutputStream outStream;
47     ByteArrayOutputStream errStream;
48     enum Level {DEBUG, INFO, AUDIT, INIT, WARN, ERROR};
49
50     @Before
51     public void setUp() {
52         outStream = new ByteArrayOutputStream();
53         errStream = new ByteArrayOutputStream();
54
55         System.setOut(new PrintStream(outStream));
56         System.setErr(new PrintStream(errStream));
57
58         authzEnv = new AuthzEnv();
59     }
60
61     @After
62     public void tearDown() {
63         System.setOut(System.out);
64         System.setErr(System.err);
65     }
66
67     @Test
68     @SuppressWarnings("unused")
69     public void testConstructors() {
70         AuthzEnv authzEnv1 = new AuthzEnv("Test");
71         AuthzEnv authzEnv2 = new AuthzEnv((PropAccess)null);
72         AuthzEnv authzEnv3 = new AuthzEnv((Properties)null);
73     }
74
75     @Test
76     public void testTransRate() {
77         Long Result = authzEnv.transRate();
78         assertNotNull(Result);
79     }
80
81     @Test
82     public void checkNewTransNoAvg() {
83         assertNotNull(authzEnv.newTransNoAvg());
84     }
85
86     @Test
87     public void checkNewTrans() {
88         assertNotNull(authzEnv.newTrans());
89     }
90
91     @Test
92     public void checkPropAccess() {
93         assertNotNull(authzEnv.access());
94     }
95
96     @Test
97     public void checkgetProperties() { //TODO:[GABE]No setter for this, add?
98         assertNotNull(authzEnv.getProperties());
99         assertNotNull(authzEnv.getProperties("test"));
100     }
101
102     @Test
103     public void checkPropertyGetters(){
104         authzEnv.setProperty("key","value");
105         assertEquals(authzEnv.getProperty("key"), "value");
106         assertEquals(authzEnv.getProperty("key","value"), "value");
107     }
108
109     @Test
110     public void checkPropertySetters(){
111         assertEquals(authzEnv.getProperty("key","value"), authzEnv.setProperty("key","value"));
112     }
113
114     @Test(expected = IOException.class)
115     public void testDecryptException() throws IOException{
116         authzEnv.setProperty(Config.CADI_KEYFILE, "test/keyfile");
117         authzEnv.decrypt(null, false);
118     }
119
120     @Test
121     public void testDecrypt() throws IOException{
122         String encrypted = "encrypted";
123         String Result = authzEnv.decrypt(encrypted, true);
124         assertEquals("encrypted",Result);
125     }
126
127     @Test
128     public void testClassLoader() {
129         ClassLoader cLoad = mock(ClassLoader.class);
130         cLoad = authzEnv.classLoader();
131         assertNotNull(cLoad);
132     }
133
134     @Test
135     public void testLoad() throws IOException {
136         InputStream is = mock(InputStream.class);
137         authzEnv.load(is);
138     }
139
140     @Test
141     public void testLog() {
142         Access.Level lvl = Access.Level.DEBUG;
143         Object msgs = null;
144         authzEnv.log(lvl, msgs);
145     }
146
147     @Test
148     public void testLog1() {
149
150         Exception e = new Exception();
151         Object msgs = null;
152         authzEnv.log(e, msgs);
153     }
154
155     @Test
156     public void testPrintf() {
157         Access.Level lvl = Access.Level.DEBUG;
158         Object msgs = null;
159         authzEnv.printf(lvl, "Test", msgs);
160     }
161
162     @Test
163     public void testWillLog() {
164         Access.Level lvl = Access.Level.DEBUG;
165         Access.Level lvl1 = Access.Level.AUDIT;
166         boolean test = authzEnv.willLog(lvl);
167         assertFalse(test);
168         test = authzEnv.willLog(lvl1);
169         assertTrue(test);
170     }
171
172     @Test
173     public void testSetLogLevel() {
174         Access.Level lvl = Access.Level.DEBUG;
175         authzEnv.setLogLevel(lvl);
176     }
177
178 }