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