2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.rest;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Properties;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.onap.policy.rest.XACMLRestProperties;
33 public class PapUrlResolverTest {
36 public void testPropertyModifications() {
37 DateFormat df = new SimpleDateFormat();
39 String urls = "http://one.localhost.com,http://two.localhost.com,"
40 + "http://three.localhost.com,http://four.localhost.com";
41 String failed = "-1,-1,-1,-1";
42 String succeeded = "-1,-1,-1,-1";
43 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
44 Assert.assertEquals(rs.getProperties().getProperty(XACMLRestProperties.PROP_PAP_URLS),
49 Assert.assertTrue(rs.hasMoreUrls());
51 Assert.assertFalse(rs.hasMoreUrls());
52 Properties prop = rs.getProperties();
53 Assert.assertEquals(df.format(new Date()) + ",-1,-1,-1",
54 prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
55 Assert.assertEquals("-1," + df.format(new Date()) + ",-1,-1",
56 prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS));
58 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
59 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
60 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
61 Assert.assertTrue(rs.hasMoreUrls());
62 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
64 Assert.assertTrue(rs.hasMoreUrls());
65 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
67 Assert.assertTrue(rs.hasMoreUrls());
68 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
70 Assert.assertTrue(rs.hasMoreUrls());
71 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
74 Assert.assertFalse(rs.hasMoreUrls());
75 prop = rs.getProperties();
76 Assert.assertEquals("-1,-1,-1,-1",
77 prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
79 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
80 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
81 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
82 Assert.assertTrue(rs.hasMoreUrls());
83 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
86 Assert.assertFalse(rs.hasMoreUrls());
87 prop = rs.getProperties();
88 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
89 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
90 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
91 Assert.assertTrue(rs.hasMoreUrls());
92 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
95 Assert.assertFalse(rs.hasMoreUrls());
96 prop = rs.getProperties();
97 failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
98 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
99 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
100 Assert.assertTrue(rs.hasMoreUrls());
101 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
104 Assert.assertFalse(rs.hasMoreUrls());
106 prop = rs.getProperties();
107 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
110 @SuppressWarnings("unused")
112 public void testModifyUrl() {
113 String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654",
114 "http://mypap2.com:45/pap/");
119 public void testSorts() {
120 long currentTime = new Date().getTime();
121 DateFormat df = new SimpleDateFormat();
124 "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,"
125 + "http://four.localhost.com";
126 String failed = df.format(new Date(currentTime - 3600000)) + ",-1,-1,"
127 + df.format(new Date(currentTime - 3200000));
128 String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
129 PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
130 Assert.assertTrue(rs.hasMoreUrls());
131 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
133 Assert.assertTrue(rs.hasMoreUrls());
134 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
136 Assert.assertTrue(rs.hasMoreUrls());
137 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
139 Assert.assertTrue(rs.hasMoreUrls());
140 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
142 Assert.assertFalse(rs.hasMoreUrls());
144 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
145 failed = "-1,-1,-1,-1";
146 succeeded = "-1," + df.format(new Date(currentTime - 3600000)) + ","
147 + df.format(new Date(currentTime - 6600000)) + ",-1";
148 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
149 Assert.assertTrue(rs.hasMoreUrls());
150 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
152 Assert.assertTrue(rs.hasMoreUrls());
153 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
155 Assert.assertTrue(rs.hasMoreUrls());
156 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
158 Assert.assertTrue(rs.hasMoreUrls());
159 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
161 Assert.assertFalse(rs.hasMoreUrls());
163 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
164 failed = "-1,-1,-1,-1";
165 succeeded = "-1,-1,-1,-1";
166 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
167 Assert.assertTrue(rs.hasMoreUrls());
168 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
170 Assert.assertTrue(rs.hasMoreUrls());
171 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
173 Assert.assertTrue(rs.hasMoreUrls());
174 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
176 Assert.assertTrue(rs.hasMoreUrls());
177 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
179 Assert.assertFalse(rs.hasMoreUrls());
181 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
182 failed = df.format(new Date(currentTime - 3900000)) + ","
183 + df.format(new Date(currentTime - 5600000)) + ","
184 + df.format(new Date(currentTime - 4600000)) + ","
185 + df.format(new Date(currentTime - 3600000));
186 succeeded = "-1,-1,-1,-1";
187 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
188 Assert.assertTrue(rs.hasMoreUrls());
189 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
191 Assert.assertTrue(rs.hasMoreUrls());
192 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
194 Assert.assertTrue(rs.hasMoreUrls());
195 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
197 Assert.assertTrue(rs.hasMoreUrls());
198 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
200 Assert.assertFalse(rs.hasMoreUrls());
202 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
203 failed = df.format(new Date(currentTime - (3600000 * 4))) + ",-1,-1,-1";
204 succeeded = "-1,-1,-1,-1";
205 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
206 Assert.assertTrue(rs.hasMoreUrls());
207 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
209 Assert.assertTrue(rs.hasMoreUrls());
210 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
212 Assert.assertTrue(rs.hasMoreUrls());
213 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
215 Assert.assertTrue(rs.hasMoreUrls());
216 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
218 Assert.assertFalse(rs.hasMoreUrls());
220 urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
221 failed = df.format(new Date(currentTime - (3600000 * 6))) + ",-1,-1,-1";
222 succeeded = "-1,-1,-1,-1";
223 rs = PapUrlResolver.getInstance(urls, failed, succeeded);
224 Assert.assertTrue(rs.hasMoreUrls());
225 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
227 Assert.assertTrue(rs.hasMoreUrls());
228 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
230 Assert.assertTrue(rs.hasMoreUrls());
231 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
233 Assert.assertTrue(rs.hasMoreUrls());
234 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
236 Assert.assertFalse(rs.hasMoreUrls());