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.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.when;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.lang.reflect.Field;
32 import java.net.HttpURLConnection;
34 import java.net.URISyntaxException;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.aaf.cadi.CadiException;
46 import org.onap.aaf.cadi.LocatorException;
47 import org.onap.aaf.cadi.PropAccess;
48 import org.onap.aaf.cadi.client.Future;
49 import org.onap.aaf.cadi.config.Config;
50 import org.onap.aaf.cadi.config.SecurityInfoC;
51 import org.onap.aaf.cadi.http.HClient;
52 import org.onap.aaf.misc.env.Data.TYPE;
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 {
112 // return clientMock;
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"));