Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.gooddata.sdk.common.util.GoodDataToStringBuilder;
import com.gooddata.sdk.common.util.ISOZonedDateTime;

import java.time.ZonedDateTime;

/**
* Model class, used for special audit log events/access logs directly from haproxy,
* that represents access logs for particular hosts.
*/
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("accessLog")
@JsonIgnoreProperties(ignoreUnknown = true)
public class AccessLog {

public static final String RESOURCE_URI = "/gdc/domains/{domainId}/accessLogs";

private final String id;
private final String host;
private final String path;
private final String method;
private final String code;
private final String size;
private final String userIp;
@ISOZonedDateTime
private final ZonedDateTime occurred;
@ISOZonedDateTime
private final ZonedDateTime recorded;

@JsonCreator
public AccessLog(@JsonProperty("id") String id, @JsonProperty("host") String host, @JsonProperty("path") String path,
@JsonProperty("method") String method, @JsonProperty("code") String code, @JsonProperty("size") String size,
@JsonProperty("userIp") String userIp, @JsonProperty("occurred") ZonedDateTime occurred, @JsonProperty("recorded") ZonedDateTime recorded) {
this.id = id;
this.host = host;
this.path = path;
this.method = method;
this.code = code;
this.size = size;
this.userIp = userIp;
this.occurred = occurred;
this.recorded = recorded;
}

public String getId() {
return id;
}

public String getHost() {
return host;
}

public String getPath() {
return path;
}

public String getMethod() {
return method;
}

public String getCode() {
return code;
}

public String getSize() {
return size;
}

public String getUserIp() {
return userIp;
}

public ZonedDateTime getOccurred() {
return occurred;
}

public ZonedDateTime getRecorded() {
return recorded;
}

@Override
public String toString() {
return GoodDataToStringBuilder.defaultToString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.gooddata.sdk.common.collections.Page;
import com.gooddata.sdk.common.collections.Paging;

import java.util.List;
import java.util.Map;

@JsonTypeInfo(
include = JsonTypeInfo.As.WRAPPER_OBJECT,
use = JsonTypeInfo.Id.NAME
)
@JsonTypeName("accessLogs")
@JsonIgnoreProperties(
ignoreUnknown = true
)
@JsonSerialize(
using = AccessLogsSerializer.class
)
@JsonDeserialize(
using = AccessLogsDeserializer.class
)
public class AccessLogs extends Page<AccessLog> {
static final String ROOT_NODE = "accessLogs";

public AccessLogs(List<AccessLog> items, Paging paging, Map<String, String> links) {
super(items, paging, links);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import com.gooddata.sdk.common.collections.PageDeserializer;
import com.gooddata.sdk.common.collections.Paging;

import java.util.List;
import java.util.Map;

public class AccessLogsDeserializer extends PageDeserializer<AccessLogs, AccessLog> {

public AccessLogsDeserializer() { super(AccessLog.class); }

@Override
protected AccessLogs createPage(List<AccessLog> items, Paging paging, Map<String, String> links) {
return new AccessLogs(items, paging, links);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import com.gooddata.sdk.common.collections.PageSerializer;

public class AccessLogsSerializer extends PageSerializer {
public AccessLogsSerializer() {
super(AccessLogs.ROOT_NODE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import org.testng.annotations.Test;

import java.time.LocalDate;
import java.time.ZonedDateTime;

import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
import static java.time.ZoneOffset.UTC;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static net.javacrumbs.jsonunit.core.util.ResourceUtils.resource;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class AccessLogTest {

private static final ZonedDateTime DATE = ZonedDateTime.of(LocalDate.of(1993, 3, 9).atStartOfDay(), UTC);

private final AccessLog ACCESS_LOG = new AccessLog("123", "visa.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);

@Test
public void testSerialize() throws Exception {
assertThat(ACCESS_LOG, jsonEquals(resource("auditevents/accessLog.json")));
}

@Test
public void testDeserialize() throws Exception {
final AccessLog deserializedObject = readObjectFromResource("/auditevents/accessLog.json", AccessLog.class);
assertThat(deserializedObject, notNullValue());
assertThat(deserializedObject.getId(), is(ACCESS_LOG.getId()));
assertThat(deserializedObject.getHost(), is(ACCESS_LOG.getHost()));
assertThat(deserializedObject.getPath(), is(ACCESS_LOG.getPath()));
assertThat(deserializedObject.getMethod(), is(ACCESS_LOG.getMethod()));
assertThat(deserializedObject.getCode(), is(ACCESS_LOG.getCode()));
assertThat(deserializedObject.getSize(), is(ACCESS_LOG.getSize()));
assertThat(deserializedObject.getUserIp(), is(ACCESS_LOG.getUserIp()));
assertThat(deserializedObject.getOccurred(), is(ACCESS_LOG.getOccurred()));
assertThat(deserializedObject.getRecorded(), is(ACCESS_LOG.getRecorded()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.sdk.model.auditevent;

import com.gooddata.sdk.common.collections.Paging;
import org.springframework.web.util.UriTemplate;
import org.testng.annotations.Test;

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;

import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
import static java.time.ZoneOffset.UTC;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static net.javacrumbs.jsonunit.core.util.ResourceUtils.resource;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;

public class AccessLogsTest {

private static final ZonedDateTime DATE = ZonedDateTime.of(LocalDate.of(1993, 3, 9).atStartOfDay(), UTC);

private final AccessLog ACCESS_LOG_1 = new AccessLog("123", "visa.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);
private final AccessLog ACCESS_LOG_2 = new AccessLog("456", "mastercard.gooddata.com", "/gdc/ping", "GET", "200", "2231", "127.0.0.1", DATE, DATE);

private static final String DOMAIN = "default";
private static final String RESOURCE_URI = new UriTemplate(AccessLog.RESOURCE_URI).expand(DOMAIN).toString();
private static final String RESOURCE_NEXT_URI = RESOURCE_URI + "?offset=456";

private final AccessLogs ACCESS_LOGS = new AccessLogs(
asList(ACCESS_LOG_1, ACCESS_LOG_2),
new Paging(RESOURCE_NEXT_URI),
singletonMap("self", RESOURCE_URI)
);

private final AccessLogs EMPTY_ACCESS_LOGS = new AccessLogs(
Collections.emptyList(),
new Paging(null),
singletonMap("self", RESOURCE_URI)
);

@Test
public void shouldSerialize() throws Exception {
assertThat(ACCESS_LOGS, jsonEquals(resource("auditevents/accessLogs.json")));
}

@Test
public void shouldDeserialize() throws Exception {
final AccessLogs deserialized = readObjectFromResource("/auditevents/accessLogs.json", AccessLogs.class);
assertThat(deserialized.getPaging().getNextUri(), is(RESOURCE_NEXT_URI));
final List<AccessLog> pageItems = deserialized.getPageItems();
assertThat(pageItems, hasSize(2));
assertThat(pageItems.get(0).getId(), is(ACCESS_LOG_1.getId()));
assertThat(pageItems.get(1).getId(), is(ACCESS_LOG_2.getId()));
}

@Test
public void testSerializeEmptyAccessLogs() throws Exception {
assertThat(EMPTY_ACCESS_LOGS, jsonEquals(resource("auditevents/emptyAccessLogs.json")));
}

@Test
public void testDeserializeEmptyEvents() throws Exception {
final AccessLogs deserialized = readObjectFromResource("/auditevents/emptyAccessLogs.json", AccessLogs.class);
assertThat(deserialized.getPaging().getNextUri(), nullValue());
assertThat(deserialized.getPageItems(), hasSize(0));
}

}
13 changes: 13 additions & 0 deletions gooddata-java-model/src/test/resources/auditevents/accessLog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"accessLog": {
"id": "123",
"host": "visa.gooddata.com",
"path": "/gdc/ping",
"method": "GET",
"code": "200",
"size": "2231",
"userIp": "127.0.0.1",
"occurred": "1993-03-09T00:00:00.000Z",
"recorded": "1993-03-09T00:00:00.000Z"
}
}
38 changes: 38 additions & 0 deletions gooddata-java-model/src/test/resources/auditevents/accessLogs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"accessLogs": {
"items": [
{
"accessLog": {
"id": "123",
"host": "visa.gooddata.com",
"path": "/gdc/ping",
"method": "GET",
"code": "200",
"size": "2231",
"userIp": "127.0.0.1",
"occurred": "1993-03-09T00:00:00.000Z",
"recorded": "1993-03-09T00:00:00.000Z"
}
},
{
"accessLog": {
"id": "456",
"host": "mastercard.gooddata.com",
"path": "/gdc/ping",
"method": "GET",
"code": "200",
"size": "2231",
"userIp": "127.0.0.1",
"occurred": "1993-03-09T00:00:00.000Z",
"recorded": "1993-03-09T00:00:00.000Z"
}
}
],
"paging": {
"next": "/gdc/domains/default/accessLogs?offset=456"
},
"links": {
"self": "/gdc/domains/default/accessLogs"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"accessLogs": {
"items": [
],
"paging": {
},
"links": {
"self": "/gdc/domains/default/accessLogs"
}
}
}
Loading