64e9572eb80901821551f2d2db541783f02eab7b
[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_API_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_API_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
89     @Test
90     public void nameFromLocatorURITest() throws LocatorException, URISyntaxException {
91         AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
92         assertThat(loc.getNameFromURI(new URI("example.com")), is("example.com"));
93         assertThat(loc.getNameFromURI(new URI("example.com/extra/stuff")), is("extra"));
94         assertThat(loc.getNameFromURI(new URI("example.com/locate/stuff")), is("stuff")); // n' stuff
95     }
96
97     @Test
98     public void setSelfTest() throws LocatorException {
99         AbsAAFLocator.setCreatorSelf("host", 8000);
100         AbsAAFLocator.setCreator(null);
101         AbsAAFLocator.setCreatorSelf("host", 8000);
102         (new AAFLocatorStub(access, "name:v2.0")).setSelf("host", 8000);  // oof
103     }
104
105     @Test
106     public void coverage() throws LocatorException {
107         AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
108         assertThat(loc.get(null), is(nullValue()));
109
110         try {
111             loc.get(mock(Item.class));
112             fail("Should've thrown an exception");
113         } catch (Exception e) {
114         }
115
116         try {
117             loc.invalidate(mock(Item.class));
118             fail("Should've thrown an exception");
119         } catch (Exception e) {
120         }
121
122         try {
123             loc.best();
124             fail("Should've thrown an exception");
125         } catch (Exception e) {
126         }
127
128         assertThat(loc.first(), is(nullValue()));
129
130         assertThat(loc.hasItems(), is(false));
131         assertThat(loc.next(null), is(nullValue()));
132
133         try {
134             loc.next(mock(Item.class));
135             fail("Should've thrown an exception");
136         } catch (Exception e) {
137         }
138
139         loc.destroy();
140
141
142         assertThat(loc.exposeGetURI(uri), is(uri));
143
144         assertThat(loc.setPathInfo("pathInfo"), is(not(nullValue())));
145         assertThat(loc.setQuery("query"), is(not(nullValue())));
146         assertThat(loc.setFragment("fragment"), is(not(nullValue())));
147         
148         assertThat(loc.exposeGetURI(uri), is(not(uri)));
149     }
150
151
152     @Test(expected = LocatorException.class)
153     public void throwsTest() throws LocatorException {
154         @SuppressWarnings("unused")
155         AAFLocatorStub loc = new AAFLocatorStub(new PropAccess(), "name");
156     }
157
158     private class AAFLocatorStub extends AbsAAFLocator<BasicTrans> {
159         public AAFLocatorStub(Access access, String name) throws LocatorException {
160             super(access, name, 10000L);
161         }
162         @Override public boolean refresh() { return false; }
163         @Override protected URI getURI() { return uri; }
164         public String getName() { return name; }
165         public String getVersion() { return version; }
166         public String getNameFromURI(URI uri) { return nameFromLocatorURI(uri); }
167         public URI exposeGetURI(URI uri) throws LocatorException { return super.getURI(uri); }
168     }
169
170 }