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.client.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.client.AbsAuthentication;
29 import org.onap.aaf.cadi.config.SecurityInfoC;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.net.HttpURLConnection;
36 public class JU_AbsAuthentication {
38 private final static String ID = "id";
39 private final static String PASSWORD = "password";
40 private final static String WARNING = "Your service has 1000 consecutive bad service " +
41 "logins to AAF. AAF Access will be disabled after 10000\n";
43 private static ByteArrayOutputStream errStream;
47 errStream = new ByteArrayOutputStream();
48 System.setErr(new PrintStream(errStream));
52 public void tearDown() {
53 System.setErr(System.err);
57 public void test() throws IOException, InterruptedException {
58 AuthStub stub = new AuthStub(null, null, null);
59 assertThat(stub.getID(), is(nullValue()));
60 assertThat(stub.headValue(), is(""));
61 assertThat(stub.count(), is(0));
64 assertThat(stub.getID(), is(ID));
66 stub = new AuthStub(null, ID, PASSWORD.getBytes());
67 assertThat(stub.getID(), is(ID));
68 assertThat(stub.headValue(), is(PASSWORD));
69 assertThat(stub.count(), is(0));
71 assertThat(stub.setLastResponse(200), is(0));
72 assertThat(stub.isDenied(), is(false));
74 for (int i = 1; i <= 10; i++) {
75 assertThat(stub.setLastResponse(401), is(i));
76 assertThat(stub.isDenied(), is(false));
78 assertThat(stub.setLastResponse(401), is(11));
79 assertThat(stub.isDenied(), is(true));
82 assertThat(stub.setLastResponse(401), is(1000));
83 assertThat(errStream.toString(), is(WARNING));
87 assertThat(stub.isDenied(), is(false));
90 private class AuthStub extends AbsAuthentication<HttpURLConnection> {
92 public AuthStub(SecurityInfoC<HttpURLConnection> securityInfo, String user, byte[] headValue)
93 throws IOException { super(securityInfo, user, headValue); }
95 @Override public void setSecurity(HttpURLConnection client) throws CadiException { }
96 @Override public void setUser(String id) { super.setUser(id); }
97 @Override public String headValue() throws IOException { return super.headValue(); }
99 public void setLastMiss(long lastMiss) { this.lastMiss = lastMiss; }
100 public void setCount(int count) { this.count = count; }