Skip to content

Commit b4d25d8

Browse files
author
gdgate
authored
Merge pull request #1001 from jeskepetr/WA-12423
Add support for AccessLog API Reviewed-by: Peter Plocháň https://github.com/peter-plochan
2 parents b6f1569 + 28a7fd6 commit b4d25d8

12 files changed

Lines changed: 574 additions & 39 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import com.fasterxml.jackson.annotation.JsonCreator;
9+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
11+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
12+
import com.fasterxml.jackson.annotation.JsonTypeName;
13+
import com.gooddata.sdk.common.util.GoodDataToStringBuilder;
14+
import com.gooddata.sdk.common.util.ISOZonedDateTime;
15+
16+
import java.time.ZonedDateTime;
17+
18+
/**
19+
* Model class, used for special audit log events/access logs directly from haproxy,
20+
* that represents access logs for particular hosts.
21+
*/
22+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
23+
@JsonTypeName("accessLog")
24+
@JsonIgnoreProperties(ignoreUnknown = true)
25+
public class AccessLog {
26+
27+
public static final String RESOURCE_URI = "/gdc/domains/{domainId}/accessLogs";
28+
29+
private final String id;
30+
private final String host;
31+
private final String path;
32+
private final String method;
33+
private final String code;
34+
private final String size;
35+
private final String userIp;
36+
@ISOZonedDateTime
37+
private final ZonedDateTime occurred;
38+
@ISOZonedDateTime
39+
private final ZonedDateTime recorded;
40+
41+
@JsonCreator
42+
public AccessLog(@JsonProperty("id") String id, @JsonProperty("host") String host, @JsonProperty("path") String path,
43+
@JsonProperty("method") String method, @JsonProperty("code") String code, @JsonProperty("size") String size,
44+
@JsonProperty("userIp") String userIp, @JsonProperty("occurred") ZonedDateTime occurred, @JsonProperty("recorded") ZonedDateTime recorded) {
45+
this.id = id;
46+
this.host = host;
47+
this.path = path;
48+
this.method = method;
49+
this.code = code;
50+
this.size = size;
51+
this.userIp = userIp;
52+
this.occurred = occurred;
53+
this.recorded = recorded;
54+
}
55+
56+
public String getId() {
57+
return id;
58+
}
59+
60+
public String getHost() {
61+
return host;
62+
}
63+
64+
public String getPath() {
65+
return path;
66+
}
67+
68+
public String getMethod() {
69+
return method;
70+
}
71+
72+
public String getCode() {
73+
return code;
74+
}
75+
76+
public String getSize() {
77+
return size;
78+
}
79+
80+
public String getUserIp() {
81+
return userIp;
82+
}
83+
84+
public ZonedDateTime getOccurred() {
85+
return occurred;
86+
}
87+
88+
public ZonedDateTime getRecorded() {
89+
return recorded;
90+
}
91+
92+
@Override
93+
public String toString() {
94+
return GoodDataToStringBuilder.defaultToString(this);
95+
}
96+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
9+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
10+
import com.fasterxml.jackson.annotation.JsonTypeName;
11+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
12+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
13+
import com.gooddata.sdk.common.collections.Page;
14+
import com.gooddata.sdk.common.collections.Paging;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
@JsonTypeInfo(
20+
include = JsonTypeInfo.As.WRAPPER_OBJECT,
21+
use = JsonTypeInfo.Id.NAME
22+
)
23+
@JsonTypeName("accessLogs")
24+
@JsonIgnoreProperties(
25+
ignoreUnknown = true
26+
)
27+
@JsonSerialize(
28+
using = AccessLogsSerializer.class
29+
)
30+
@JsonDeserialize(
31+
using = AccessLogsDeserializer.class
32+
)
33+
public class AccessLogs extends Page<AccessLog> {
34+
static final String ROOT_NODE = "accessLogs";
35+
36+
public AccessLogs(List<AccessLog> items, Paging paging, Map<String, String> links) {
37+
super(items, paging, links);
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import com.gooddata.sdk.common.collections.PageDeserializer;
9+
import com.gooddata.sdk.common.collections.Paging;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class AccessLogsDeserializer extends PageDeserializer<AccessLogs, AccessLog> {
15+
16+
public AccessLogsDeserializer() { super(AccessLog.class); }
17+
18+
@Override
19+
protected AccessLogs createPage(List<AccessLog> items, Paging paging, Map<String, String> links) {
20+
return new AccessLogs(items, paging, links);
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import com.gooddata.sdk.common.collections.PageSerializer;
9+
10+
public class AccessLogsSerializer extends PageSerializer {
11+
public AccessLogsSerializer() {
12+
super(AccessLogs.ROOT_NODE);
13+
}
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import org.testng.annotations.Test;
9+
10+
import java.time.LocalDate;
11+
import java.time.ZonedDateTime;
12+
13+
import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
14+
import static java.time.ZoneOffset.UTC;
15+
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
16+
import static net.javacrumbs.jsonunit.core.util.ResourceUtils.resource;
17+
import static org.hamcrest.CoreMatchers.is;
18+
import static org.hamcrest.CoreMatchers.notNullValue;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
21+
public class AccessLogTest {
22+
23+
private static final ZonedDateTime DATE = ZonedDateTime.of(LocalDate.of(1993, 3, 9).atStartOfDay(), UTC);
24+
25+
private final AccessLog ACCESS_LOG = new AccessLog("123", "visa.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);
26+
27+
@Test
28+
public void testSerialize() throws Exception {
29+
assertThat(ACCESS_LOG, jsonEquals(resource("auditevents/accessLog.json")));
30+
}
31+
32+
@Test
33+
public void testDeserialize() throws Exception {
34+
final AccessLog deserializedObject = readObjectFromResource("/auditevents/accessLog.json", AccessLog.class);
35+
assertThat(deserializedObject, notNullValue());
36+
assertThat(deserializedObject.getId(), is(ACCESS_LOG.getId()));
37+
assertThat(deserializedObject.getHost(), is(ACCESS_LOG.getHost()));
38+
assertThat(deserializedObject.getPath(), is(ACCESS_LOG.getPath()));
39+
assertThat(deserializedObject.getMethod(), is(ACCESS_LOG.getMethod()));
40+
assertThat(deserializedObject.getCode(), is(ACCESS_LOG.getCode()));
41+
assertThat(deserializedObject.getSize(), is(ACCESS_LOG.getSize()));
42+
assertThat(deserializedObject.getUserIp(), is(ACCESS_LOG.getUserIp()));
43+
assertThat(deserializedObject.getOccurred(), is(ACCESS_LOG.getOccurred()));
44+
assertThat(deserializedObject.getRecorded(), is(ACCESS_LOG.getRecorded()));
45+
}
46+
47+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.auditevent;
7+
8+
import com.gooddata.sdk.common.collections.Paging;
9+
import org.springframework.web.util.UriTemplate;
10+
import org.testng.annotations.Test;
11+
12+
import java.time.LocalDate;
13+
import java.time.ZonedDateTime;
14+
import java.util.Collections;
15+
import java.util.List;
16+
17+
import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
18+
import static java.time.ZoneOffset.UTC;
19+
import static java.util.Arrays.asList;
20+
import static java.util.Collections.singletonMap;
21+
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
22+
import static net.javacrumbs.jsonunit.core.util.ResourceUtils.resource;
23+
import static org.hamcrest.CoreMatchers.is;
24+
import static org.hamcrest.CoreMatchers.nullValue;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.hamcrest.Matchers.hasSize;
27+
28+
public class AccessLogsTest {
29+
30+
private static final ZonedDateTime DATE = ZonedDateTime.of(LocalDate.of(1993, 3, 9).atStartOfDay(), UTC);
31+
32+
private final AccessLog ACCESS_LOG_1 = new AccessLog("123", "visa.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);
33+
private final AccessLog ACCESS_LOG_2 = new AccessLog("456", "mastercard.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);
34+
35+
private static final String DOMAIN = "default";
36+
private static final String RESOURCE_URI = new UriTemplate(AccessLog.RESOURCE_URI).expand(DOMAIN).toString();
37+
private static final String RESOURCE_NEXT_URI = RESOURCE_URI + "?offset=456";
38+
39+
private final AccessLogs ACCESS_LOGS = new AccessLogs(
40+
asList(ACCESS_LOG_1, ACCESS_LOG_2),
41+
new Paging(RESOURCE_NEXT_URI),
42+
singletonMap("self", RESOURCE_URI)
43+
);
44+
45+
private final AccessLogs EMPTY_ACCESS_LOGS = new AccessLogs(
46+
Collections.emptyList(),
47+
new Paging(null),
48+
singletonMap("self", RESOURCE_URI)
49+
);
50+
51+
@Test
52+
public void shouldSerialize() throws Exception {
53+
assertThat(ACCESS_LOGS, jsonEquals(resource("auditevents/accessLogs.json")));
54+
}
55+
56+
@Test
57+
public void shouldDeserialize() throws Exception {
58+
final AccessLogs deserialized = readObjectFromResource("/auditevents/accessLogs.json", AccessLogs.class);
59+
assertThat(deserialized.getPaging().getNextUri(), is(RESOURCE_NEXT_URI));
60+
final List<AccessLog> pageItems = deserialized.getPageItems();
61+
assertThat(pageItems, hasSize(2));
62+
assertThat(pageItems.get(0).getId(), is(ACCESS_LOG_1.getId()));
63+
assertThat(pageItems.get(1).getId(), is(ACCESS_LOG_2.getId()));
64+
}
65+
66+
@Test
67+
public void testSerializeEmptyAccessLogs() throws Exception {
68+
assertThat(EMPTY_ACCESS_LOGS, jsonEquals(resource("auditevents/emptyAccessLogs.json")));
69+
}
70+
71+
@Test
72+
public void testDeserializeEmptyEvents() throws Exception {
73+
final AccessLogs deserialized = readObjectFromResource("/auditevents/emptyAccessLogs.json", AccessLogs.class);
74+
assertThat(deserialized.getPaging().getNextUri(), nullValue());
75+
assertThat(deserialized.getPageItems(), hasSize(0));
76+
}
77+
78+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"accessLog": {
3+
"id": "123",
4+
"host": "visa.gooddata.com",
5+
"path": "/gdc/ping",
6+
"method": "GET",
7+
"code": "200",
8+
"size": "2231",
9+
"userIp": "127.0.0.1",
10+
"occurred": "1993-03-09T00:00:00.000Z",
11+
"recorded": "1993-03-09T00:00:00.000Z"
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"accessLogs": {
3+
"items": [
4+
{
5+
"accessLog": {
6+
"id": "123",
7+
"host": "visa.gooddata.com",
8+
"path": "/gdc/ping",
9+
"method": "GET",
10+
"code": "200",
11+
"size": "2231",
12+
"userIp": "127.0.0.1",
13+
"occurred": "1993-03-09T00:00:00.000Z",
14+
"recorded": "1993-03-09T00:00:00.000Z"
15+
}
16+
},
17+
{
18+
"accessLog": {
19+
"id": "456",
20+
"host": "mastercard.gooddata.com",
21+
"path": "/gdc/ping",
22+
"method": "GET",
23+
"code": "200",
24+
"size": "2231",
25+
"userIp": "127.0.0.1",
26+
"occurred": "1993-03-09T00:00:00.000Z",
27+
"recorded": "1993-03-09T00:00:00.000Z"
28+
}
29+
}
30+
],
31+
"paging": {
32+
"next": "/gdc/domains/default/accessLogs?offset=456"
33+
},
34+
"links": {
35+
"self": "/gdc/domains/default/accessLogs"
36+
}
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"accessLogs": {
3+
"items": [
4+
],
5+
"paging": {
6+
},
7+
"links": {
8+
"self": "/gdc/domains/default/accessLogs"
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)