Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_SecurityInfoC.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
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
23 package org.onap.aaf.cadi.config.test;
24
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
27
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;
33 import java.net.URL;
34
35 import org.junit.*;
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;
40
41 public class JU_SecurityInfoC {
42
43     ByteArrayOutputStream outStream;
44     ByteArrayOutputStream errStream;
45
46     @Before
47     public void setup() {
48         outStream = new ByteArrayOutputStream();
49         errStream = new ByteArrayOutputStream();
50
51         System.setOut(new PrintStream(outStream));
52         System.setErr(new PrintStream(errStream));
53     }
54
55     @After
56     public void tearDown() {
57         System.setOut(System.out);
58         System.setErr(System.err);
59     }
60
61 //    @Test
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));
65 //        try {
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."));
71 //        }
72 //        assertThat(si.defSS.setLastResponse(0), is(0));
73 //
74 //        // Try it again for coverage
75 //        SecurityInfoC<HttpURLConnection> siClone = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class);
76 //        assertThat(siClone, is(si));
77 //    }
78
79     @Test
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"));
85         try {
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"));
91         }
92         assertThat(si.defSS.setLastResponse(0), is(0));
93         assertThat(si.defSS.setLastResponse(1), is(1));
94         assertThat(si.defSS.setLastResponse(-1), is(-1));
95     }
96
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 { }
102     }
103
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; }
108     }
109
110 }