2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.taf.test;
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;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
32 import java.net.URISyntaxException;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
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;
55 public class JU_HttpEpiTaf {
57 private PropAccess access;
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;
68 public void setup() throws URISyntaxException {
69 MockitoAnnotations.initMocks(this);
71 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
75 public void test() throws Exception {
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"));
84 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, new NullTaf());
85 taf.validate(LifeForm.CBLF, reqMock, respMock);
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);
94 access.setLogLevel(Level.DEBUG);
95 taf.validate(LifeForm.CBLF, reqMock, respMock);
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);
102 when(trespMock.isAuthenticated()).thenReturn(RESP.IS_AUTHENTICATED);
103 taf.validate(LifeForm.CBLF, reqMock, respMock);
105 when(trespMock.isAuthenticated()).thenReturn(RESP.TRY_AUTHENTICATING);
106 taf.validate(LifeForm.CBLF, reqMock, respMock);
108 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock, tafMock);
109 taf.validate(LifeForm.CBLF, reqMock, respMock);
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);
115 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock, tafMock);
116 taf.validate(LifeForm.CBLF, reqMock, respMock);
118 taf = new HttpEpiTaf(access, locMock, trustCheckerMock, tafMock);
119 taf.validate(LifeForm.CBLF, reqMock, respMock);
121 taf = new HttpEpiTaf(access, locMock, null, tafMock);
122 when(redirMock.isAuthenticated()).thenReturn(RESP.IS_AUTHENTICATED);
124 taf.validate(LifeForm.CBLF, reqMock, respMock);
125 fail("Should've thrown an exception");
126 } catch (Exception e) {
129 assertThat(taf.revalidate(null), is(false));
130 assertThat(taf.revalidate(null), is(false));
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));
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));