Collection syntax change because of Sonar
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / v2_0 / test / JU_AAFLocator.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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import org.junit.*;
29 import org.mockito.*;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.lang.reflect.Field;
34 import java.net.HttpURLConnection;
35 import java.net.URI;
36 import java.net.URISyntaxException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40
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;
54
55 import locate.v1_0.Endpoint;
56 import locate.v1_0.Endpoints;
57
58 public class JU_AAFLocator {
59         
60         @Mock private HClient clientMock;
61         @Mock private Future<Endpoints> futureMock;
62         @Mock private Endpoints endpointsMock;
63         
64         private PropAccess access;
65         
66         private ByteArrayOutputStream errStream;
67         
68         private static final String uriString = "https://example.com";
69
70         @Before
71         public void setUp() throws Exception {
72                 MockitoAnnotations.initMocks(this);
73                 
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);
78                 
79                 futureMock.value = endpointsMock;
80                 List<Endpoint> endpoints = new ArrayList<>();
81                 endpoints.add(new Endpoint());
82                 when(endpointsMock.getEndpoint()).thenReturn(endpoints);
83
84                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
85                 
86                 errStream = new ByteArrayOutputStream();
87
88                 System.setErr(new PrintStream(errStream));
89         }
90         
91         @After
92         public void tearDown() {
93                 System.setErr(System.err);
94         }
95         
96         @AfterClass
97         public static void tearDownAfterClass() throws Exception {
98                 Field field = SecurityInfoC.class.getDeclaredField("sicMap");
99                 field.setAccessible(true);
100                 field.set(null, new HashMap<>());
101         }
102
103         @Test
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) {
110                         @Override
111                         protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
112                                 return clientMock;
113                         }
114                 };
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"));
121         }
122
123 }