Removing code smells
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / ExpiryRecordTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning.beans;
25
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;
29
30 import java.text.ParseException;
31
32 public class ExpiryRecordTest {
33
34
35     private ExpiryRecord expiryRecord;
36
37     @Test
38     public void Validate_Constructor_Creates_Object_With_Get_Methods() throws ParseException {
39         String[] args = {"2018-08-29-10-10-10-543.", "EXP", "238465493.fileName", "1","285", "123/file.txt","GET","","2000","example","100"};
40         expiryRecord = new ExpiryRecord(args);
41         Assert.assertEquals("238465493.fileName", expiryRecord.getPublishId());
42         Assert.assertEquals(1, expiryRecord.getFeedid());
43         Assert.assertEquals("123/file.txt", expiryRecord.getRequestUri());
44         Assert.assertEquals("GET", expiryRecord.getMethod());
45         Assert.assertEquals("", expiryRecord.getContentType());
46         Assert.assertEquals(2000, expiryRecord.getContentLength());
47         Assert.assertEquals(285, expiryRecord.getSubid());
48         Assert.assertEquals("file.txt", expiryRecord.getFileid());
49         Assert.assertEquals(100, expiryRecord.getDeliveryAttempts());
50         Assert.assertEquals("other", expiryRecord.getReason());
51     }
52
53     @Test
54     public void Validate_AsJsonObject_Correct_Json_Object_After_Set_Methods() throws ParseException {
55         String[] args = {"2018-08-29-10-10-10-543.", "EXP", "238465493.fileName", "1","285", "123/file.txt","GET","","2000","example","100"};
56         expiryRecord = new ExpiryRecord(args);
57         expiryRecord.setContentLength(265);
58         expiryRecord.setEventTime(1535533810543L);
59         expiryRecord.setPublishId("2345657324.fileName");
60         expiryRecord.setFeedid(2);
61         expiryRecord.setRequestUri("/delete/2");
62         expiryRecord.setContentType("application/json");
63         expiryRecord.setMethod("PUT");
64         expiryRecord.setSubid(322);
65         expiryRecord.setFileid("file.txt");
66         expiryRecord.setDeliveryAttempts(125);
67         expiryRecord.setReason("Out of memory");
68
69         LOGJSONObject expiryRecordJson = createBaseLogRecordJson();
70         String expiryRecordString = stripBracketFromJson(expiryRecordJson);
71         String expiryRecordObject = stripBracketFromJson(expiryRecord.asJSONObject());
72         Assert.assertTrue(expiryRecordObject.matches(expiryRecordString));
73     }
74
75     private LOGJSONObject createBaseLogRecordJson() {
76         LOGJSONObject expiryRecordJson = new LOGJSONObject();
77         expiryRecordJson.put("expiryReason", "Out of memory");
78         expiryRecordJson.put("publishId", "2345657324.fileName");
79         expiryRecordJson.put("attempts", 125);
80         expiryRecordJson.put("requestURI", "/delete/2");
81         expiryRecordJson.put("method", "PUT");
82         expiryRecordJson.put("contentType", "application/json");
83         expiryRecordJson.put("type", "exp");
84         expiryRecordJson.put("date", "2018-08-29T[0-1][0-9]:10:10.543Z");
85         expiryRecordJson.put("contentLength", 265);
86         return expiryRecordJson;
87     }
88
89     private String stripBracketFromJson(LOGJSONObject expiryRecordJson) {
90         String expiryRecordString = expiryRecordJson.toString();
91         expiryRecordString = expiryRecordString.substring(1, expiryRecordString.length() - 1);
92         return expiryRecordString;
93     }
94 }
95