2 * ============LICENSE_START====================================================
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
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====================================================
22 package org.onap.aaf.cadi.locator.test;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26 import static org.hamcrest.CoreMatchers.*;
30 import java.io.ByteArrayOutputStream;
31 import java.io.PrintStream;
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;
41 public class JU_HClientHotPeerLocator {
43 @Mock private HX509SS ssMock;
45 private PropAccess access;
46 private ByteArrayOutputStream outStream;
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 = "~%$!@#$//";
55 MockitoAnnotations.initMocks(this);
57 outStream = new ByteArrayOutputStream();
58 access = new PropAccess(new PrintStream(outStream), new String[0]);
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));
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));
74 HClient firstClient = loc.get(loc.first());
75 HClient bestClient = loc.bestClient();
76 assertThat(bestClient, is(firstClient));
78 Locator.Item item = loc.first();
79 assertThat(loc.info(item), is(goodURL1));
81 item = loc.next(item);
82 assertThat(loc.info(item), is(goodURL2));
84 item = loc.next(item);
85 assertThat(item, is(nullValue()));
86 assertThat(loc.info(item), is("Invalid Item"));
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));
111 loc.invalidate(loc.first());
114 Locator.Item item = loc.best();
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());
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);
131 @Test(expected = LocatorException.class)
132 public void coverageTest() throws LocatorException {
134 String urlStr = goodURL1 + ',' + goodURL2;
135 loc = new CoverageLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
136 assertThat(loc._invalidate(null), is(nullValue()));
139 loc._newClient("bad string");
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);
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); }