Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PDP-REST / src / test / java / org / openecomp / policy / pdp / rest / PapUrlResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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
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 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;
26
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Properties;
31
32 import org.junit.Assert;
33
34 public class PapUrlResolverTest {
35
36         
37         @Test
38         public void testPropertyModifications(){
39                 long currentTime = new Date().getTime();
40                 DateFormat df = new SimpleDateFormat();
41                 //first sort order
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);
47                 
48                 rs.failed();
49                 rs.getNext();
50                 Assert.assertTrue(rs.hasMoreUrls());
51                 rs.succeeded();
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));
56                 
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());
62                 rs.getNext();
63                 Assert.assertTrue(rs.hasMoreUrls());
64                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
65                 rs.getNext();
66                 Assert.assertTrue(rs.hasMoreUrls());
67                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
68                 rs.getNext();
69                 Assert.assertTrue(rs.hasMoreUrls());
70                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
71                 rs.succeeded();
72                 rs.getNext();
73                 Assert.assertFalse(rs.hasMoreUrls());
74                 prop = rs.getProperties();
75                 Assert.assertEquals("-1,-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));          
76                 
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());
82                 rs.succeeded();
83                 rs.getNext();
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());
91                 rs.succeeded();
92                 rs.getNext();
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());
100                 rs.succeeded();
101                 rs.getNext();
102                 Assert.assertFalse(rs.hasMoreUrls());
103                 
104                 
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
109                 /*
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());
116                 rs.succeeded();
117                 rs.getNext();
118                 Assert.assertFalse(rs.hasMoreUrls());
119                 */
120         }
121         @Test
122         public void testModifyUrl(){
123                 String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654", "http://mypap2.com:45/pap/");
124                 int u = 8;
125         }
126                 
127         @Test
128         public void testSorts(){
129                 long currentTime = new Date().getTime();
130                 DateFormat df = new SimpleDateFormat();
131                 //first sort order
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());
138                 rs.getNext();
139                 Assert.assertTrue(rs.hasMoreUrls());
140                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
141                 rs.getNext();
142                 Assert.assertTrue(rs.hasMoreUrls());
143                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
144                 rs.getNext();
145                 Assert.assertTrue(rs.hasMoreUrls());
146                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
147                 rs.getNext();
148                 Assert.assertFalse(rs.hasMoreUrls());
149                 
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());
156                 rs.getNext();
157                 Assert.assertTrue(rs.hasMoreUrls());
158                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
159                 rs.getNext();
160                 Assert.assertTrue(rs.hasMoreUrls());
161                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
162                 rs.getNext();
163                 Assert.assertTrue(rs.hasMoreUrls());
164                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
165                 rs.getNext();
166                 Assert.assertFalse(rs.hasMoreUrls());
167                 
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());
174                 rs.getNext();
175                 Assert.assertTrue(rs.hasMoreUrls());
176                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
177                 rs.getNext();
178                 Assert.assertTrue(rs.hasMoreUrls());
179                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
180                 rs.getNext();
181                 Assert.assertTrue(rs.hasMoreUrls());
182                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
183                 rs.getNext();
184                 Assert.assertFalse(rs.hasMoreUrls());
185                 
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());
192                 rs.getNext();
193                 Assert.assertTrue(rs.hasMoreUrls());
194                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
195                 rs.getNext();
196                 Assert.assertTrue(rs.hasMoreUrls());
197                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
198                 rs.getNext();
199                 Assert.assertTrue(rs.hasMoreUrls());
200                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
201                 rs.getNext();
202                 Assert.assertFalse(rs.hasMoreUrls());
203                 
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());
210                 rs.getNext();
211                 Assert.assertTrue(rs.hasMoreUrls());
212                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
213                 rs.getNext();
214                 Assert.assertTrue(rs.hasMoreUrls());
215                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
216                 rs.getNext();
217                 Assert.assertTrue(rs.hasMoreUrls());
218                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
219                 rs.getNext();
220                 Assert.assertFalse(rs.hasMoreUrls());
221                 
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());
228                 rs.getNext();
229                 Assert.assertTrue(rs.hasMoreUrls());
230                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
231                 rs.getNext();
232                 Assert.assertTrue(rs.hasMoreUrls());
233                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
234                 rs.getNext();
235                 Assert.assertTrue(rs.hasMoreUrls());
236                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
237                 rs.getNext();
238                 Assert.assertFalse(rs.hasMoreUrls());
239                 
240                 
241                 
242                 
243                 //not used because we are no longer sorting based on success time, only failed time. Modified above.
244                 /*
245                 //first sort order
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());
252                 rs.getNext();
253                 Assert.assertTrue(rs.hasMoreUrls());
254                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
255                 rs.getNext();
256                 Assert.assertTrue(rs.hasMoreUrls());
257                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
258                 rs.getNext();
259                 Assert.assertTrue(rs.hasMoreUrls());
260                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
261                 rs.getNext();
262                 Assert.assertFalse(rs.hasMoreUrls());
263                 
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());
270                 rs.getNext();
271                 Assert.assertTrue(rs.hasMoreUrls());
272                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
273                 rs.getNext();
274                 Assert.assertTrue(rs.hasMoreUrls());
275                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
276                 rs.getNext();
277                 Assert.assertTrue(rs.hasMoreUrls());
278                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
279                 rs.getNext();
280                 Assert.assertFalse(rs.hasMoreUrls());
281                 
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());
288                 rs.getNext();
289                 Assert.assertTrue(rs.hasMoreUrls());
290                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
291                 rs.getNext();
292                 Assert.assertTrue(rs.hasMoreUrls());
293                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
294                 rs.getNext();
295                 Assert.assertTrue(rs.hasMoreUrls());
296                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
297                 rs.getNext();
298                 Assert.assertFalse(rs.hasMoreUrls());
299                 
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());
306                 rs.getNext();
307                 Assert.assertTrue(rs.hasMoreUrls());
308                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
309                 rs.getNext();
310                 Assert.assertTrue(rs.hasMoreUrls());
311                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
312                 rs.getNext();
313                 Assert.assertTrue(rs.hasMoreUrls());
314                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
315                 rs.getNext();
316                 Assert.assertFalse(rs.hasMoreUrls());
317                 
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());
324                 rs.getNext();
325                 Assert.assertTrue(rs.hasMoreUrls());
326                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
327                 rs.getNext();
328                 Assert.assertTrue(rs.hasMoreUrls());
329                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
330                 rs.getNext();
331                 Assert.assertTrue(rs.hasMoreUrls());
332                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
333                 rs.getNext();
334                 Assert.assertFalse(rs.hasMoreUrls());
335                 
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());
342                 rs.getNext();
343                 Assert.assertTrue(rs.hasMoreUrls());
344                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
345                 rs.getNext();
346                 Assert.assertTrue(rs.hasMoreUrls());
347                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
348                 rs.getNext();
349                 Assert.assertTrue(rs.hasMoreUrls());
350                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
351                 rs.getNext();
352                 Assert.assertFalse(rs.hasMoreUrls());
353                 */
354         }
355 }