Skip to content

Commit

Permalink
Merge pull request #69 from dennislloydjr/compliance-data-changes
Browse files Browse the repository at this point in the history
Compliance data changes
  • Loading branch information
jcodagnone authored Jan 13, 2017
2 parents f228619 + 209a196 commit e843db5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.zaubersoftware.gnip4j.api.impl.formats;

import java.io.IOException;

import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import com.zaubersoftware.gnip4j.api.support.logging.LoggerFactory;
import com.zaubersoftware.gnip4j.api.support.logging.spi.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.io.UncheckedIOException;

/**
* Translates JSON input from the Compliance v2 stream into instances of Activity.
Expand All @@ -41,11 +40,9 @@ public class ComplianceActivityUnmarshaller implements Unmarshaller<Activity> {
public final Activity unmarshall(final String s) {
try {
return mapper.readValue(s, ComplianceActivity.class).toActivity();
} catch (final JsonMappingException e) {
} catch (final IOException | UncheckedIOException e) {
logger.warn("Failed to parse compliance activity: " + s, e);
return null;
} catch (final IOException e) {
throw new IllegalArgumentException("parsing compliance activity", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2011-2016 Zauber S.A. <http://flowics.com/>
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,36 +15,37 @@
*/
package com.zaubersoftware.gnip4j.api.model.compliance;

import java.io.Serializable;

import com.zaubersoftware.gnip4j.api.model.Activity;
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;

import com.zaubersoftware.gnip4j.api.model.Activity;
import java.io.Serializable;

