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.*;
26 import static org.mockito.Mockito.*;
30 import java.net.Socket;
33 import org.onap.aaf.cadi.Locator.Item;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.locator.PropertyLocator;
37 public class JU_PropertyLocator {
44 MockitoAnnotations.initMocks(this);
46 when(socketMock.isConnected()).thenReturn(true);
47 when(socketMock.isClosed()).thenReturn(true).thenReturn(false);
51 public void test() throws Exception {
52 String uris = "https://fred.wilma.com:26444,https://tom.jerry.com:[534-535]";
53 PropertyLocator pl = new PropertyLocator(uris, 0L, 1000*60*20L) {
54 @Override protected Socket createSocket() { return socketMock; }
56 String str = pl.toString();
57 assertThat(str.contains("https://fred.wilma.com:26444"), is(true));
58 assertThat(str.contains("https://tom.jerry.com:534"), is(true));
59 assertThat(str.contains("https://tom.jerry.com:535"), is(true));
61 Item item = pl.first();
62 assertThat(item.toString(), is("Item: 0 order: 0"));
64 URI uri = pl.get(item);
65 assertThat(uri.toString(), is("https://fred.wilma.com:26444"));
67 assertThat(pl.get(null), is(nullValue()));
69 assertThat(pl.hasItems(), is(true));
71 assertThat(countItems(pl), is(3));
72 pl.invalidate(pl.best());
74 assertThat(countItems(pl), is(2));
75 pl.invalidate(pl.best());
77 assertThat(countItems(pl), is(1));
79 pl.invalidate(pl.best());
81 assertThat(pl.hasItems(), is(false));
82 assertThat(countItems(pl), is(0));
84 Thread.sleep(20L); // PL checks same milli...
87 assertThat(pl.hasItems(), is(true));
89 assertThat(pl.next(null), is(nullValue()));
99 pl = new PropertyLocator(uris);
103 @Test(expected=LocatorException.class)
104 public void exceptionTest() throws LocatorException {
105 new PropertyLocator(null);
108 private int countItems(PropertyLocator pl) throws LocatorException {
110 for (Item i = pl.first(); i != null; i = pl.next(i)) {