ab6a39c02ae141f594fa665345d717fe51be09e9
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / http / test / JU_HRcli.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
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.http.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.net.HttpURLConnection;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33
34 import org.junit.*;
35 import org.mockito.*;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.Locator;
38 import org.onap.aaf.cadi.Locator.Item;
39 import org.onap.aaf.cadi.LocatorException;
40 import org.onap.aaf.cadi.PropAccess;
41 import org.onap.aaf.cadi.SecuritySetter;
42 import org.onap.aaf.cadi.client.EClient;
43 import org.onap.aaf.cadi.http.HMangr;
44 import org.onap.aaf.cadi.http.HRcli;
45
46 public class JU_HRcli {
47     
48     @Mock
49     SecuritySetter<HttpURLConnection> ssMock;
50     
51     @Mock
52     Locator<URI> locMock;
53     
54     @Mock
55     Locator.Item itemMock;
56     
57     private HMangr hman;
58     private PropAccess access;
59     private static URI uri;
60     
61     private static final String uriString = "example.com";
62     
63     @Before
64     public void setup() throws LocatorException, URISyntaxException {
65         MockitoAnnotations.initMocks(this);
66
67         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
68         hman = new HMangr(access, locMock);
69         uri = new URI(uriString);
70
71         when(locMock.get(itemMock)).thenReturn(uri);
72     }
73
74     @Test(expected = CadiException.class)
75     public void publicInterfaceTest() throws URISyntaxException, LocatorException, CadiException {
76         HRcli hrcli = new HRcli(hman, itemMock, ssMock);
77         assertThat(hrcli.setManager(hman), is(hrcli));
78         assertThat(hrcli.toString(), is(uriString));
79
80         hrcli.setSecuritySetter(ssMock);
81         assertThat(hrcli.getSecuritySetter(), is(ssMock));
82         
83         // No throw
84         hrcli.invalidate();
85         // Throw
86         doThrow(CadiException.class).when(locMock).invalidate(itemMock);
87         hrcli.invalidate();
88     }
89     
90     @Test(expected = CadiException.class)
91     public void protectedInterfaceTest() throws CadiException, LocatorException {
92         HRcliStub hrcli = new HRcliStub(hman, uri, itemMock, ssMock);
93         HRcli clone = hrcli.clone(uri, ssMock);
94         assertThat(clone.toString(), is(hrcli.toString()));
95         
96         EClient<HttpURLConnection> eclient = hrcli.client();
97         assertThat(eclient, is(not(nullValue())));
98
99         hrcli = new HRcliStub(hman, null, itemMock, ssMock);
100         when(locMock.best()).thenReturn(itemMock);
101         eclient = hrcli.client();
102         assertThat(eclient, is(not(nullValue())));
103
104         hrcli = new HRcliStub(hman, null, itemMock, ssMock);
105         when(locMock.best()).thenReturn(null);
106         eclient = hrcli.client();
107     }
108     
109     private class HRcliStub extends HRcli {
110         public HRcliStub(HMangr hman, URI uri, Item locItem, SecuritySetter<HttpURLConnection> secSet) {
111             super(hman, uri, locItem, secSet);
112         }
113         public HRcli clone(URI uri, SecuritySetter<HttpURLConnection> ss) {
114             return super.clone(uri, ss);
115         }
116         public EClient<HttpURLConnection> client() throws CadiException {
117             return super.client();
118         }
119     }
120
121 }