Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.
Open
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
Expand Up @@ -57,6 +57,7 @@ public class Activity implements Serializable {
private String subtitle;
private String title;
private Object target;
private Info info;

public final InReplyTo getInReplyTo() {
return inReplyTo;
Expand Down Expand Up @@ -196,7 +197,12 @@ public final void setLink(final String value) {
public static class Location implements Serializable {
private static final long serialVersionUID = 1L;
private Geo geo;
@JsonProperty(value = "country_code")
private String countryCode;

@JsonProperty(value = "twitter_country_code")
private String twitterCountryCode;

private String displayName;
private String objectType;
private String streetAddress;
Expand Down Expand Up @@ -226,6 +232,14 @@ public final void setCountryCode(final String value) {
countryCode = value;
}

public String getTwitterCountryCode() {
return twitterCountryCode;
}

public void setTwitterCountryCode(String twitterCountryCode) {
this.twitterCountryCode = twitterCountryCode;
}

public final String getDisplayName() {
return displayName;
}
Expand Down Expand Up @@ -354,4 +368,12 @@ public final Object getTarget() {
public final void setTarget(final Object target) {
this.target = target;
}

public Info getInfo() {
return info;
}

public void setInfo(Info info) {
this.info = info;
}
}
98 changes: 98 additions & 0 deletions core/src/main/java/com/zaubersoftware/gnip4j/api/model/Info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2011-2012 Zauber S.A. <http://www.zaubersoftware.com/>
*
* 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
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaubersoftware.gnip4j.api.model;

import org.codehaus.jackson.annotate.JsonProperty;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
*
*/
public final class Info implements Serializable {
private static final long serialVersionUID = 1;
private String message;
private Date sent;
@JsonProperty(value = "activity_count")
private int activityCount;
@JsonProperty(value = "minutes_failed")
private List<Date> minutesFailed;

SimpleDateFormat dateFormatter;

public Info() {
//2013-02-27T22:15:50+00:00
this.dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ssXXX");
}


public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Date getSent() {
return sent;
}

public void setSent(Date sent) {
this.sent = sent;
}

public int getActivityCount() {
return activityCount;
}

public void setActivityCount(int activityCount) {
this.activityCount = activityCount;
}

public List<Date> getMinutesFailed() {
return minutesFailed;
}

public void setMinutesFailed(List<Date> minutesFailed) {
this.minutesFailed = minutesFailed;
}

public String toString() {
StringBuilder s = new StringBuilder("Info[");
s.append("message:").append(this.message);
s.append(", sent:").append(this.dateFormatter.format(this.sent));
s.append(", activity_count:").append(this.activityCount);
int size = 0;
if (this.minutesFailed != null && (size = this.minutesFailed.size()) > 0) {
s.append(", minutes_failed: [");
int i = 0;
for (Date minute_failed : this.minutesFailed) {
s.append(this.dateFormatter.format(minute_failed));
i++;
if (i < size) {
s.append(", ");
}
}
s.append("]");
}
s.append("]");
return s.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zaubersoftware.gnip4j.api.model.Info;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
Expand All @@ -41,7 +44,7 @@

/**
* Tests the {@link Activity} JSON Deserialization.
*
*
* @author Guido Marucci Blas
* @since May 5, 2011
*/
Expand All @@ -54,16 +57,64 @@ public void setUp() throws Exception {
mapper = JsonActivityFeedProcessor.getObjectMapper();
}


@Test
public void testReplayCompletedMessage() throws Exception {
final InputStream is = getClass().getClassLoader().getResourceAsStream(
"com/zaubersoftware/gnip4j/payload/replay-request-completed.json");
try {
JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
Activity activity = parser.readValueAs(Activity.class);
Info info = activity.getInfo();
assertNotNull(info);
assertEquals("message content", "Replay Request Completed", info.getMessage());
assertEquals("activity count", 8874, info.getActivityCount());
assertTrue("info date",
info.getSent()
.equals(new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ssXXX")
.parse("2013-02-27T22:15:50+00:00")));
} finally {
if (is != null) {
is.close();
}
}
}

@Test
public void testReplayCompletedWithErrorsMessage() throws Exception {
final InputStream is = getClass().getClassLoader().getResourceAsStream(
"com/zaubersoftware/gnip4j/payload/replay-request-completed-with-errors.json");
try {
JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
Activity activity = parser.readValueAs(Activity.class);
Info info = activity.getInfo();
assertNotNull(info);
assertEquals("message content", "Replay Request Completed with Errors", info.getMessage());
assertEquals("activity count", 56333, info.getActivityCount());
assertTrue("info date",
info.getSent()
.equals(new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ssXXX")
.parse("2013-02-27T16:00:02+00:00")));
List<Date> minutesFailed = info.getMinutesFailed();
assertNotNull(minutesFailed);
assertEquals("size of minutesFailed", 2, minutesFailed.size());
} finally {
if (is != null) {
is.close();
}
}
}

/** test a complete unmarshal from the json */
@Test
public void testGetGnip() throws Exception {
final InputStream is = getClass().getClassLoader().getResourceAsStream(
"com/zaubersoftware/gnip4j/payload/payload-example.js");
"com/zaubersoftware/gnip4j/payload/payload-example.js");
try {
final JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
final Activity activity = parser.readValueAs(Activity.class);
final Activity activity2= parser.readValueAs(Activity.class);

assertNotNull(activity.getGnip());
assertNotNull(activity.getGnip().getLanguage());
assertEquals("en", activity.getGnip().getLanguage().getValue());
Expand All @@ -72,9 +123,9 @@ public void testGetGnip() throws Exception {
assertEquals(1, matchingRules.size());
assertEquals("coke", matchingRules.get(0).getValue());
assertEquals(null, matchingRules.get(0).getTag());

final Activity activity3= parser.readValueAs(Activity.class);

assertNotNull(activity3.getTwitterEntities().getMediaUrls());
assertNotNull(activity3.getTwitterEntities().getMediaUrls().get(0).getSizes());
} finally {
Expand All @@ -86,7 +137,7 @@ public void testGetGnip() throws Exception {
@Test
public void testGeoCoordinates() throws Exception {
final InputStream is = getClass().getClassLoader().getResourceAsStream(
"com/zaubersoftware/gnip4j/payload/payload-example-geo.json");
"com/zaubersoftware/gnip4j/payload/payload-example-geo.json");
try {
final JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
final Activity activity = parser.readValueAs(Activity.class);
Expand All @@ -108,51 +159,51 @@ public void testDeserializeWithPolygonAndPoint() throws JsonParseException, IOEx
try {
final JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
final Activities activities = parser.readValueAs(Activities.class);


Geo geo1 = activities.getActivities().get(0).getGeo();
Geo geo2 = activities.getActivities().get(1).getGeo();
Geo geo3 = activities.getActivities().get(0).getLocation().getGeo();
Geo geo4 = activities.getActivities().get(1).getLocation().getGeo();

assertNull(geo1);
assertEquals("lat: 35.11222481 lon: -78.99696934", geo2.getCoordinates().toString());
assertEquals("[[ lat: -0.5093057 lon: 51.286606 ][ lat: 0.334433 lon: 51.286606 ][ lat: 0.334433 lon: 51.691672 ][ lat: -0.5093057 lon: 51.691672 ]]", geo3.getCoordinates().toString());

assertEquals("[[ lat: -79.058407 lon: 35.106225 ][ lat: -78.944666 lon: 35.106225 ][ lat: -78.944666 lon: 35.177993 ][ lat: -79.058407 lon: 35.177993 ]]", geo4.getCoordinates().toString());

} finally {
is.close();
}
}

/*USE THIS TEST TO TEST ENCODING OF JsonParser
* Run this test with -Dfile.encoding=UTF-8 and then with another encoding, and compare the file results
* */
public void utfDesearilzationTest() throws JsonParseException, IOException{
InputStream in = getClass().getClassLoader().getResourceAsStream("com/zaubersoftware/gnip4j/payload/deserialize/utf8_tweets.json");

try {
final JsonParser parser = mapper.getJsonFactory().createJsonParser(in);
final Activities activities = parser.readValueAs(Activities.class);


String body0 = activities.getActivities().get(0).getBody();
String body1 = activities.getActivities().get(1).getBody();
String body2 = activities.getActivities().get(2).getBody();

FileOutputStream fileOutputStream = new FileOutputStream(new File("tweets"));

fileOutputStream.write(body0.getBytes("UTF-8"));
fileOutputStream.write(body1.getBytes("UTF-8"));
fileOutputStream.write(body2.getBytes("UTF-8"));


} finally {
//text.close();
}
}

/**
* tests if the data "model" is serializable
*/
Expand All @@ -165,5 +216,4 @@ public void testSerializable() throws IOException {
final ObjectOutputStream os = new ObjectOutputStream(new ByteArrayOutputStream());
os.writeObject(activity);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"info": {
"message": "Replay Request Completed with Errors",
"sent": "2013-02-27T16:00:02+00:00",
"activity_count": 56333,
"minutes_failed": [
"2013-02-20T00:05:00+00:00",
"2013-02-20T00:06:00+00:00"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"info": {
"message": "Replay Request Completed",
"sent": "2013-02-27T22:15:50+00:00",
"activity_count": 8874
}
}