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.onap.policy.pdp.rest;
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Properties;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.policy.rest.XACMLRestProperties;
31 public class PapUrlResolverTest {
35 public void testPropertyModifications(){
36 DateFormat df = new SimpleDateFormat();
38 String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
39 String failed = "-1,-1,-1,-1";
40 String succeeded = "-1,-1,-1,-1";
41 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
42 Assert.assertEquals(rs.getProperties().getProperty(XACMLRestProperties.PROP_PAP_URLS),urls);
46 Assert.assertTrue(rs.hasMoreUrls());
48 Assert.assertFalse(rs.hasMoreUrls());
49 Properties prop = rs.getProperties();
50 Assert.assertEquals(df.format(new Date())+",-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
51 Assert.assertEquals("-1,"+df.format(new Date())+",-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS));
53 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
54 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
55 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
56 Assert.assertTrue(rs.hasMoreUrls());
57 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
59 Assert.assertTrue(rs.hasMoreUrls());
60 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
62 Assert.assertTrue(rs.hasMoreUrls());
63 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
65 Assert.assertTrue(rs.hasMoreUrls());
66 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
69 Assert.assertFalse(rs.hasMoreUrls());
70 prop = rs.getProperties();
71 Assert.assertEquals("-1,-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
73 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
74 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
75 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
76 Assert.assertTrue(rs.hasMoreUrls());
77 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
80 Assert.assertFalse(rs.hasMoreUrls());
81 prop = rs.getProperties();
82 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
83 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
84 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
85 Assert.assertTrue(rs.hasMoreUrls());
86 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
89 Assert.assertFalse(rs.hasMoreUrls());
90 prop = rs.getProperties();
91 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
92 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
93 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
94 Assert.assertTrue(rs.hasMoreUrls());
95 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
98 Assert.assertFalse(rs.hasMoreUrls());
101 prop = rs.getProperties();
102 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
104 @SuppressWarnings("unused")
106 public void testModifyUrl(){
107 String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654", "http://mypap2.com:45/pap/");
112 public void testSorts(){
113 long currentTime = new Date().getTime();
114 DateFormat df = new SimpleDateFormat();
116 String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
117 String failed = df.format(new Date(currentTime-3600000))+",-1,-1,"+df.format(new Date(currentTime-3200000));
118 String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
119 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
120 Assert.assertTrue(rs.hasMoreUrls());
121 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
123 Assert.assertTrue(rs.hasMoreUrls());
124 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
126 Assert.assertTrue(rs.hasMoreUrls());
127 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
129 Assert.assertTrue(rs.hasMoreUrls());
130 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
132 Assert.assertFalse(rs.hasMoreUrls());
134 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
135 failed = "-1,-1,-1,-1";
136 succeeded = "-1,"+df.format(new Date(currentTime-3600000))+","+df.format(new Date(currentTime-6600000))+",-1";
137 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
138 Assert.assertTrue(rs.hasMoreUrls());
139 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
141 Assert.assertTrue(rs.hasMoreUrls());
142 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
144 Assert.assertTrue(rs.hasMoreUrls());
145 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
147 Assert.assertTrue(rs.hasMoreUrls());
148 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
150 Assert.assertFalse(rs.hasMoreUrls());
152 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
153 failed = "-1,-1,-1,-1";
154 succeeded = "-1,-1,-1,-1";
155 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
156 Assert.assertTrue(rs.hasMoreUrls());
157 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
159 Assert.assertTrue(rs.hasMoreUrls());
160 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
162 Assert.assertTrue(rs.hasMoreUrls());
163 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
165 Assert.assertTrue(rs.hasMoreUrls());
166 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
168 Assert.assertFalse(rs.hasMoreUrls());
170 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
171 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));
172 succeeded = "-1,-1,-1,-1";
173 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
174 Assert.assertTrue(rs.hasMoreUrls());
175 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
177 Assert.assertTrue(rs.hasMoreUrls());
178 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
180 Assert.assertTrue(rs.hasMoreUrls());
181 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
183 Assert.assertTrue(rs.hasMoreUrls());
184 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
186 Assert.assertFalse(rs.hasMoreUrls());
188 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
189 failed = df.format(new Date(currentTime-(3600000*4)))+",-1,-1,-1";
190 succeeded = "-1,-1,-1,-1";
191 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
192 Assert.assertTrue(rs.hasMoreUrls());
193 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
195 Assert.assertTrue(rs.hasMoreUrls());
196 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
198 Assert.assertTrue(rs.hasMoreUrls());
199 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
201 Assert.assertTrue(rs.hasMoreUrls());
202 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
204 Assert.assertFalse(rs.hasMoreUrls());
206 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
207 failed = df.format(new Date(currentTime-(3600000*6)))+",-1,-1,-1";
208 succeeded = "-1,-1,-1,-1";
209 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
210 Assert.assertTrue(rs.hasMoreUrls());
211 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
213 Assert.assertTrue(rs.hasMoreUrls());
214 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
216 Assert.assertTrue(rs.hasMoreUrls());
217 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
219 Assert.assertTrue(rs.hasMoreUrls());
220 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
222 Assert.assertFalse(rs.hasMoreUrls());