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