Update Fixes from testing
[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.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;
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.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;
49
50 public class JU_AbsAAFLocator {
51
52     @Mock private LocatorCreator locatorCreatorMock;
53
54     private PropAccess access;
55     private URI uri;
56
57     private static final String uriString = "example.com";
58
59     @Before
60     public void setup() throws URISyntaxException {
61         MockitoAnnotations.initMocks(this);
62
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
66
67         uri = new URI(uriString);
68     }
69
70     @AfterClass
71     public static void tearDownAfterClass() throws Exception {
72         AbsAAFLocator.setCreator(null);
73     }
74
75     @Test
76     public void test() throws LocatorException {
77         AAFLocatorStub loc;
78
79         // Test with http
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()));
84
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"));
91     }
92
93
94     @Test
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
100     }
101
102     @Test
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
108     }
109
110     @Test
111     public void coverage() throws LocatorException {
112         AAFLocatorStub loc = new AAFLocatorStub(access, "name:v2.0");
113         assertThat(loc.get(null), is(nullValue()));
114
115         try {
116             loc.get(mock(Item.class));
117             fail("Should've thrown an exception");
118         } catch (Exception e) {
119         }
120
121         try {
122             loc.invalidate(mock(Item.class));
123             fail("Should've thrown an exception");
124         } catch (Exception e) {
125         }
126
127         try {
128             loc.best();
129             fail("Should've thrown an exception");
130         } catch (Exception e) {
131         }
132
133         assertThat(loc.first(), is(nullValue()));
134
135         assertThat(loc.hasItems(), is(false));
136         assertThat(loc.next(null), is(nullValue()));
137
138         try {
139             loc.next(mock(Item.class));
140             fail("Should've thrown an exception");
141         } catch (Exception e) {
142         }
143
144         loc.destroy();
145
146
147         assertThat(loc.exposeGetURI(uri), is(uri));
148
149         assertThat(loc.setPathInfo("pathInfo"), is(not(nullValue())));
150         assertThat(loc.setQuery("query"), is(not(nullValue())));
151         assertThat(loc.setFragment("fragment"), is(not(nullValue())));
152         
153         assertThat(loc.exposeGetURI(uri), is(not(uri)));
154     }
155
156
157     @Test(expected = LocatorException.class)
158     public void throwsTest() throws LocatorException {
159         @SuppressWarnings("unused")
160         AAFLocatorStub loc = new AAFLocatorStub(new PropAccess(), "name");
161     }
162
163     private class AAFLocatorStub extends AbsAAFLocator<BasicTrans> {
164         public AAFLocatorStub(Access access, String name) throws LocatorException {
165             super(access, name, 10000L);
166         }
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); }
173     }
174
175 }