Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / lur / test / JU_ConfigPrincipal.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.cadi.lur.test;
24
25 import org.junit.*;
26 import static org.junit.Assert.*;
27 import static org.hamcrest.CoreMatchers.*;
28 import java.lang.reflect.Field;
29
30 import java.io.IOException;
31
32 import org.onap.aaf.cadi.lur.ConfigPrincipal;
33
34 public class JU_ConfigPrincipal {
35
36     private final String name = "User";
37     private final String pass = "pass";
38
39     // Expected output of base64("User:pass")
40     private final String b64encoded = "VXNlcjpwYXNz";
41
42     private Field content_field;
43
44     @Before
45     public void setup() throws NoSuchFieldException {
46         content_field = ConfigPrincipal.class.getDeclaredField("content");
47         content_field.setAccessible(true);
48     }
49
50     @Test
51     public void testConfigPrincipalStringString() throws IOException, IllegalArgumentException, IllegalAccessException {
52         ConfigPrincipal p =  new ConfigPrincipal(name, pass);
53
54         assertThat(p.getName(), is(name));
55         assertThat(p.toString(), is(name));
56         assertThat(p.getCred(), is(pass.getBytes()));
57         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
58         content_field.set(p, "pass");
59         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
60
61         // One more time for coverage purposes
62         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
63     }
64
65     @Test
66     public void testConfigPrincipalStringByteArray() throws IOException, IllegalArgumentException, IllegalAccessException {
67         ConfigPrincipal p =  new ConfigPrincipal(name, pass.getBytes());
68
69         assertThat(p.getName(), is(name));
70         assertThat(p.toString(), is(name));
71         assertThat(p.getCred(), is(pass.getBytes()));
72         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
73         content_field.set(p, "pass");
74         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
75
76         // One more time for coverage purposes
77         assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded));
78     }
79
80 }