Imporve coverage of cadi-aaf
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / cert / test / JU_AAFListedCertIdentity.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 package org.onap.aaf.cadi.aaf.cert.test;
23
24 import static org.mockito.Mockito.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29 import org.mockito.*;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Modifier;
35 import java.net.HttpURLConnection;
36 import java.security.cert.CertificateException;
37 import java.security.cert.X509Certificate;
38 import java.util.ArrayList;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Set;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.onap.aaf.cadi.CadiException;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.aaf.cert.AAFListedCertIdentity;
48 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
49 import org.onap.aaf.cadi.client.Future;
50 import org.onap.aaf.cadi.client.Rcli;
51 import org.onap.aaf.cadi.config.Config;
52 import org.onap.aaf.misc.env.APIException;
53 import org.onap.aaf.misc.env.util.Chrono;
54 import org.onap.aaf.misc.rosetta.env.RosettaDF;
55
56 import aaf.v2_0.Certs;
57 import aaf.v2_0.Certs.Cert;
58 import aaf.v2_0.Users;
59 import aaf.v2_0.Users.User;
60
61 public class JU_AAFListedCertIdentity {
62
63         @Mock private AAFCon<?> conMock;
64         @Mock private Rcli<Object> rcliMock;
65         @Mock private RosettaDF<Users> userDFMock;
66         @Mock private RosettaDF<Certs> certDFMock;
67         @Mock private Future<Users> futureUsersMock;
68         @Mock private Future<Certs> futureCertsMock;
69
70         @Mock private Users usersMock;
71         @Mock private User userMock1;
72         @Mock private User userMock2;
73         @Mock private User userMock3;
74
75         @Mock private Certs certsMock;
76         @Mock private Cert certMock1;
77         @Mock private Cert certMock2;
78         @Mock private Cert certMock3;
79
80         @Mock private HttpServletRequest reqMock;
81         @Mock private X509Certificate x509Mock;
82
83         private List<User> usersList;
84         private List<Cert> certsList;
85
86         private PropAccess access;
87
88         private ByteArrayOutputStream outStream;
89
90         private static final String USERS = "user1,user2,user3";
91         private static final String ID = "id";
92         private static final String FINGERPRINT = "fingerprint";
93
94         private static final byte[] certBytes = "certificate".getBytes();
95
96         @Before
97         public void setup() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
98                 MockitoAnnotations.initMocks(this);
99
100                 certsList = new ArrayList<>();
101                 certsList.add(certMock1);
102                 certsList.add(certMock2);
103                 certsList.add(certMock3);
104
105                 usersList = new ArrayList<>();
106                 usersList.add(userMock1);
107                 usersList.add(userMock2);
108                 usersList.add(userMock3);
109
110                 outStream = new ByteArrayOutputStream();
111                 access = new PropAccess(new PrintStream(outStream), new String[0]);
112                 outStream.reset();
113                 access.setProperty(Config.AAF_CERT_IDS, USERS);
114                 setFinal(conMock, conMock.getClass().getField("usersDF"), userDFMock);
115                 setFinal(conMock, conMock.getClass().getField("certsDF"), certDFMock);
116                 setFinal(conMock, conMock.getClass().getField("access"), access);
117         }
118
119         @Test
120         public void test() throws APIException, CadiException, CertificateException {
121                 doReturn(rcliMock).when(conMock).client(Config.AAF_DEFAULT_VERSION);
122                 when(rcliMock.read("/authz/users/perm/com.att.aaf.trust/tguard/authenticate", Users.class, userDFMock)).thenReturn(futureUsersMock);
123                 when(rcliMock.read("/authz/users/perm/com.att.aaf.trust/basicAuth/authenticate", Users.class, userDFMock)).thenReturn(futureUsersMock);
124                 when(rcliMock.read("/authz/users/perm/com.att.aaf.trust/csp/authenticate", Users.class, userDFMock)).thenReturn(futureUsersMock);
125
126                 when(futureUsersMock.get(5000)).thenReturn(true);
127                 futureUsersMock.value = usersMock;
128                 when(usersMock.getUser()).thenReturn(usersList);
129
130                 when(rcliMock.read("/authn/cert/id/user1", Certs.class, conMock.certsDF)).thenReturn(futureCertsMock);
131                 when(rcliMock.read("/authn/cert/id/user2", Certs.class, conMock.certsDF)).thenReturn(futureCertsMock);
132                 when(rcliMock.read("/authn/cert/id/user3", Certs.class, conMock.certsDF)).thenReturn(futureCertsMock);
133
134                 when(futureCertsMock.get(5000)).thenReturn(true);
135                 futureCertsMock.value = certsMock;
136                 when(certsMock.getCert()).thenReturn(certsList);
137
138                 when(userMock1.getId()).thenReturn("user1");
139                 when(userMock2.getId()).thenReturn("user2");
140                 when(userMock3.getId()).thenReturn("user3");
141
142                 prepareCert(certMock1);
143                 prepareCert(certMock2);
144                 prepareCert(certMock3);
145
146                 AAFListedCertIdentity certID = new AAFListedCertIdentity(access, conMock);
147
148                 when(x509Mock.getEncoded()).thenReturn(certBytes);
149                 certID.identity(reqMock, null, null);
150                 certID.identity(reqMock, null, certBytes);
151                 certID.identity(reqMock, x509Mock, null);
152                 certID.identity(reqMock, x509Mock, certBytes);
153
154                 Set<String> hashSetOfUsers = AAFListedCertIdentity.trusted("basicAuth");
155                 assertThat(hashSetOfUsers.contains("user1"), is(true));
156                 assertThat(hashSetOfUsers.contains("user2"), is(true));
157                 assertThat(hashSetOfUsers.contains("user3"), is(true));
158
159         }
160
161         private void setFinal(Object object, Field field, Object newValue) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
162                 field.setAccessible(true);
163
164                 Field modifiersField = Field.class.getDeclaredField("modifiers");
165                 modifiersField.setAccessible(true);
166                 modifiersField.setInt(field, field.getModifiers() & Modifier.FINAL);
167
168                 field.set(object, newValue);
169         }
170
171         private void prepareCert(Cert cert) {
172                 Date date = new Date();
173                 when(cert.getExpires()).thenReturn(Chrono.timeStamp(new Date(date.getTime() + (60 * 60 * 24))));
174                 when(cert.getId()).thenReturn(ID);
175                 when(cert.getFingerprint()).thenReturn(FINGERPRINT.getBytes());
176         }
177
178 }