[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / BaseLogRecordTest.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 package org.onap.dmaap.datarouter.provisioning.beans;
24
25 import java.text.ParseException;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;
29
30 public class BaseLogRecordTest {
31
32   private BaseLogRecord baseLogRecord;
33
34   @Test
35   public void Validate_Constructor_Creates_Object_With_Get_Methods() throws ParseException {
36     String[] args = {"2018-08-29-10-10-10-543.", "DLX", "238465493.fileName",
37         "1", "/publish/1/fileName", "285"};
38     baseLogRecord = new BaseLogRecord(args);
39     Assert.assertEquals("238465493.fileName", baseLogRecord.getPublishId());
40     Assert.assertEquals(1, baseLogRecord.getFeedid());
41     Assert.assertEquals("", baseLogRecord.getRequestUri());
42     Assert.assertEquals("GET", baseLogRecord.getMethod());
43     Assert.assertEquals("", baseLogRecord.getContentType());
44     Assert.assertEquals(285, baseLogRecord.getContentLength());
45   }
46
47   @Test
48   public void Validate_AsJsonObject_Correct_Json_Object_After_Set_Methods() throws ParseException {
49     String[] args = {"2018-08-29-10-10-10-543.", "DEL", "238465493.fileName",
50         "1", "", "/delete/1", "DELETE", "application/octet-stream", "285"};
51     baseLogRecord = new BaseLogRecord(args);
52     baseLogRecord.setContentLength(265);
53     baseLogRecord.setEventTime(1535533810543L);
54     baseLogRecord.setPublishId("2345657324.fileName");
55     baseLogRecord.setFeedid(2);
56     baseLogRecord.setRequestUri("/delete/2");
57     baseLogRecord.setMethod("PUT");
58     baseLogRecord.setContentType("application/json");
59     LOGJSONObject baseLogRecordJson = createBaseLogRecordJson();
60     String baseLogRecordString = stripBracketFromJson(baseLogRecordJson);
61     String baseLogRecordStringObject = stripBracketFromJson(baseLogRecord.asJSONObject());
62     Assert.assertTrue(baseLogRecordStringObject.matches(baseLogRecordString));
63   }
64
65   private LOGJSONObject createBaseLogRecordJson() {
66     LOGJSONObject baseLogRecordJson = new LOGJSONObject();
67     baseLogRecordJson.put("date", "2018-08-29T[0-1][0-9]:10:10.543Z");
68     baseLogRecordJson.put("publishId", "2345657324.fileName");
69     baseLogRecordJson.put("requestURI", "/delete/2");
70     baseLogRecordJson.put("method", "PUT");
71     baseLogRecordJson.put("contentType", "application/json");
72     baseLogRecordJson.put("contentLength", 265);
73     return baseLogRecordJson;
74   }
75
76   private String stripBracketFromJson(LOGJSONObject baseLogRecordJson) {
77     String baseLogRecordString = baseLogRecordJson.toString();
78     baseLogRecordString = baseLogRecordString.substring(1, baseLogRecordString.length() - 1);
79     return baseLogRecordString;
80   }
81 }