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