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