Sonar Fixes, Formatting
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / locator / test / JU_PropertyLocator.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 static org.mockito.Mockito.*;
27 import org.junit.*;
28 import org.mockito.*;
29
30 import java.net.Socket;
31 import java.net.URI;
32
33 import org.onap.aaf.cadi.Locator.Item;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.locator.PropertyLocator;
36
37 public class JU_PropertyLocator {
38
39     @Mock
40     Socket socketMock;
41
42     @Before
43     public void setup() {
44         MockitoAnnotations.initMocks(this);
45
46         when(socketMock.isConnected()).thenReturn(true);
47         when(socketMock.isClosed()).thenReturn(true).thenReturn(false);
48     }
49
50     @Test
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; }
55         };
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));
60
61         Item item = pl.first();
62         assertThat(item.toString(), is("Item: 0 order: 0"));
63
64         URI uri = pl.get(item);
65         assertThat(uri.toString(), is("https://fred.wilma.com:26444"));
66
67         assertThat(pl.get(null), is(nullValue()));
68
69         assertThat(pl.hasItems(), is(true));
70
71         assertThat(countItems(pl), is(3));
72         pl.invalidate(pl.best());
73
74         assertThat(countItems(pl), is(2));
75         pl.invalidate(pl.best());
76
77         assertThat(countItems(pl), is(1));
78
79         pl.invalidate(pl.best());
80
81         assertThat(pl.hasItems(), is(false));
82         assertThat(countItems(pl), is(0));
83
84         Thread.sleep(20L); // PL checks same milli...
85         pl.refresh();
86
87         assertThat(pl.hasItems(), is(true));
88
89         assertThat(pl.next(null), is(nullValue()));
90
91         // coverage...
92         pl.invalidate(null);
93         pl.invalidate(null);
94         pl.invalidate(null);
95         pl.invalidate(null);
96
97         pl.destroy();
98
99         pl = new PropertyLocator(uris);
100
101     }
102
103     @Test(expected=LocatorException.class)
104     public void exceptionTest() throws LocatorException {
105         new PropertyLocator(null);
106     }
107
108     private int countItems(PropertyLocator pl) throws LocatorException {
109         int count = 0;
110         for (Item i = pl.first(); i != null; i = pl.next(i)) {
111             ++count;
112         }
113         return count;
114     }
115
116 }