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