CHeckstyle and JUnit for base package in ONAP-REST
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / PapUrlResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pdp.rest;
24
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;
32
33 public class PapUrlResolverTest {
34
35     @Test
36     public void testPropertyModifications() {
37         DateFormat df = new SimpleDateFormat();
38         // first sort order
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),
45                 urls);
46
47         rs.failed();
48         rs.getNext();
49         Assert.assertTrue(rs.hasMoreUrls());
50         rs.succeeded();
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));
57
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());
63         rs.getNext();
64         Assert.assertTrue(rs.hasMoreUrls());
65         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
66         rs.getNext();
67         Assert.assertTrue(rs.hasMoreUrls());
68         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
69         rs.getNext();
70         Assert.assertTrue(rs.hasMoreUrls());
71         Assert.assertEquals("http://one.localhost.com", rs.getUrl());
72         rs.succeeded();
73         rs.getNext();
74         Assert.assertFalse(rs.hasMoreUrls());
75         prop = rs.getProperties();
76         Assert.assertEquals("-1,-1,-1,-1",
77                 prop.getProperty(XacmlRestProperties.PROP_PAP_FAILED_URLS));
78
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());
84         rs.succeeded();
85         rs.getNext();
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());
93         rs.succeeded();
94         rs.getNext();
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());
102         rs.succeeded();
103         rs.getNext();
104         Assert.assertFalse(rs.hasMoreUrls());
105
106         prop = rs.getProperties();
107         succeeded = prop.getProperty(XacmlRestProperties.PROP_PAP_SUCCEEDED_URLS);
108     }
109
110     @SuppressWarnings("unused")
111     @Test
112     public void testModifyUrl() {
113         String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654",
114                 "http://mypap2.com:45/pap/");
115         int u = 8;
116     }
117
118     @Test
119     public void testSorts() {
120         long currentTime = new Date().getTime();
121         DateFormat df = new SimpleDateFormat();
122         // first sort order
123         String urls =
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());
132         rs.getNext();
133         Assert.assertTrue(rs.hasMoreUrls());
134         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
135         rs.getNext();
136         Assert.assertTrue(rs.hasMoreUrls());
137         Assert.assertEquals("http://one.localhost.com", rs.getUrl());
138         rs.getNext();
139         Assert.assertTrue(rs.hasMoreUrls());
140         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
141         rs.getNext();
142         Assert.assertFalse(rs.hasMoreUrls());
143
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());
151         rs.getNext();
152         Assert.assertTrue(rs.hasMoreUrls());
153         Assert.assertEquals("http://two.localhost.com", rs.getUrl());
154         rs.getNext();
155         Assert.assertTrue(rs.hasMoreUrls());
156         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
157         rs.getNext();
158         Assert.assertTrue(rs.hasMoreUrls());
159         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
160         rs.getNext();
161         Assert.assertFalse(rs.hasMoreUrls());
162
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());
169         rs.getNext();
170         Assert.assertTrue(rs.hasMoreUrls());
171         Assert.assertEquals("http://two.localhost.com", rs.getUrl());
172         rs.getNext();
173         Assert.assertTrue(rs.hasMoreUrls());
174         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
175         rs.getNext();
176         Assert.assertTrue(rs.hasMoreUrls());
177         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
178         rs.getNext();
179         Assert.assertFalse(rs.hasMoreUrls());
180
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());
190         rs.getNext();
191         Assert.assertTrue(rs.hasMoreUrls());
192         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
193         rs.getNext();
194         Assert.assertTrue(rs.hasMoreUrls());
195         Assert.assertEquals("http://one.localhost.com", rs.getUrl());
196         rs.getNext();
197         Assert.assertTrue(rs.hasMoreUrls());
198         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
199         rs.getNext();
200         Assert.assertFalse(rs.hasMoreUrls());
201
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());
208         rs.getNext();
209         Assert.assertTrue(rs.hasMoreUrls());
210         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
211         rs.getNext();
212         Assert.assertTrue(rs.hasMoreUrls());
213         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
214         rs.getNext();
215         Assert.assertTrue(rs.hasMoreUrls());
216         Assert.assertEquals("http://one.localhost.com", rs.getUrl());
217         rs.getNext();
218         Assert.assertFalse(rs.hasMoreUrls());
219
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());
226         rs.getNext();
227         Assert.assertTrue(rs.hasMoreUrls());
228         Assert.assertEquals("http://two.localhost.com", rs.getUrl());
229         rs.getNext();
230         Assert.assertTrue(rs.hasMoreUrls());
231         Assert.assertEquals("http://three.localhost.com", rs.getUrl());
232         rs.getNext();
233         Assert.assertTrue(rs.hasMoreUrls());
234         Assert.assertEquals("http://four.localhost.com", rs.getUrl());
235         rs.getNext();
236         Assert.assertFalse(rs.hasMoreUrls());
237     }
238 }