Fix DNSLocator JUnit
[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.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.net.InetAddress;
31 import java.net.URI;
32 import java.net.UnknownHostException;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.aaf.cadi.Access.Level;
37 import org.onap.aaf.cadi.Locator.Item;
38 import org.onap.aaf.cadi.LocatorException;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.locator.DNSLocator;
41
42 public class JU_DNSLocator {
43
44     private PropAccess access;
45
46     @Before
47     public void setup() {
48         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
49     }
50
51     @Test
52     public void test() throws LocatorException {
53         DNSLocator dl;
54         Item item;
55         URI uri;
56
57         dl = new DNSLocator(access, "https", "localhost", "8100-8101");
58
59         item = dl.best();
60         uri = dl.get(item);
61         assertThat(uri.toString(), is("https://localhost:8100"));
62         item = dl.best();
63         assertThat(uri.toString(), is("https://localhost:8100"));
64
65         assertThat(dl.hasItems(), is(true));
66         for (item = dl.first(); item != null; item = dl.next(item)) {
67             dl.invalidate(item);
68         }
69         assertThat(dl.hasItems(), is(false));
70
71         // This doesn't actually do anything besides increase coverage
72         dl.destroy();
73     }
74
75     @Test
76     public void constructorTest() throws LocatorException {
77         // For coverage
78         new DNSLocator(access, "https", "localhost", "8100");
79         new DNSLocator(access, "https", "localhost", "8100-8101");
80
81         new DNSLocator(access, "http://localhost");
82         new DNSLocator(access, "https://localhost");
83         new DNSLocator(access, "https://localhost:8100");
84         new DNSLocator(access, "https://localhost:[8100]");
85         new DNSLocator(access, "https://localhost:[8100-8101]");
86         new DNSLocator(access, "https://localhost:8000/");
87         new DNSLocator(access, "https://aaf-locatexx.onapxxx:8095/locate");
88         try {
89             new DNSLocator(access, "https:localhost:8000");
90             fail("Invalid URL should not pass");
91         } catch (LocatorException e) {
92             access.log(Level.DEBUG, "Valid Exception");
93
94         }
95     }
96
97     @Test
98     public void refreshTest() throws LocatorException {
99         DNSLocator dl = new DNSLocator(access, "https", "bogushost", "8100-8101",
100                 // Note: Lambda would be nice but need JDK 1.7 still
101                 // PowerMock couldn't do InetAddress
102                 new DNSLocator.DNSLookup() {
103                                 @Override
104                                 public InetAddress[] getAllByName(String host) throws UnknownHostException {
105                                         return new InetAddress[0];
106                                 }
107                 }
108         );
109         assertThat(dl.refresh(), is(true));
110     }
111
112     @Test(expected = LocatorException.class)
113     public void throws1Test() throws LocatorException {
114         new DNSLocator(access, null);
115     }
116
117     @Test(expected = LocatorException.class)
118     public void throws2Test() throws LocatorException {
119         new DNSLocator(access, "ftp:invalid");
120     }
121
122     @Test(expected = LocatorException.class)
123     public void throws3Test() throws LocatorException {
124         new DNSLocator(access, "https:localhost:[8100");
125     }
126
127     @Test(expected = LocatorException.class)
128     public void throws4Test() throws LocatorException {
129         new DNSLocator(access, "https:localhost:[]");
130     }
131
132     @Test(expected = LocatorException.class)
133     public void throws5Test() throws LocatorException {
134         new DNSLocator(access, "https:localhost:[8100-]");
135     }
136
137     @Test(expected = LocatorException.class)
138     public void throws6Test() throws LocatorException {
139         new DNSLocator(access, "https:localhost:[-8101]");
140     }
141
142     @Test(expected = LocatorException.class)
143     public void throws7Test() throws LocatorException {
144         new DNSLocator(access, "https:localhost:/");
145     }
146
147 }