1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.config.test;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.net.HttpURLConnection;
32 import java.net.MalformedURLException;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.SecuritySetter;
39 import org.onap.aaf.cadi.config.SecurityInfoC;
41 public class JU_SecurityInfoC {
43 ByteArrayOutputStream outStream;
44 ByteArrayOutputStream errStream;
48 outStream = new ByteArrayOutputStream();
49 errStream = new ByteArrayOutputStream();
51 System.setOut(new PrintStream(outStream));
52 System.setErr(new PrintStream(errStream));
56 public void tearDown() {
57 System.setOut(System.out);
58 System.setErr(System.err);
62 // public void instanceTest() throws CadiException, MalformedURLException {
63 // SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class );
64 // assertThat(si.defSS.getID(), is(SecurityInfoC.DEF_ID));
66 // si.defSS.setSecurity(new HttpURLConnectionStub());
67 // fail("Should have thrown an exception");
68 // } catch (CadiException e) {
69 // assertTrue(e instanceof CadiException);
70 // assertThat(e.getMessage(), is("No Client Credentials set."));
72 // assertThat(si.defSS.setLastResponse(0), is(0));
74 // // Try it again for coverage
75 // SecurityInfoC<HttpURLConnection> siClone = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class);
76 // assertThat(siClone, is(si));
80 public void setTest() throws MalformedURLException, CadiException {
81 SecurityInfoC<HttpURLConnectionStub> si = SecurityInfoC.instance(new PropAccess(), HttpURLConnectionStub.class);
82 SecuritySetter<HttpURLConnectionStub> ss = new SecuritySetterStub<HttpURLConnectionStub>();
83 assertThat(si.set(ss), is(si));
84 assertThat(si.defSS.getID(), is("Example ID"));
86 si.defSS.setSecurity(new HttpURLConnectionStub());
87 fail("Should have thrown an exception");
88 } catch (CadiException e) {
89 assertTrue(e instanceof CadiException);
90 assertThat(e.getMessage(), is("Example exception"));
92 assertThat(si.defSS.setLastResponse(0), is(0));
93 assertThat(si.defSS.setLastResponse(1), is(1));
94 assertThat(si.defSS.setLastResponse(-1), is(-1));
97 public static class HttpURLConnectionStub extends HttpURLConnection {
98 public HttpURLConnectionStub() throws MalformedURLException { super(new URL("http://www.example.com")); }
99 @Override public void disconnect() { }
100 @Override public boolean usingProxy() { return false; }
101 @Override public void connect() throws IOException { }
104 private class SecuritySetterStub<CT> implements SecuritySetter<CT> {
105 public String getID() { return "Example ID"; }
106 public void setSecurity(CT client) throws CadiException { throw new CadiException("Example exception"); }
107 public int setLastResponse(int respCode) { return respCode; }