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.Mockito.*;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.lang.reflect.Field;
34 import java.net.HttpURLConnection;
36 import java.net.URISyntaxException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.SecuritySetter;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.LocatorException;
45 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
46 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
47 import org.onap.aaf.cadi.client.Future;
48 import org.onap.aaf.cadi.config.Config;
49 import org.onap.aaf.cadi.config.SecurityInfoC;
50 import org.onap.aaf.cadi.http.HClient;
51 import org.onap.aaf.misc.env.Data.TYPE;
52 import org.onap.aaf.misc.env.impl.BasicTrans;
53 import org.onap.aaf.misc.rosetta.env.RosettaDF;
55 import locate.v1_0.Endpoint;
56 import locate.v1_0.Endpoints;
58 public class JU_AAFLocator {
60 @Mock private HClient clientMock;
61 @Mock private Future<Endpoints> futureMock;
62 @Mock private Endpoints endpointsMock;
64 private PropAccess access;
66 private ByteArrayOutputStream errStream;
68 private static final String uriString = "https://example.com";
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
74 doReturn(futureMock).when(clientMock).futureRead((RosettaDF<?>)any(), eq(TYPE.JSON));
75 when(clientMock.timeout()).thenReturn(1);
76 when(clientMock.getURI()).thenReturn(new URI(uriString));
77 when(futureMock.get(1)).thenReturn(true);
79 futureMock.value = endpointsMock;
80 List<Endpoint> endpoints = new ArrayList<>();
81 endpoints.add(new Endpoint());
82 when(endpointsMock.getEndpoint()).thenReturn(endpoints);
84 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
86 errStream = new ByteArrayOutputStream();
88 System.setErr(new PrintStream(errStream));
92 public void tearDown() {
93 System.setErr(System.err);
97 public static void tearDownAfterClass() throws Exception {
98 Field field = SecurityInfoC.class.getDeclaredField("sicMap");
99 field.setAccessible(true);
100 field.set(null, new HashMap<>());
104 public void test() throws CadiException, URISyntaxException, LocatorException {
105 access.setProperty(Config.CADI_LATITUDE, "38.62"); // St Louis approx lat
106 access.setProperty(Config.CADI_LONGITUDE, "90.19"); // St Louis approx lon
107 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
108 URI locatorURI = new URI("https://somemachine.moc:10/com.att.aaf.service/2.0");
109 AbsAAFLocator<BasicTrans> al = new AAFLocator(si, locatorURI) {
111 protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
115 // Start over: This was originally calling a developer machine.
116 // assertThat(al.refresh(), is(true));
117 // when(futureMock.get(1)).thenReturn(false);
118 // assertThat(al.refresh(), is(false));
119 // String errorMessage = errStream.toString().split(": ", 2)[1];
120 // assertThat(errorMessage, is("Error reading location information from " + uriString + ": 0 null\n \n"));