93a20474af62d08505b76a4919d22fe683d40f0c
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / taf / test / JU_HttpEpiTaf.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.taf.test;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.when;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aaf.cadi.Access.Level;
42 import org.onap.aaf.cadi.CachedPrincipal.Resp;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.Locator;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.Taf.LifeForm;
47 import org.onap.aaf.cadi.TrustChecker;
48 import org.onap.aaf.cadi.taf.HttpEpiTaf;
49 import org.onap.aaf.cadi.taf.HttpTaf;
50 import org.onap.aaf.cadi.taf.NullTaf;
51 import org.onap.aaf.cadi.taf.Redirectable;
52 import org.onap.aaf.cadi.taf.TafResp;
53 import org.onap.aaf.cadi.taf.TafResp.RESP;
54
55 public class JU_HttpEpiTaf {
56
57         private PropAccess access;
58
59         @Mock private Locator<URI> locMock;
60         @Mock private TrustChecker trustCheckerMock;
61         @Mock private HttpServletRequest reqMock;
62         @Mock private HttpServletResponse respMock;
63         @Mock private HttpTaf tafMock;
64         @Mock private TafResp trespMock;
65         @Mock private Redirectable redirMock;
66
67         @Before
68         public void setup() throws URISyntaxException {
69                 MockitoAnnotations.initMocks(this);
70
71                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
72         }
73
74         @Test
75         public void test() throws Exception {
76                 HttpEpiTaf taf;
77                 try {
78                         taf = new HttpEpiTaf(access, locMock, trustCheckerMock);
79                         fail("Should've thrown an exception");
80                 } catch (CadiException e) {
81                         assertThat(e.getMessage(), is("Need at least one HttpTaf implementation in constructor"));
82                 }
83
84                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, new NullTaf());
85                 taf.validate(LifeForm.CBLF, reqMock, respMock);
86
87                 // Coverage of tricorderScan
88                 taf.validate(LifeForm.LFN, reqMock, respMock);
89                 when(reqMock.getHeader("User-Agent")).thenReturn("Non-mozilla-header");
90                 taf.validate(LifeForm.LFN, reqMock, respMock);
91                 when(reqMock.getHeader("User-Agent")).thenReturn("Mozilla-header");
92                 taf.validate(LifeForm.LFN, reqMock, respMock);
93
94                 access.setLogLevel(Level.DEBUG);
95                 taf.validate(LifeForm.CBLF, reqMock, respMock);
96
97                 when(tafMock.validate(LifeForm.CBLF, reqMock, respMock)).thenReturn(trespMock);
98                 when(trespMock.isAuthenticated()).thenReturn(RESP.TRY_ANOTHER_TAF);
99                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock);
100                 taf.validate(LifeForm.CBLF, reqMock, respMock);
101
102                 when(trespMock.isAuthenticated()).thenReturn(RESP.IS_AUTHENTICATED);
103                 taf.validate(LifeForm.CBLF, reqMock, respMock);
104
105                 when(trespMock.isAuthenticated()).thenReturn(RESP.TRY_AUTHENTICATING);
106                 taf.validate(LifeForm.CBLF, reqMock, respMock);
107
108                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock, tafMock);
109                 taf.validate(LifeForm.CBLF, reqMock, respMock);
110
111                 when(tafMock.validate(LifeForm.CBLF, reqMock, respMock)).thenReturn(redirMock);
112                 when(redirMock.isAuthenticated()).thenReturn(RESP.TRY_AUTHENTICATING);
113                 taf.validate(LifeForm.CBLF, reqMock, respMock);
114
115                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock, tafMock);
116                 taf.validate(LifeForm.CBLF, reqMock, respMock);
117
118                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock);
119                 taf.validate(LifeForm.CBLF, reqMock, respMock);
120
121                 taf = new HttpEpiTaf(access, locMock, null, tafMock);
122                 when(redirMock.isAuthenticated()).thenReturn(RESP.IS_AUTHENTICATED);
123                 try {
124                         taf.validate(LifeForm.CBLF, reqMock, respMock);
125                         fail("Should've thrown an exception");
126                 } catch (Exception e) {
127                 }
128
129                 assertThat(taf.revalidate(null), is(false));
130                 assertThat(taf.revalidate(null), is(false));
131
132                 when(tafMock.revalidate(null, null)).thenReturn(Resp.NOT_MINE);
133                 assertThat(taf.revalidate(null, null), is(Resp.NOT_MINE));
134                 when(tafMock.revalidate(null, null)).thenReturn(Resp.REVALIDATED);
135                 assertThat(taf.revalidate(null, null), is(Resp.REVALIDATED));
136
137                 when(tafMock.revalidate(null, null)).thenReturn(Resp.NOT_MINE).thenReturn(Resp.NOT_MINE).thenReturn(Resp.REVALIDATED);
138                 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock, tafMock, tafMock);
139                 assertThat(taf.revalidate(null, null), is(Resp.REVALIDATED));
140
141                 taf.toString();
142
143         }
144
145 }