Improve coverage of cadi-client
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / locator / test / JU_HClientHotPeerLocator.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.locator.test;
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import org.junit.*;
28 import org.mockito.*;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.PrintStream;
32
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Locator;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.http.HClient;
38 import org.onap.aaf.cadi.http.HX509SS;
39 import org.onap.aaf.cadi.locator.HClientHotPeerLocator;
40
41 public class JU_HClientHotPeerLocator {
42
43         @Mock private HX509SS ssMock;
44
45         private PropAccess access;
46         private ByteArrayOutputStream outStream;
47
48         // Note: - The IP and port are irrelevant for these tests
49         private static final String goodURL1 = "fakeIP1:fakePort1/38/-90";  // Approx St Louis
50         private static final String goodURL2 = "fakeIP2:fakePort2/33/-96";  // Approx Dallas
51         private static final String badURL = "~%$!@#$//";
52
53         @Before
54         public void setup() {
55                 MockitoAnnotations.initMocks(this);
56
57                 outStream = new ByteArrayOutputStream();
58                 access = new PropAccess(new PrintStream(outStream), new String[0]);
59         }
60
61         @Test
62         public void test() throws LocatorException {
63                 HClientHotPeerLocator loc;
64                 String urlStr = goodURL1 + ',' + goodURL2;
65                 loc = new HClientHotPeerLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
66                 assertThat(loc.hasItems(), is(true));
67
68                 String[] messages = outStream.toString().split("\n");
69                 String preffered = messages[0].split(" ", 4)[3];
70                 String alternate = messages[1].split(" ", 4)[3];
71                 assertThat(preffered, is("Preferred Client is " + goodURL1));
72                 assertThat(alternate, is("Alternate Client is " + goodURL2));
73
74                 HClient firstClient = loc.get(loc.first());
75                 HClient bestClient = loc.bestClient();
76                 assertThat(bestClient, is(firstClient));
77
78                 Locator.Item item = loc.first();
79                 assertThat(loc.info(item), is(goodURL1));
80
81                 item = loc.next(item);
82                 assertThat(loc.info(item), is(goodURL2));
83
84                 item = loc.next(item);
85                 assertThat(item, is(nullValue()));
86                 assertThat(loc.info(item), is("Invalid Item"));
87
88                 item = loc.first();
89                 loc.invalidate(item);
90                 loc.get(item);
91                 
92                 loc.destroy();
93                 item = loc.best();
94         }
95
96         @Test(expected = LocatorException.class)
97         public void failuresTest() throws LocatorException {
98                 HClientHotPeerLocator loc;
99                 String urlStr = goodURL1 + ',' + goodURL2 + ',' + badURL;
100                 loc = new HClientHotPeerLocator(access, urlStr, 1000000, "38.627", "-90.199", ssMock);
101                 String[] messages = outStream.toString().split("\n");
102                 String preffered = messages[0].split(" ", 4)[3];
103                 String alternate1 = messages[1].split(" ", 4)[3];
104                 String alternate2 = messages[2].split(" ", 4)[3];
105                 assertThat(preffered, is("Preferred Client is " + badURL));
106                 assertThat(alternate1, is("Alternate Client is " + goodURL1));
107                 assertThat(alternate2, is("Alternate Client is " + goodURL2));
108
109                 outStream.reset();
110                 
111                 loc.invalidate(loc.first());
112
113                 loc.destroy();
114                 Locator.Item item = loc.best();
115         }
116         
117         @Test
118         public void hasNoItemTest() throws LocatorException {
119                 HClientHotPeerLocator loc;
120                 loc = new HClientHotPeerLocator(access, badURL, 0, "38.627", "-90.199", ssMock);
121                 assertThat(loc.hasItems(), is(false));
122                 loc.invalidate(loc.first());
123         }
124
125         @Test(expected = LocatorException.class)
126         public void invalidClientTest() throws LocatorException {
127                 @SuppressWarnings("unused")
128                 HClientHotPeerLocator loc = new HClientHotPeerLocator(access, "InvalidClient", 0, "38.627", "-90.199", ssMock);
129         }
130
131         @Test(expected = LocatorException.class)
132         public void coverageTest() throws LocatorException {
133                 CoverageLocator loc;
134                 String urlStr = goodURL1 + ',' + goodURL2;
135                 loc = new CoverageLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
136                 assertThat(loc._invalidate(null), is(nullValue()));
137                 loc._destroy(null);
138                 
139                 loc._newClient("bad string");
140         }
141
142         private class CoverageLocator extends HClientHotPeerLocator {
143                 public CoverageLocator(Access access, String urlstr, long invalidateTime, String localLatitude,
144                                 String localLongitude, HX509SS ss) throws LocatorException {
145                         super(access, urlstr, invalidateTime, localLatitude, localLongitude, ss);
146                 }
147                 public HClient _newClient(String clientInfo) throws LocatorException { return super._newClient(clientInfo); }
148                 public HClient _invalidate(HClient client) { return super._invalidate(client); }
149                 public void _destroy(HClient client) { super._destroy(client); }
150         }
151 }