[POLICY-73] replace openecomp for policy-engine
[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  * 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.onap.policy.pdp.rest;
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Properties;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.policy.rest.XACMLRestProperties;
30
31 public class PapUrlResolverTest {
32
33         
34         @Test
35         public void testPropertyModifications(){
36                 DateFormat df = new SimpleDateFormat();
37                 //first sort order
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);
43                 
44                 rs.failed();
45                 rs.getNext();
46                 Assert.assertTrue(rs.hasMoreUrls());
47                 rs.succeeded();
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));
52                 
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());
58                 rs.getNext();
59                 Assert.assertTrue(rs.hasMoreUrls());
60                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
61                 rs.getNext();
62                 Assert.assertTrue(rs.hasMoreUrls());
63                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
64                 rs.getNext();
65                 Assert.assertTrue(rs.hasMoreUrls());
66                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
67                 rs.succeeded();
68                 rs.getNext();
69                 Assert.assertFalse(rs.hasMoreUrls());
70                 prop = rs.getProperties();
71                 Assert.assertEquals("-1,-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));          
72                 
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());
78                 rs.succeeded();
79                 rs.getNext();
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());
87                 rs.succeeded();
88                 rs.getNext();
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());
96                 rs.succeeded();
97                 rs.getNext();
98                 Assert.assertFalse(rs.hasMoreUrls());
99                 
100                 
101                 prop = rs.getProperties();
102                 succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
103         }
104         @SuppressWarnings("unused")
105         @Test
106         public void testModifyUrl(){
107                 String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654", "http://mypap2.com:45/pap/");
108                 int u = 8;
109         }
110                 
111         @Test
112         public void testSorts(){
113                 long currentTime = new Date().getTime();
114                 DateFormat df = new SimpleDateFormat();
115                 //first sort order
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());
122                 rs.getNext();
123                 Assert.assertTrue(rs.hasMoreUrls());
124                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
125                 rs.getNext();
126                 Assert.assertTrue(rs.hasMoreUrls());
127                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
128                 rs.getNext();
129                 Assert.assertTrue(rs.hasMoreUrls());
130                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
131                 rs.getNext();
132                 Assert.assertFalse(rs.hasMoreUrls());
133                 
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());
140                 rs.getNext();
141                 Assert.assertTrue(rs.hasMoreUrls());
142                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
143                 rs.getNext();
144                 Assert.assertTrue(rs.hasMoreUrls());
145                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
146                 rs.getNext();
147                 Assert.assertTrue(rs.hasMoreUrls());
148                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
149                 rs.getNext();
150                 Assert.assertFalse(rs.hasMoreUrls());
151                 
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());
158                 rs.getNext();
159                 Assert.assertTrue(rs.hasMoreUrls());
160                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
161                 rs.getNext();
162                 Assert.assertTrue(rs.hasMoreUrls());
163                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
164                 rs.getNext();
165                 Assert.assertTrue(rs.hasMoreUrls());
166                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
167                 rs.getNext();
168                 Assert.assertFalse(rs.hasMoreUrls());
169                 
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());
176                 rs.getNext();
177                 Assert.assertTrue(rs.hasMoreUrls());
178                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
179                 rs.getNext();
180                 Assert.assertTrue(rs.hasMoreUrls());
181                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
182                 rs.getNext();
183                 Assert.assertTrue(rs.hasMoreUrls());
184                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
185                 rs.getNext();
186                 Assert.assertFalse(rs.hasMoreUrls());
187                 
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());
194                 rs.getNext();
195                 Assert.assertTrue(rs.hasMoreUrls());
196                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
197                 rs.getNext();
198                 Assert.assertTrue(rs.hasMoreUrls());
199                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
200                 rs.getNext();
201                 Assert.assertTrue(rs.hasMoreUrls());
202                 Assert.assertEquals("http://one.localhost.com", rs.getUrl());
203                 rs.getNext();
204                 Assert.assertFalse(rs.hasMoreUrls());
205                 
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());
212                 rs.getNext();
213                 Assert.assertTrue(rs.hasMoreUrls());
214                 Assert.assertEquals("http://two.localhost.com", rs.getUrl());
215                 rs.getNext();
216                 Assert.assertTrue(rs.hasMoreUrls());
217                 Assert.assertEquals("http://three.localhost.com", rs.getUrl());
218                 rs.getNext();
219                 Assert.assertTrue(rs.hasMoreUrls());
220                 Assert.assertEquals("http://four.localhost.com", rs.getUrl());
221                 rs.getNext();
222                 Assert.assertFalse(rs.hasMoreUrls());
223         }
224 }