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.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Mockito.mock;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
34 import java.net.URISyntaxException;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aaf.cadi.Access;
42 import org.onap.aaf.cadi.Locator.Item;
43 import org.onap.aaf.cadi.LocatorException;
44 import org.onap.aaf.cadi.PropAccess;
45 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
46 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator.LocatorCreator;
47 import org.onap.aaf.cadi.config.Config;
48 import org.onap.aaf.misc.env.impl.BasicTrans;
50 public class JU_AbsAAFLocator {
52 @Mock private LocatorCreator locatorCreatorMock;
54 private PropAccess access;
57 private static final String uriString = "example.com";
60 public void setup() throws URISyntaxException {
61 MockitoAnnotations.initMocks(this);
63 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
64 access.setProperty(Config.CADI_LATITUDE, "38.62"); // St Louis approx lat
65 access.setProperty(Config.CADI_LONGITUDE, "90.19"); // St Louis approx lon
67 uri = new URI(uriString);
71 public static void tearDownAfterClass() throws Exception {
72 AbsAAFLocator.setCreator(null);
76 public void test() throws LocatorException {
80 loc = new AAFLocatorStub(access, "httpname");
81 assertThat(loc.getName(), is("httpname"));
82 assertThat(loc.getVersion(), is(Config.AAF_DEFAULT_API_VERSION));
83 assertThat(loc.toString(), is("AAFLocator for " + "httpname" + " on " + loc.getURI()));
85 loc = new AAFLocatorStub(access, "name");
86 assertThat(loc.getName(), is("name"));
87 assertThat(loc.getVersion(), is(Config.AAF_DEFAULT_API_VERSION));
88 loc = new AAFLocatorStub(access, "name:v2.0");
89 assertThat(loc.getName(), is("name"));
90 assertThat(loc.getVersion(), is("v2.0"));
95 public void nameFromLocatorURITest() throws LocatorException, URISyntaxException {
96 AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
97 assertThat(loc.getNameFromURI(new URI("example.com")), is("example.com"));
98 assertThat(loc.getNameFromURI(new URI("example.com/extra/stuff")), is("extra"));
99 assertThat(loc.getNameFromURI(new URI("example.com/locate/stuff")), is("stuff")); // n' stuff
103 public void setSelfTest() throws LocatorException {
104 AbsAAFLocator.setCreatorSelf("host", 8000);
105 AbsAAFLocator.setCreator(null);
106 AbsAAFLocator.setCreatorSelf("host", 8000);
107 (new AAFLocatorStub(access, "name:v2.0")).setSelf("host", 8000); // oof
111 public void coverage() throws LocatorException {
112 AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
113 assertThat(loc.get(null), is(nullValue()));
116 loc.get(mock(Item.class));
117 fail("Should've thrown an exception");
118 } catch (Exception e) {
122 loc.invalidate(mock(Item.class));
123 fail("Should've thrown an exception");
124 } catch (Exception e) {
129 fail("Should've thrown an exception");
130 } catch (Exception e) {
133 assertThat(loc.first(), is(nullValue()));
135 assertThat(loc.hasItems(), is(false));
136 assertThat(loc.next(null), is(nullValue()));
139 loc.next(mock(Item.class));
140 fail("Should've thrown an exception");
141 } catch (Exception e) {
147 assertThat(loc.exposeGetURI(uri), is(uri));
149 assertThat(loc.setPathInfo("pathInfo"), is(not(nullValue())));
150 assertThat(loc.setQuery("query"), is(not(nullValue())));
151 assertThat(loc.setFragment("fragment"), is(not(nullValue())));
153 assertThat(loc.exposeGetURI(uri), is(not(uri)));
157 @Test(expected = LocatorException.class)
158 public void throwsTest() throws LocatorException {
159 @SuppressWarnings("unused")
160 AAFLocatorStub loc = new AAFLocatorStub(new PropAccess(), "name");
163 private class AAFLocatorStub extends AbsAAFLocator<BasicTrans> {
164 public AAFLocatorStub(Access access, String name) throws LocatorException {
165 super(access, name, 10000L);
167 @Override public boolean refresh() { return false; }
168 @Override protected URI getURI() { return uri; }
169 public String getName() { return name; }
170 public String getVersion() { return version; }
171 public String getNameFromURI(URI uri) { return nameFromLocatorURI(uri); }
172 public URI exposeGetURI(URI uri) throws LocatorException { return super.getURI(uri); }