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.hamcrest.CoreMatchers.*;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.Locator;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.PropAccess;
36 import org.onap.aaf.cadi.http.HClient;
37 import org.onap.aaf.cadi.http.HX509SS;
38 import org.onap.aaf.cadi.locator.HClientHotPeerLocator;
40 public class JU_HClientHotPeerLocator {
42 @Mock private HX509SS ssMock;
44 private PropAccess access;
45 private ByteArrayOutputStream outStream;
47 // Note: - The IP and port are irrelevant for these tests
48 private static final String goodURL1 = "fakeIP1:fakePort1/38/-90"; // Approx St Louis
49 private static final String goodURL2 = "fakeIP2:fakePort2/33/-96"; // Approx Dallas
50 private static final String badURL = "~%$!@#$//";
54 MockitoAnnotations.initMocks(this);
56 outStream = new ByteArrayOutputStream();
57 access = new PropAccess(new PrintStream(outStream), new String[0]);
61 public void test() throws LocatorException {
62 HClientHotPeerLocator loc;
63 String urlStr = goodURL1 + ',' + goodURL2;
64 loc = new HClientHotPeerLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
65 assertThat(loc.hasItems(), is(true));
67 String[] messages = outStream.toString().split(System.lineSeparator());
68 String preffered = messages[0].split(" ", 4)[3];
69 String alternate = messages[1].split(" ", 4)[3];
70 assertThat(preffered, is("Preferred Client is " + goodURL1));
71 assertThat(alternate, is("Alternate Client is " + goodURL2));
73 HClient firstClient = loc.get(loc.first());
74 HClient bestClient = loc.bestClient();
75 assertThat(bestClient, is(firstClient));
77 Locator.Item item = loc.first();
78 assertThat(loc.info(item), is(goodURL1));
80 item = loc.next(item);
81 assertThat(loc.info(item), is(goodURL2));
83 item = loc.next(item);
84 assertThat(item, is(nullValue()));
85 assertThat(loc.info(item), is("Invalid Item"));
90 loc.invalidate(loc.bestClient());
91 loc.invalidate(loc.get(loc.next(item)));
95 @Test(expected = LocatorException.class)
96 public void failuresTest() throws LocatorException {
97 HClientHotPeerLocator loc;
98 String urlStr = goodURL1 + ',' + goodURL2 + ',' + badURL;
99 loc = new HClientHotPeerLocator(access, urlStr, 1000000, "38.627", "-90.199", ssMock);
100 String[] messages = outStream.toString().split(System.lineSeparator());
101 String preffered = messages[0].split(" ", 4)[3];
102 String alternate1 = messages[1].split(" ", 4)[3];
103 String alternate2 = messages[2].split(" ", 4)[3];
104 assertThat(preffered, is("Preferred Client is " + badURL));
105 assertThat(alternate1, is("Alternate Client is " + goodURL1));
106 assertThat(alternate2, is("Alternate Client is " + goodURL2));
110 loc.invalidate(loc.first());
117 public void hasNoItemTest() throws LocatorException {
118 HClientHotPeerLocator loc;
119 loc = new HClientHotPeerLocator(access, badURL, 0, "38.627", "-90.199", ssMock);
120 assertThat(loc.hasItems(), is(false));
121 loc.invalidate(loc.first());
124 @Test(expected = LocatorException.class)
125 public void invalidClientTest() throws LocatorException {
126 @SuppressWarnings("unused")
127 HClientHotPeerLocator loc = new HClientHotPeerLocator(access, "InvalidClient", 0, "38.627", "-90.199", ssMock);
130 @Test(expected = LocatorException.class)
131 public void coverageTest() throws LocatorException {
133 String urlStr = goodURL1 + ',' + goodURL2;
134 loc = new CoverageLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
135 assertThat(loc._invalidate(null), is(nullValue()));
138 loc._newClient("bad string");
141 private class CoverageLocator extends HClientHotPeerLocator {
142 public CoverageLocator(Access access, String urlstr, long invalidateTime, String localLatitude,
143 String localLongitude, HX509SS ss) throws LocatorException {
144 super(access, urlstr, invalidateTime, localLatitude, localLongitude, ss);
146 public HClient _newClient(String clientInfo) throws LocatorException { return super._newClient(clientInfo); }
147 public HClient _invalidate(HClient client) { return super._invalidate(client); }
148 public void _destroy(HClient client) { super._destroy(client); }