major-Extract the assignment out of this expression
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / locator / test / JU_DNSLocator.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
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.net.URI;
31
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.PropAccess;
34 import org.onap.aaf.cadi.Locator.Item;
35 import org.onap.aaf.cadi.locator.DNSLocator;
36
37 public class JU_DNSLocator {
38     
39     private PropAccess access;
40     
41     @Before
42     public void setup() {
43         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
44     }
45
46     @Test
47     public void test() throws LocatorException {
48         DNSLocator dl;
49         Item item;
50         URI uri;
51
52         dl = new DNSLocator(access, "https", "localhost", "8100-8101");
53         
54         item = dl.best();
55         uri = dl.get(item);
56         assertThat(uri.toString(), is("https://localhost:8100"));
57         item = dl.best();
58         assertThat(uri.toString(), is("https://localhost:8100"));
59
60         assertThat(dl.hasItems(), is(true));
61         for (item = dl.first(); item != null; item = dl.next(item)) {
62             dl.invalidate(item);
63         }
64         assertThat(dl.hasItems(), is(false));
65
66         // This doesn't actually do anything besides increase coverage 
67         dl.destroy();
68     }
69     
70     @Test
71     public void constructorTest() throws LocatorException {
72         // For coverage
73         new DNSLocator(access, "https", "localhost", "8100");
74         new DNSLocator(access, "https", "localhost", "8100-8101");
75
76         new DNSLocator(access, "http:localhost");
77         new DNSLocator(access, "https:localhost");
78         new DNSLocator(access, "https:localhost:8100");
79         new DNSLocator(access, "https:localhost:[8100]");
80         new DNSLocator(access, "https:localhost:[8100-8101]");
81         new DNSLocator(access, "https:localhost:8000/");
82     }
83     
84     @Test
85     public void refreshTest() throws LocatorException {
86         DNSLocator dl = new DNSLocator(access, "https", "bogushost", "8100-8101");
87         assertThat(dl.refresh(), is(false));
88     }
89     
90     @Test(expected = LocatorException.class)
91     public void throws1Test() throws LocatorException {
92         new DNSLocator(access, null);
93     }
94
95     @Test(expected = LocatorException.class)
96     public void throws2Test() throws LocatorException {
97         new DNSLocator(access, "ftp:invalid");
98     }
99
100     @Test(expected = LocatorException.class)
101     public void throws3Test() throws LocatorException {
102         new DNSLocator(access, "https:localhost:[8100");
103     }
104
105     @Test(expected = LocatorException.class)
106     public void throws4Test() throws LocatorException {
107         new DNSLocator(access, "https:localhost:[]");
108     }
109
110     @Test(expected = LocatorException.class)
111     public void throws5Test() throws LocatorException {
112         new DNSLocator(access, "https:localhost:[8100-]");
113     }
114
115     @Test(expected = LocatorException.class)
116     public void throws6Test() throws LocatorException {
117         new DNSLocator(access, "https:localhost:[-8101]");
118     }
119
120     @Test(expected = LocatorException.class)
121     public void throws7Test() throws LocatorException {
122         new DNSLocator(access, "https:localhost:/");
123     }
124
125 }