Improve Batches
[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.hamcrest.CoreMatchers.*;
26 import org.junit.*;
27 import org.mockito.*;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31
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;
39
40 public class JU_HClientHotPeerLocator {
41
42     @Mock private HX509SS ssMock;
43
44     private PropAccess access;
45     private ByteArrayOutputStream outStream;
46
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 = "~%$!@#$//";
51
52     @Before
53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55
56         outStream = new ByteArrayOutputStream();
57         access = new PropAccess(new PrintStream(outStream), new String[0]);
58     }
59
60     @Test
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));
66
67         String[] messages = outStream.toString().split(System.lineSeparator());
68         String preffered = messages[2].split(" ", 4)[3];
69         String alternate = messages[3].split(" ", 4)[3];
70         assertThat(preffered, is("Preferred Client is " + goodURL1));
71         assertThat(alternate, is("Alternate Client is " + goodURL2));
72
73         HClient firstClient = loc.get(loc.first());
74         HClient bestClient = loc.bestClient();
75         assertThat(bestClient, is(firstClient));
76
77         Locator.Item item = loc.first();
78         assertThat(loc.info(item), is(goodURL1));
79
80         item = loc.next(item);
81         assertThat(loc.info(item), is(goodURL2));
82
83         item = loc.next(item);
84         assertThat(item, is(nullValue()));
85         assertThat(loc.info(item), is("Invalid Item"));
86
87         item = loc.first();
88         loc.invalidate(item);
89         
90         loc.invalidate(loc.bestClient());
91         loc.invalidate(loc.get(loc.next(item)));
92         loc.destroy();
93     }
94
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[2].split(" ", 4)[3];
102         String alternate1 = messages[3].split(" ", 4)[3];
103         String alternate2 = messages[4].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));
107
108         outStream.reset();
109         
110         loc.invalidate(loc.first());
111
112         loc.destroy();
113         loc.best();
114     }
115     
116     @Test
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());
122     }
123
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);
128     }
129
130     @Test(expected = LocatorException.class)
131     public void coverageTest() throws LocatorException {
132         CoverageLocator loc;
133         String urlStr = goodURL1 + ',' + goodURL2;
134         loc = new CoverageLocator(access, urlStr, 0, "38.627", "-90.199", ssMock);
135         assertThat(loc._invalidate(null), is(nullValue()));
136         loc._destroy(null);
137         
138         loc._newClient("bad string");
139     }
140
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);
145         }
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); }
149     }
150 }