/**
* Activity from the Compliance v2 stream that can be converted to an Activity
*
* @author Dennis Lloyd Jr
* @since Oct 14, 2016
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value=DeleteStatusActivity.class, name=ComplianceActivity.Verb.DELETE),
@JsonSubTypes.Type(value=DeleteUserActivity.class, name=ComplianceActivity.Verb.USER_DELETE),
@JsonSubTypes.Type(value=UndeleteUserActivity.class, name=ComplianceActivity.Verb.USER_UNDELETE),
@JsonSubTypes.Type(value=ScrubGeoActivity.class, name=ComplianceActivity.Verb.SCRUB_GEO),
@JsonSubTypes.Type(value=UserProtectActivity.class, name=ComplianceActivity.Verb.USER_PROTECT),
@JsonSubTypes.Type(value=UserUnprotectActivity.class, name=ComplianceActivity.Verb.USER_UNPROTECT),
@JsonSubTypes.Type(value=UserSuspendActivity.class, name=ComplianceActivity.Verb.USER_SUSPEND),
@JsonSubTypes.Type(value=UserUnsuspendActivity.class, name=ComplianceActivity.Verb.USER_UNSUSPEND),
@JsonSubTypes.Type(value=StatusWithheldActivity.class, name=ComplianceActivity.Verb.STATUS_WITHHELD)
@JsonSubTypes.Type(value = DeleteActivity.class, name = ComplianceActivity.Verb.DELETE),
@JsonSubTypes.Type(value = DeleteUserActivity.class, name = ComplianceActivity.Verb.USER_DELETE),
@JsonSubTypes.Type(value = UndeleteUserActivity.class, name = ComplianceActivity.Verb.USER_UNDELETE),
@JsonSubTypes.Type(value = ScrubGeoActivity.class, name = ComplianceActivity.Verb.SCRUB_GEO),
@JsonSubTypes.Type(value = UserProtectActivity.class, name = ComplianceActivity.Verb.USER_PROTECT),
@JsonSubTypes.Type(value = UserUnprotectActivity.class, name = ComplianceActivity.Verb.USER_UNPROTECT),
@JsonSubTypes.Type(value = UserSuspendActivity.class, name = ComplianceActivity.Verb.USER_SUSPEND),
@JsonSubTypes.Type(value = UserUnsuspendActivity.class, name = ComplianceActivity.Verb.USER_UNSUSPEND),
@JsonSubTypes.Type(value = StatusWithheldActivity.class, name = ComplianceActivity.Verb.STATUS_WITHHELD),
@JsonSubTypes.Type(value = UserWithheldActivity.class, name = ComplianceActivity.Verb.USER_WITHHELD)
})
public interface ComplianceActivity extends Serializable {
Activity toActivity();

interface Verb {
String DELETE = "delete";
String FAVORITE_DELETE = "favorite_delete";
String USER_DELETE = "user_delete";
String USER_UNDELETE = "user_undelete";
String SCRUB_GEO = "scrub_geo";
Expand All @@ -53,5 +54,6 @@ interface Verb {
String USER_SUSPEND = "user_suspend";
String USER_UNSUSPEND = "user_unsuspend";
String STATUS_WITHHELD = "status_withheld";
String USER_WITHHELD = "user_withheld";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.zaubersoftware.gnip4j.api.model.compliance;

import java.io.UncheckedIOException;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonTypeName;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.Actor;
import org.codehaus.jackson.map.JsonMappingException;

/**
* Represents deleting a status from the Compliance v2 stream
Expand All @@ -30,9 +32,11 @@
* @since Oct 14, 2016
*/
@JsonTypeName(ComplianceActivity.Verb.DELETE)
class DeleteStatusActivity implements ComplianceActivity {
class DeleteActivity implements ComplianceActivity {
@JsonProperty("status")
private Status status;
@JsonProperty("favorite")
private Favorite favorite;
@JsonProperty("timestamp_ms")
private String timestamp;

Expand All @@ -43,13 +47,31 @@ public static class Status {
private String userId;
}

public static class Favorite {
@JsonProperty("id")
private String id;
@JsonProperty("user_id")
private String userId;
}

@Override
public Activity toActivity() {
final Activity result = new Activity();
result.setVerb(ComplianceActivity.Verb.DELETE);
result.setId(status.id);
result.setActor(new Actor());
result.getActor().setId(status.userId);

if(status != null) {
result.setVerb(ComplianceActivity.Verb.DELETE);
result.setId(status.id);
result.setActor(new Actor());
result.getActor().setId(status.userId);
} else if (favorite != null) {
result.setVerb(ComplianceActivity.Verb.FAVORITE_DELETE);
result.setId(favorite.id);
result.setActor(new Actor());
result.getActor().setId(favorite.userId);
} else {
throw new UncheckedIOException(new JsonMappingException("Expected delete of 'status' or 'favorite'"));
}

result.setUpdated(new Date(Long.parseLong(timestamp)));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.zaubersoftware.gnip4j.api.model.compliance;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.Actor;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonTypeName;

import java.util.Collection;
import java.util.Date;

/**
* Represents a user is withheld in the Compliance v2 Stream
*
* @author Dennis Lloyd Jr
* @since Dec 13, 2016
*/
@JsonTypeName(ComplianceActivity.Verb.USER_WITHHELD)
public class UserWithheldActivity implements ComplianceActivity {
@JsonProperty("id")
private String id;

@JsonProperty("withheld_in_countries")
private Collection<String> withheldInCountries;

@JsonProperty("timestamp_ms")
private String timestamp;

@Override
public Activity toActivity() {
final Activity result = new Activity();
result.setVerb(ComplianceActivity.Verb.USER_WITHHELD);
result.setActor(new Actor());
result.getActor().setId(id);
result.setWithheldInCountries(withheldInCountries);
result.setUpdated(new Date(Long.parseLong(timestamp)));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package com.zaubersoftware.gnip4j.api.impl.formats;

import static org.junit.Assert.*;
import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* @author Dennis Lloyd Jr
Expand All @@ -48,6 +48,23 @@ public void deleteStatus() {
assertEquals(new Date(1432228155593L), result.getUpdated());
}

@Test
public void deleteFavorite() {
final String s = "{\"delete\":{\"favorite\":{\"id\":601430178305220600,\"id_str\":\"601430178305220600\",\"user_id\":3198576760,\"user_id_str\":\"3198576760\"},\"timestamp_ms\":\"1432228155593\"}}";
final Activity result = unmarshaller.unmarshall(s);
assertEquals(ComplianceActivity.Verb.FAVORITE_DELETE, result.getVerb());
assertEquals("601430178305220600", result.getId());
assertEquals("3198576760", result.getActor().getId());
assertEquals(new Date(1432228155593L), result.getUpdated());
}

@Test
public void deleteUnknown() { // What happens when we receive data for a delete that we don't expect
final String s = "{\"delete\":{\"unknown\":{\"id\":601430178305220600,\"id_str\":\"601430178305220600\",\"user_id\":3198576760,\"user_id_str\":\"3198576760\"},\"timestamp_ms\":\"1432228155593\"}}";
final Activity result = unmarshaller.unmarshall(s);
assertNull(result);
}

@Test
public void deleteUser() {
final String s = "{\"user_delete\":{\"id\":771136850,\"timestamp_ms\":\"1432228153548\"}}";
Expand Down Expand Up @@ -123,6 +140,16 @@ public void statusWithheld() {
assertEquals(new Date(1476731813446L), result.getUpdated());
}

@Test
public void userWithheld() {
final String s = "{\"user_withheld\":{\"id\":1375036644,\"timestamp_ms\":\"1481626100690\",\"withheld_in_countries\":[\"TR\"]}}";
final Activity result = unmarshaller.unmarshall(s);
assertEquals(ComplianceActivity.Verb.USER_WITHHELD, result.getVerb());
assertEquals("1375036644", result.getActor().getId());
assertEquals(Arrays.asList("TR"), result.getWithheldInCountries());
assertEquals(new Date(1481626100690L), result.getUpdated());
}

@Test
public void unrecognizedMesssageType() {
final String s = "{\"unknown\":{\"id\":3293130873,\"timestamp_ms\":\"1432228193828\"}}";
Expand Down

0 comments on commit e843db5

Please sign in to comment.