2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.policy.pdp.rest;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.openecomp.policy.pdp.rest.PapUrlResolver;
25 import org.openecomp.policy.rest.XACMLRestProperties;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Properties;
32 import org.junit.Assert;
34 public class PapUrlResolverTest {
38 public void testPropertyModifications(){
39 long currentTime = new Date().getTime();
40 DateFormat df = new SimpleDateFormat();
42 String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
43 String failed = "-1,-1,-1,-1";
44 String succeeded = "-1,-1,-1,-1";
45 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
46 Assert.assertEquals(rs.getProperties().getProperty(XACMLRestProperties.PROP_PAP_URLS),urls);
50 Assert.assertTrue(rs.hasMoreUrls());
52 Assert.assertFalse(rs.hasMoreUrls());
53 Properties prop = rs.getProperties();
54 Assert.assertEquals(df.format(new Date())+",-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
55 Assert.assertEquals("-1,"+df.format(new Date())+",-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS));
57 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
58 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
59 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
60 Assert.assertTrue(rs.hasMoreUrls());
61 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
63 Assert.assertTrue(rs.hasMoreUrls());
64 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
66 Assert.assertTrue(rs.hasMoreUrls());
67 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
69 Assert.assertTrue(rs.hasMoreUrls());
70 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
73 Assert.assertFalse(rs.hasMoreUrls());
74 prop = rs.getProperties();
75 Assert.assertEquals("-1,-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
77 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
78 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
79 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
80 Assert.assertTrue(rs.hasMoreUrls());
81 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
84 Assert.assertFalse(rs.hasMoreUrls());
85 prop = rs.getProperties();
86 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
87 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
88 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
89 Assert.assertTrue(rs.hasMoreUrls());
90 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
93 Assert.assertFalse(rs.hasMoreUrls());
94 prop = rs.getProperties();
95 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
96 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
97 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
98 Assert.assertTrue(rs.hasMoreUrls());
99 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
102 Assert.assertFalse(rs.hasMoreUrls());
105 prop = rs.getProperties();
106 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
107 System.out.println(succeeded);
108 //can't test this in a timely manner since it will need to wait a minute, since they all succeeded at the same time
110 prop = rs.getProperties();
111 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
112 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
113 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
114 Assert.assertTrue(rs.hasMoreUrls());
115 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
118 Assert.assertFalse(rs.hasMoreUrls());
122 public void testModifyUrl(){
123 String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654", "http://mypap2.com:45/pap/");
128 public void testSorts(){
129 long currentTime = new Date().getTime();
130 DateFormat df = new SimpleDateFormat();
132 String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
133 String failed = df.format(new Date(currentTime-3600000))+",-1,-1,"+df.format(new Date(currentTime-3200000));
134 String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
135 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
136 Assert.assertTrue(rs.hasMoreUrls());
137 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
139 Assert.assertTrue(rs.hasMoreUrls());
140 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
142 Assert.assertTrue(rs.hasMoreUrls());
143 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
145 Assert.assertTrue(rs.hasMoreUrls());
146 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
148 Assert.assertFalse(rs.hasMoreUrls());
150 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
151 failed = "-1,-1,-1,-1";
152 succeeded = "-1,"+df.format(new Date(currentTime-3600000))+","+df.format(new Date(currentTime-6600000))+",-1";
153 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
154 Assert.assertTrue(rs.hasMoreUrls());
155 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
157 Assert.assertTrue(rs.hasMoreUrls());
158 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
160 Assert.assertTrue(rs.hasMoreUrls());
161 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
163 Assert.assertTrue(rs.hasMoreUrls());
164 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
166 Assert.assertFalse(rs.hasMoreUrls());
168 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
169 failed = "-1,-1,-1,-1";
170 succeeded = "-1,-1,-1,-1";
171 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
172 Assert.assertTrue(rs.hasMoreUrls());
173 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
175 Assert.assertTrue(rs.hasMoreUrls());
176 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
178 Assert.assertTrue(rs.hasMoreUrls());
179 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
181 Assert.assertTrue(rs.hasMoreUrls());
182 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
184 Assert.assertFalse(rs.hasMoreUrls());
186 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
187 failed = df.format(new Date(currentTime-3900000))+","+df.format(new Date(currentTime-5600000))+","+df.format(new Date(currentTime-4600000))+","+df.format(new Date(currentTime-3600000));
188 succeeded = "-1,-1,-1,-1";
189 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
190 Assert.assertTrue(rs.hasMoreUrls());
191 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
193 Assert.assertTrue(rs.hasMoreUrls());
194 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
196 Assert.assertTrue(rs.hasMoreUrls());
197 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
199 Assert.assertTrue(rs.hasMoreUrls());
200 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
202 Assert.assertFalse(rs.hasMoreUrls());
204 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
205 failed = df.format(new Date(currentTime-(3600000*4)))+",-1,-1,-1";
206 succeeded = "-1,-1,-1,-1";
207 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
208 Assert.assertTrue(rs.hasMoreUrls());
209 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
211 Assert.assertTrue(rs.hasMoreUrls());
212 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
214 Assert.assertTrue(rs.hasMoreUrls());
215 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
217 Assert.assertTrue(rs.hasMoreUrls());
218 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
220 Assert.assertFalse(rs.hasMoreUrls());
222 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
223 failed = df.format(new Date(currentTime-(3600000*6)))+",-1,-1,-1";
224 succeeded = "-1,-1,-1,-1";
225 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
226 Assert.assertTrue(rs.hasMoreUrls());
227 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
229 Assert.assertTrue(rs.hasMoreUrls());
230 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
232 Assert.assertTrue(rs.hasMoreUrls());
233 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
235 Assert.assertTrue(rs.hasMoreUrls());
236 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
238 Assert.assertFalse(rs.hasMoreUrls());
243 //not used because we are no longer sorting based on success time, only failed time. Modified above.
246 String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
247 String failed = df.format(new Date(currentTime-3600000))+",-1,-1,"+df.format(new Date(currentTime-3200000));
248 String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
249 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
250 Assert.assertTrue(rs.hasMoreUrls());
251 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
253 Assert.assertTrue(rs.hasMoreUrls());
254 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
256 Assert.assertTrue(rs.hasMoreUrls());
257 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
259 Assert.assertTrue(rs.hasMoreUrls());
260 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
262 Assert.assertFalse(rs.hasMoreUrls());
264 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
265 failed = "-1,-1,-1,-1";
266 succeeded = "-1,"+df.format(new Date(currentTime-3600000))+","+df.format(new Date(currentTime-6600000))+",-1";
267 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
268 Assert.assertTrue(rs.hasMoreUrls());
269 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
271 Assert.assertTrue(rs.hasMoreUrls());
272 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
274 Assert.assertTrue(rs.hasMoreUrls());
275 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
277 Assert.assertTrue(rs.hasMoreUrls());
278 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
280 Assert.assertFalse(rs.hasMoreUrls());
282 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
283 failed = "-1,-1,-1,-1";
284 succeeded = "-1,-1,-1,-1";
285 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
286 Assert.assertTrue(rs.hasMoreUrls());
287 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
289 Assert.assertTrue(rs.hasMoreUrls());
290 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
292 Assert.assertTrue(rs.hasMoreUrls());
293 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
295 Assert.assertTrue(rs.hasMoreUrls());
296 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
298 Assert.assertFalse(rs.hasMoreUrls());
300 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
301 failed = df.format(new Date(currentTime-3900000))+","+df.format(new Date(currentTime-5600000))+","+df.format(new Date(currentTime-4600000))+","+df.format(new Date(currentTime-3600000));
302 succeeded = "-1,-1,-1,-1";
303 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
304 Assert.assertTrue(rs.hasMoreUrls());
305 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
307 Assert.assertTrue(rs.hasMoreUrls());
308 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
310 Assert.assertTrue(rs.hasMoreUrls());
311 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
313 Assert.assertTrue(rs.hasMoreUrls());
314 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
316 Assert.assertFalse(rs.hasMoreUrls());
318 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
319 failed = df.format(new Date(currentTime-(3600000*4)))+",-1,-1,-1";
320 succeeded = "-1,-1,-1,-1";
321 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
322 Assert.assertTrue(rs.hasMoreUrls());
323 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
325 Assert.assertTrue(rs.hasMoreUrls());
326 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
328 Assert.assertTrue(rs.hasMoreUrls());
329 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
331 Assert.assertTrue(rs.hasMoreUrls());
332 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
334 Assert.assertFalse(rs.hasMoreUrls());
336 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
337 failed = df.format(new Date(currentTime-(3600000*6)))+",-1,-1,-1";
338 succeeded = "-1,-1,-1,-1";
339 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
340 Assert.assertTrue(rs.hasMoreUrls());
341 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
343 Assert.assertTrue(rs.hasMoreUrls());
344 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
346 Assert.assertTrue(rs.hasMoreUrls());
347 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
349 Assert.assertTrue(rs.hasMoreUrls());
350 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
352 Assert.assertFalse(rs.hasMoreUrls());