Skip to content

Commit

Permalink
Added model parsing tests from fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
sleroux committed Dec 1, 2012
1 parent aedd6e3 commit 152129d
Show file tree
Hide file tree
Showing 24 changed files with 621 additions and 33 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion src/main/java/postageapp/PostageAppClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private String sendRequest(String endpoint, String content) throws PostageAppExc

this.checkForError(response);

return (Map<String, ?>) response.get("data");
return (Map<String, ?>) responseMap.get("data");
}

private void checkForError(Map<String, ?> responseJson) throws PostageAppException {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/postageapp/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
*/
public class Message extends PostageModel {
private String uid, template;
private int transmissionsTotal, transmissionsFailed, transmissionsCompleted;
private double transmissionsTotal, transmissionsFailed, transmissionsCompleted;
private Date createdAt, willPurgeAt;

public Message(String uid, Map<String, ?> json) {
super(json);

this.uid = uid;
this.template = (String) json.get("template");
this.transmissionsTotal = (Integer) json.get("transmissions_total");
this.transmissionsCompleted = (Integer) json.get("transmissions_completed");
this.transmissionsFailed = (Integer) json.get("transmissions_failed");
this.transmissionsTotal = (Double) json.get("transmissions_total");
this.transmissionsCompleted = (Double) json.get("transmissions_completed");
this.transmissionsFailed = (Double) json.get("transmissions_failed");
this.createdAt = this.dateFromString((String) json.get("created_at"));
this.willPurgeAt = this.dateFromString((String) json.get("will_purge_at"));
}
Expand All @@ -35,15 +35,15 @@ public String getTemplate() {
return template;
}

public int getTransmissionsCompleted() {
public double getTransmissionsCompleted() {
return transmissionsCompleted;
}

public int getTransmissionsFailed() {
public double getTransmissionsFailed() {
return transmissionsFailed;
}

public int getTransmissionsTotal() {
public double getTransmissionsTotal() {
return transmissionsTotal;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/postageapp/models/MessageTransmission.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* To change this template use File | Settings | File Templates.
*/
public class MessageTransmission extends PostageModel {
private String status, resultCode, resultMessage;
private String status, resultCode, errorMessage;
private Date createdAt, failedAt, openedAt;

public MessageTransmission(Map<String, ?> json) {
Expand All @@ -26,7 +26,7 @@ public MessageTransmission(Map<String, ?> json) {
this.failedAt = this.dateFromString((String) json.get("failed_at"));
this.openedAt = this.dateFromString((String) json.get("opened_at"));
this.resultCode = (String) json.get("result_code");
this.resultMessage = (String) json.get("result_message");
this.errorMessage = (String) json.get("error_message");
}

public Date getCreatedAt() {
Expand All @@ -45,8 +45,8 @@ public String getResultCode() {
return resultCode;
}

public String getResultMessage() {
return resultMessage;
public String getErrorMessage() {
return errorMessage;
}

public String getStatus() {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/postageapp/models/MessageTransmissonsResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package postageapp.models;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -10,11 +11,11 @@
* To change this template use File | Settings | File Templates.
*/
public class MessageTransmissonsResponse {
private String messageId;
private Map<String, MessageTransmission> transmissions;
private double messageId;
private Map<String, MessageTransmission> transmissions = new HashMap<String, MessageTransmission>();

public MessageTransmissonsResponse(Map<String, ?> json) {
Map<String, String> messageMap = (Map<String, String>) json.get("message");
Map<String, Double> messageMap = (Map<String, Double>) json.get("message");
this.messageId = messageMap.get("id");

Map<String, ?> transmissionsMap = (Map<String, ?>) json.get("transmissions");
Expand All @@ -23,4 +24,12 @@ public MessageTransmissonsResponse(Map<String, ?> json) {
this.transmissions.put(email, new MessageTransmission((Map<String, String>) transmissionsMap.get(email)));
}
}

public double getMessageId() {
return this.messageId;
}

public Map<String, MessageTransmission> getTransmissions() {
return this.transmissions;
}
}
11 changes: 6 additions & 5 deletions src/main/java/postageapp/models/PostageModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package postageapp.models;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -14,18 +17,16 @@
* To change this template use File | Settings | File Templates.
*/
public abstract class PostageModel {
private final DateFormat dateFormat = new SimpleDateFormat("YYY-MM-dd hh:mm:ss");
private Map<String, ?> json;
private DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");

public PostageModel(Map<String, ?> json) {
this.json = json;
}

protected Date dateFromString(String date) {
try {
return this.dateFormat.parse((String) json.get("created_at"));
} catch (ParseException e) {
e.printStackTrace();
if (date != null) {
return this.formatter.parseDateTime(date).toDate();
}

return null;
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/postageapp/models/ProjectMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
* To change this template use File | Settings | File Templates.
*/
public class ProjectMetrics extends PostageModel {
private int currentPercent, previousPercent,
private double currentPercent, previousPercent,
diffPercent, currentValue, previousValue;

public ProjectMetrics(Map<String, ?> json) {
super(json);

this.currentPercent = (Integer) json.get("currentPercent");
this.previousPercent = (Integer) json.get("previousPercent");
this.diffPercent = (Integer) json.get("diffPercent");
this.currentValue = (Integer) json.get("currentValue");
this.previousPercent = (Integer) json.get("previousPercent");
this.previousValue = (Integer) json.get("previousValue");
this.currentPercent = (Double) json.get("current_percent");
this.previousPercent = (Double) json.get("previous_percent");
this.diffPercent = (Double) json.get("diff_percent");
this.currentValue = (Double) json.get("current_value");
this.previousPercent = (Double) json.get("previous_percent");
this.previousValue = (Double) json.get("previous_value");
}

public int getCurrentPercent() {
public double getCurrentPercent() {
return currentPercent;
}

public int getCurrentValue() {
public double getCurrentValue() {
return currentValue;
}

public int getDiffPercent() {
public double getDiffPercent() {
return diffPercent;
}

public int getPreviousPercent() {
public double getPreviousPercent() {
return previousPercent;
}

public int getPreviousValue() {
public double getPreviousValue() {
return previousValue;
}
}
33 changes: 32 additions & 1 deletion src/test/java/postageapp/ModelTestCase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package postageapp;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
Expand All @@ -8,5 +16,28 @@
* Time: 7:57 PM
* To change this template use File | Settings | File Templates.
*/
public class ModelTestCase extends TestCase {
public abstract class ModelTestCase extends TestCase {
protected String loadModelFixture(String fixturePath) {
InputStream jsonStream = getClass().getClassLoader().getResourceAsStream(fixturePath);
assertNotNull(jsonStream);

String jsonString = null;
try {
jsonString = IOUtils.toString(jsonStream);
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}

protected Map<String, ?> getDataFromResponse(String jsonString) {
Gson gson = new Gson();

Type mapType = new TypeToken<Map<String, ?>>() {
}.getType();
Map<String, ?> responseMap = gson.fromJson(jsonString, mapType);

return (Map<String, ?>) responseMap.get("data");
}

}
11 changes: 11 additions & 0 deletions src/test/java/postageapp/PostageAppClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package postageapp;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
* Date: 2012-11-29
* Time: 8:32 PM
* To change this template use File | Settings | File Templates.
*/
public class PostageAppClientTest {
}
11 changes: 11 additions & 0 deletions src/test/java/postageapp/http/PostageAppExceptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package postageapp.http;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
* Date: 2012-11-29
* Time: 8:32 PM
* To change this template use File | Settings | File Templates.
*/
public class PostageAppExceptionTest {
}
11 changes: 11 additions & 0 deletions src/test/java/postageapp/http/PostageAppHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package postageapp.http;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
* Date: 2012-11-29
* Time: 8:32 PM
* To change this template use File | Settings | File Templates.
*/
public class PostageAppHttpClientTest {
}
23 changes: 23 additions & 0 deletions src/test/java/postageapp/models/AccountInfoTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package postageapp.models;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import postageapp.ModelTestCase;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
Expand All @@ -10,4 +19,18 @@
* To change this template use File | Settings | File Templates.
*/
public class AccountInfoTest extends ModelTestCase {
@Test
public void testParseAccountInfoJson() throws Exception {
Map<String, ?> dataObj = this.getDataFromResponse(this.loadModelFixture("mock_api_1.0/mock_account_info.json"));

AccountInfo info = new AccountInfo((Map<String, ?>)dataObj.get("account"));

assertNotNull(info.getName());
assertNotNull(info.getTransmissions());
assertNotNull(info.getTransmissions().get("today"));
assertNotNull(info.getTransmissions().get("this_month"));
assertNotNull(info.getTransmissions().get("overall"));
assertNotNull(info.getUrl());
assertNotNull(info.getUsers());
}
}
29 changes: 29 additions & 0 deletions src/test/java/postageapp/models/MessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package postageapp.models;

import org.junit.Test;
import postageapp.ModelTestCase;

import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
* Date: 2012-11-29
* Time: 8:31 PM
* To change this template use File | Settings | File Templates.
*/
public class MessageTest extends ModelTestCase {
@Test
public void testParseMessageJson() throws Exception {
Map<String, ?> dataObj = this.getDataFromResponse(this.loadModelFixture("mock_api_1.0/mock_get_messages.json"));
Message msg = new Message("27cf6ede7501a32d54d22abe17e3c154d2cae7f3",
(Map<String, ?>)dataObj.get("27cf6ede7501a32d54d22abe17e3c154d2cae7f3"));
assertNotNull(msg.getCreatedAt());
assertNotNull(msg.getTemplate());
assertNotNull(msg.getTransmissionsCompleted());
assertNotNull(msg.getTransmissionsFailed());
assertNotNull(msg.getTransmissionsTotal());
assertNotNull(msg.getUid());
assertNotNull(msg.getWillPurgeAt());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package postageapp.models;

import org.junit.Test;
import postageapp.ModelTestCase;

import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: stephanleroux
* Date: 2012-11-29
* Time: 8:31 PM
* To change this template use File | Settings | File Templates.
*/
public class MessageTransmissionResponseTest extends ModelTestCase {
@Test
public void testParseMessageTransmissionResponseJson() {
Map<String, ?> data = this.getDataFromResponse(this.loadModelFixture("mock_api_1.0/mock_message_transmissions.json"));
MessageTransmissonsResponse response = new MessageTransmissonsResponse(data);
assertNotNull(response);
assertNotNull(response.getMessageId());
assertNotNull(response.getTransmissions());

MessageTransmission transmission = response.getTransmissions().get("[email protected]");
assertNotNull(transmission);
assertNotNull(transmission.getErrorMessage());
assertNotNull(transmission.getCreatedAt());
assertNull(transmission.getFailedAt());
assertNotNull(transmission.getOpenedAt());
assertNotNull(transmission.getResultCode());
assertNotNull(transmission.getStatus());
}
}
Loading

0 comments on commit 152129d

Please sign in to comment.