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.aaf.v2_0.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Matchers.*;
27 import static org.mockito.Mockito.*;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
34 import java.net.URISyntaxException;
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;
45 public class JU_AbsAAFLocator {
47 @Mock private LocatorCreator locatorCreatorMock;
49 private PropAccess access;
52 private static final String uriString = "example.com";
55 public void setup() throws URISyntaxException {
56 MockitoAnnotations.initMocks(this);
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
62 uri = new URI(uriString);
66 public static void tearDownAfterClass() throws Exception {
67 AbsAAFLocator.setCreator(null);
71 public void test() throws LocatorException {
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()));
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"));
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("example.com/extra/stuff"));
94 assertThat(loc.getNameFromURI(new URI("example.com/locate/stuff")), is("stuff")); // n' stuff
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
106 public void coverage() throws LocatorException {
107 AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
108 assertThat(loc.get(null), is(nullValue()));
111 loc.get(mock(Item.class));
112 fail("Should've thrown an exception");
113 } catch (Exception e) {
117 loc.invalidate(mock(Item.class));
118 fail("Should've thrown an exception");
119 } catch (Exception e) {
124 fail("Should've thrown an exception");
125 } catch (Exception e) {
128 assertThat(loc.first(), is(nullValue()));
130 assertThat(loc.hasItems(), is(false));
131 assertThat(loc.next(null), is(nullValue()));
134 loc.next(mock(Item.class));
135 fail("Should've thrown an exception");
136 } catch (Exception e) {
142 assertThat(loc.exposeGetURI(uri), is(uri));
144 assertThat(loc.setPathInfo("pathInfo"), is(not(nullValue())));
145 assertThat(loc.setQuery("query"), is(not(nullValue())));
146 assertThat(loc.setFragment("fragment"), is(not(nullValue())));
148 assertThat(loc.exposeGetURI(uri), is(not(uri)));
152 @Test(expected = LocatorException.class)
153 public void throwsTest() throws LocatorException {
154 @SuppressWarnings("unused")
155 AAFLocatorStub loc = new AAFLocatorStub(new PropAccess(), "name");
158 private class AAFLocatorStub extends AbsAAFLocator<BasicTrans> {
159 public AAFLocatorStub(Access access, String name) throws LocatorException {
160 super(access, name, 10000L);
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); }