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.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.anyOf;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.fail;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.net.InetAddress;
33 import java.net.UnknownHostException;
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;
43 public class JU_DNSLocator {
45 private PropAccess access;
49 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
53 public void test() throws LocatorException {
58 dl = new DNSLocator(access, "https", "localhost", "8100-8101");
62 assertThat(uri.toString(), anyOf(is("https://localhost:8100"), is("https://127.0.0.1:8100")));
64 assertThat(uri.toString(), anyOf(is("https://localhost:8100"), is("https://127.0.0.1:8100")));
66 assertThat(dl.hasItems(), is(true));
67 for (item = dl.first(); item != null; item = dl.next(item)) {
70 assertThat(dl.hasItems(), is(false));
72 // This doesn't actually do anything besides increase coverage
77 public void constructorTest() throws LocatorException {
79 new DNSLocator(access, "https", "localhost", "8100");
80 new DNSLocator(access, "https", "localhost", "8100-8101");
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");
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");
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() {
105 public InetAddress[] getAllByName(String host) throws UnknownHostException {
106 return new InetAddress[0];
110 assertThat(dl.refresh(), is(true));
113 @Test(expected = LocatorException.class)
114 public void throws1Test() throws LocatorException {
115 new DNSLocator(access, null);
118 @Test(expected = LocatorException.class)
119 public void throws2Test() throws LocatorException {
120 new DNSLocator(access, "ftp:invalid");
123 @Test(expected = LocatorException.class)
124 public void throws3Test() throws LocatorException {
125 new DNSLocator(access, "https:localhost:[8100");
128 @Test(expected = LocatorException.class)
129 public void throws4Test() throws LocatorException {
130 new DNSLocator(access, "https:localhost:[]");
133 @Test(expected = LocatorException.class)
134 public void throws5Test() throws LocatorException {
135 new DNSLocator(access, "https:localhost:[8100-]");
138 @Test(expected = LocatorException.class)
139 public void throws6Test() throws LocatorException {
140 new DNSLocator(access, "https:localhost:[-8101]");
143 @Test(expected = LocatorException.class)
144 public void throws7Test() throws LocatorException {
145 new DNSLocator(access, "https:localhost:/");