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.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.net.InetAddress;
32 import java.net.UnknownHostException;
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;
42 public class JU_DNSLocator {
44 private PropAccess access;
48 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
52 public void test() throws LocatorException {
57 dl = new DNSLocator(access, "https", "localhost", "8100-8101");
61 assertThat(uri.toString(), is("https://localhost:8100"));
63 assertThat(uri.toString(), is("https://localhost:8100"));
65 assertThat(dl.hasItems(), is(true));
66 for (item = dl.first(); item != null; item = dl.next(item)) {
69 assertThat(dl.hasItems(), is(false));
71 // This doesn't actually do anything besides increase coverage
76 public void constructorTest() throws LocatorException {
78 new DNSLocator(access, "https", "localhost", "8100");
79 new DNSLocator(access, "https", "localhost", "8100-8101");
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");
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");
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() {
104 public InetAddress[] getAllByName(String host) throws UnknownHostException {
105 return new InetAddress[0];
109 assertThat(dl.refresh(), is(true));
112 @Test(expected = LocatorException.class)
113 public void throws1Test() throws LocatorException {
114 new DNSLocator(access, null);
117 @Test(expected = LocatorException.class)
118 public void throws2Test() throws LocatorException {
119 new DNSLocator(access, "ftp:invalid");
122 @Test(expected = LocatorException.class)
123 public void throws3Test() throws LocatorException {
124 new DNSLocator(access, "https:localhost:[8100");
127 @Test(expected = LocatorException.class)
128 public void throws4Test() throws LocatorException {
129 new DNSLocator(access, "https:localhost:[]");
132 @Test(expected = LocatorException.class)
133 public void throws5Test() throws LocatorException {
134 new DNSLocator(access, "https:localhost:[8100-]");
137 @Test(expected = LocatorException.class)
138 public void throws6Test() throws LocatorException {
139 new DNSLocator(access, "https:localhost:[-8101]");
142 @Test(expected = LocatorException.class)
143 public void throws7Test() throws LocatorException {
144 new DNSLocator(access, "https:localhost:/");