e9c74cbfe148503e995fbab69031f8d68d218ebe
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / v2_0 / test / JU_AbsAAFLocator.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.aaf.v2_0.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Matchers.*;
27 import static org.mockito.Mockito.*;
28 import org.junit.*;
29 import org.mockito.*;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35
36 import org.onap.aaf.cadi.Access;
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.aaf.v2_0.AbsAAFLocator;
41 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator.LocatorCreator;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.misc.env.impl.BasicTrans;
44
45 public class JU_AbsAAFLocator {
46
47         @Mock private LocatorCreator locatorCreatorMock;
48
49         private PropAccess access;
50         private URI uri;
51
52         private static final String uriString = "example.com";
53
54         @Before
55         public void setup() throws URISyntaxException {
56                 MockitoAnnotations.initMocks(this);
57
58                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
59                 access.setProperty(Config.CADI_LATITUDE, "38.62");  // St Louis approx lat
60                 access.setProperty(Config.CADI_LONGITUDE, "90.19");  // St Louis approx lon
61
62                 uri = new URI(uriString);
63         }
64
65         @AfterClass
66         public static void tearDownAfterClass() throws Exception {
67                 AbsAAFLocator.setCreator(null);
68         }
69
70         @Test
71         public void test() throws LocatorException {
72                 AAFLocatorStub loc;
73
74                 // Test with http
75                 loc = new AAFLocatorStub(access, "httpname");
76                 assertThat(loc.getName(), is("httpname"));
77                 assertThat(loc.getVersion(), is(Config.AAF_DEFAULT_VERSION));
78                 assertThat(loc.toString(), is("AAFLocator for " + "httpname" + " on " + loc.getURI()));
79
80                 loc = new AAFLocatorStub(access, "name");
81                 assertThat(loc.getName(), is("name"));
82                 assertThat(loc.getVersion(), is(Config.AAF_DEFAULT_VERSION));
83                 loc = new AAFLocatorStub(access, "name:v2.0");
84                 assertThat(loc.getName(), is("name"));
85                 assertThat(loc.getVersion(), is("v2.0"));
86         }
87
88         @Test
89         public void createTest() throws LocatorException {
90                 AbsAAFLocator.setCreator(locatorCreatorMock);
91
92                 assertThat(AbsAAFLocator.create("nonsense"), is(nullValue()));
93                 assertThat(AbsAAFLocator.create("nonsense/locate"), is(nullValue()));
94                 assertThat(AbsAAFLocator.create("nonsense/locate/"), is(nullValue()));
95                 assertThat(AbsAAFLocator.create("nonsense/locate//"), is(nullValue()));
96                 assertThat(AbsAAFLocator.create("nonsense/locate/name:v2.0"), is(nullValue()));
97
98                 assertThat(AbsAAFLocator.create("http/locate/name:v2.0"), is(nullValue()));
99
100                 doReturn(mock(AbsAAFLocator.class)).when(locatorCreatorMock).create(anyString(), anyString());
101                 assertThat(AbsAAFLocator.create("http/locate/name:v2.0/path"), is(not(nullValue())));
102
103                 AbsAAFLocator.setCreator(null);
104                 assertThat(AbsAAFLocator.create("http/locate/name:v2.0"), is(nullValue()));
105
106                 assertThat(AbsAAFLocator.create("http"), is(not(nullValue())));
107
108                 AbsAAFLocator.setCreator(locatorCreatorMock);
109                 assertThat(AbsAAFLocator.create("first", "second"), is(not(nullValue())));
110         }
111
112         @Test
113         public void nameFromLocatorURITest() throws LocatorException, URISyntaxException {
114                 AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
115                 assertThat(loc.getNameFromURI(new URI("example.com")), is("example.com"));
116                 assertThat(loc.getNameFromURI(new URI("example.com/extra/stuff")), is("example.com/extra/stuff"));
117                 assertThat(loc.getNameFromURI(new URI("example.com/locate/stuff")), is("stuff")); // n' stuff
118         }
119
120         @Test
121         public void setSelfTest() throws LocatorException {
122                 AbsAAFLocator.setCreatorSelf("host", 8000);
123                 AbsAAFLocator.setCreator(null);
124                 AbsAAFLocator.setCreatorSelf("host", 8000);
125                 (new AAFLocatorStub(access, "name:v2.0")).setSelf("host", 8000);  // oof
126         }
127
128         @Test
129         public void coverage() throws LocatorException {
130                 AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
131                 assertThat(loc.get(null), is(nullValue()));
132
133                 try {
134                         loc.get(mock(Item.class));
135                         fail("Should've thrown an exception");
136                 } catch (Exception e) {
137                 }
138
139                 try {
140                         loc.invalidate(mock(Item.class));
141                         fail("Should've thrown an exception");
142                 } catch (Exception e) {
143                 }
144
145                 try {
146                         loc.best();
147                         fail("Should've thrown an exception");
148                 } catch (Exception e) {
149                 }
150
151                 assertThat(loc.first(), is(nullValue()));
152
153                 assertThat(loc.hasItems(), is(false));
154                 assertThat(loc.next(null), is(nullValue()));
155
156                 try {
157                         loc.next(mock(Item.class));
158                         fail("Should've thrown an exception");
159                 } catch (Exception e) {
160                 }
161
162                 loc.destroy();
163
164
165                 assertThat(loc.exposeGetURI(uri), is(uri));
166
167                 assertThat(loc.setPathInfo("pathInfo"), is(not(nullValue())));
168                 assertThat(loc.setQuery("query"), is(not(nullValue())));
169                 assertThat(loc.setFragment("fragment"), is(not(nullValue())));
170                 
171                 assertThat(loc.exposeGetURI(uri), is(not(uri)));
172         }
173
174
175         @Test(expected = LocatorException.class)
176         public void throwsTest() throws LocatorException {
177                 @SuppressWarnings("unused")
178                 AAFLocatorStub loc = new AAFLocatorStub(new PropAccess(), "name");
179         }
180
181         private class AAFLocatorStub extends AbsAAFLocator<BasicTrans> {
182                 public AAFLocatorStub(Access access, String name) throws LocatorException {
183                         super(access, name, 10000L);
184                 }
185                 @Override public boolean refresh() { return false; }
186                 @Override protected URI getURI() { return uri; }
187                 public String getName() { return name; }
188                 public String getVersion() { return version; }
189                 public String getNameFromURI(URI uri) { return nameFromLocatorURI(uri); }
190                 public URI exposeGetURI(URI uri) throws LocatorException { return super.getURI(uri); }
191         }
192
193 }