Skip to content

Commit

Permalink
Added dates to Change and Patchset
Browse files Browse the repository at this point in the history
Added createdOn for Change and Patchset as well as lastUpdated
for Change. These values were previously thrown away but are now
saved.

Change-Id: I1fef9a7019101e89afb942d640168a43ab2b15ba
  • Loading branch information
TWestling committed Aug 27, 2014
1 parent 963b0fc commit a154e14
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2014 Sony Mobile Communications AB. All rights reserved.
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,6 +26,8 @@
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventType;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritJsonEvent;
import java.lang.reflect.Constructor;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
Expand Down Expand Up @@ -187,8 +188,8 @@ public static GerritJsonEvent getEventIfInteresting(String jsonString) {
/**
* Returns the value of a JSON property as a String if it exists otherwise returns the defaultValue.
* @param json the JSONObject to check.
* @param key the key
* @param defaultValue the value to return if the key is missing
* @param key the key.
* @param defaultValue the value to return if the key is missing.
* @return the value for the key as a string.
*/
public static String getString(JSONObject json, String key, String defaultValue) {
Expand All @@ -204,7 +205,7 @@ public static String getString(JSONObject json, String key, String defaultValue)
* Same as calling {@link #getString(net.sf.json.JSONObject, java.lang.String, java.lang.String) }
* with null as defaultValue.
* @param json the JSONObject to check.
* @param key the key
* @param key the key.
* @return the value for the key as a string.
*/
public static String getString(JSONObject json, String key) {
Expand All @@ -215,7 +216,7 @@ public static String getString(JSONObject json, String key) {
* Returns the value of a JSON property as a boolean if it exists and boolean value
* otherwise returns the defaultValue.
* @param json the JSONObject to check.
* @param key the key
* @param key the key.
* @param defaultValue the value to return if the key is missing or not boolean value.
* @return the value for the key as a boolean.
*/
Expand All @@ -236,10 +237,43 @@ public static boolean getBoolean(JSONObject json, String key, boolean defaultVal
/**
* Returns the value of a JSON property as a boolean if it exists otherwise returns false.
* @param json the JSONObject to check.
* @param key the key
* @param key the key.
* @return the value for the key as a boolean.
*/
public static boolean getBoolean(JSONObject json, String key) {
return getBoolean(json, key, false);
}

/**
* Returns the value of a JSON property as a Date if it exists otherwise returns null.
* @param json the JSONObject to check.
* @param key the key.
* @return the value for the key as a Date.
*/
public static Date getDate(JSONObject json, String key) {
return getDate(json, key, null);
}

/**
* Returns the value of a JSON property as a Date if it exists otherwise returns the defaultValue.
* @param json the JSONObject to check.
* @param key the key.
* @param defaultValue the value to return if the key is missing.
* @return the value for the key as a Date.
*/
public static Date getDate(JSONObject json, String key, Date defaultValue) {
Date result = defaultValue;
if (json.containsKey(key)) {
try {
String secondsString = json.getString(key);
//In gerrit, time is written in seconds, not milliseconds.
Long milliseconds = TimeUnit.SECONDS.toMillis(Long.parseLong(secondsString));
result = new Date(milliseconds);
// CS IGNORE EmptyBlockCheck FOR NEXT 2 LINES. REASON: result is already set to defaultValue.
} catch (JSONException ex) {
} catch (NumberFormatException nfe) {
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2012 Sony Mobile Communications AB. All rights reserved.
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -214,6 +213,14 @@ public abstract class GerritEventKeys {
* reviewer.
*/
public static final String REVIEWER = "reviewer";
/**
* createdOn.
*/
public static final String CREATED_ON = "createdOn";
/**
* lastUpdated.
*/
public static final String LAST_UPDATED = "lastUpdated";
/**
* Empty default constructor to hinder instantiation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2013 Sony Mobile Communications AB. All rights reserved.
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,14 +24,17 @@
package com.sonymobile.tools.gerrit.gerritevents.dto.attr;

import com.sonymobile.tools.gerrit.gerritevents.dto.GerritJsonDTO;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getString;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getString;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getDate;

import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.BRANCH;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.COMMENTS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.COMMIT_MESSAGE;
Expand All @@ -43,6 +45,9 @@
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PROJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.SUBJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.URL;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CREATED_ON;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.LAST_UPDATED;


/**
* Represents a Gerrit JSON Change DTO.
Expand Down Expand Up @@ -88,6 +93,16 @@ public class Change implements GerritJsonDTO {
*/
private String url;

/**
* The Date when this change was created.
*/
private Date createdOn;

/**
* The Date when this change was last updated.
*/
private Date lastUpdated;

private List<Comment> comments;
/**
* Default constructor.
Expand All @@ -112,6 +127,8 @@ public void fromJson(JSONObject json) {
id = getString(json, ID);
number = getString(json, NUMBER);
subject = getString(json, SUBJECT);
createdOn = getDate(json, CREATED_ON);
lastUpdated = getDate(json, LAST_UPDATED);
if (json.containsKey(OWNER)) {
owner = new Account(json.getJSONObject(OWNER));
}
Expand Down Expand Up @@ -282,6 +299,38 @@ public void setUrl(String url) {
this.url = url;
}

/**
* The Date this change was created on.
* @return the Date.
*/
public Date getCreatedOn() {
return createdOn;
}

/**
* The Date this change was created on.
* @param date the Date.
*/
public void setCreatedOn(Date date) {
this.createdOn = date;
}

/**
* The Date this change was last updated.
* @return the Date.
*/
public Date getLastUpdated() {
return lastUpdated;
}

/**
* The Date this change was last updated.
* @param date the Date.
*/
public void setLastUpdated(Date date) {
this.lastUpdated = date;
}

@Override
public boolean equals(Object obj) {
//CS IGNORE MagicNumber FOR NEXT 14 LINES. REASON: Autogenerated Code.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2014 Sony Mobile Communications AB. All rights reserved.
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,16 +23,19 @@
*/
package com.sonymobile.tools.gerrit.gerritevents.dto.attr;

import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getString;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getBoolean;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritChangeKind;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritJsonDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getDate;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getString;
import static com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory.getBoolean;

import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NUMBER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.REVISION;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.REF;
Expand All @@ -43,6 +45,7 @@
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.AUTHOR;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.APPROVALS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PARENTS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CREATED_ON;

/**
* Represents a Gerrit JSON Patchset DTO.
Expand Down Expand Up @@ -87,7 +90,10 @@ public class PatchSet implements GerritJsonDTO {
* The list of parent ids.
*/
private List<String> parents;

/**
* The Date when this change was created.
*/
private Date createdOn;
/**
* Default constructor.
*/
Expand All @@ -107,6 +113,7 @@ public void fromJson(JSONObject json) {
number = getString(json, NUMBER);
revision = getString(json, REVISION);
draft = getBoolean(json, IS_DRAFT);
createdOn = getDate(json, CREATED_ON);
if (json.containsKey(KIND)) {
kind = GerritChangeKind.fromString(getString(json, KIND));
}
Expand Down Expand Up @@ -258,6 +265,22 @@ public void setUploader(Account uploader) {
this.uploader = uploader;
}

/**
* The Date this change was created on.
* @return the Date.
*/
public Date getCreatedOn() {
return createdOn;
}

/**
* The Date this change was created on.
* @param date the Date.
*/
public void setCreatedOn(Date date) {
this.createdOn = date;
}

@Override
public boolean equals(Object obj) {
//CS IGNORE MagicNumber FOR NEXT 15 LINES. REASON: Autogenerated Code.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2014 Sony Mobile Communications AB. All rights reserved.
* Copyright 2010 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,6 +29,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PROJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.BRANCH;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.ID;
Expand All @@ -42,6 +44,9 @@
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.COMMENTS;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.MESSAGE;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.REVIEWER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CREATED_ON;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.LAST_UPDATED;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -160,6 +165,32 @@ public void testFromJsonWithNonEmptyComments() throws Exception {
assertEquals(1, change.getComments().size());
}

/**
* Tests {@link Change#fromJson(net.sf.json.JSONObject)}.
* With date values, createdOn and lastUpdated.
* @throws Exception if so.
*/
@Test
// CS IGNORE MagicNumber FOR NEXT 3 LINES. REASON: TestData
public void testFromJsonWithDateValues() throws Exception {
long createdOn = 100000000L;
long lastUpdated = 110000000L;
JSONObject json = new JSONObject();
//In gerrit, time is written in seconds, not milliseconds.
long createdOnInMilliseconds = TimeUnit.SECONDS.toMillis(createdOn);
Date createdOnAsDate = new Date(createdOnInMilliseconds);
//In gerrit, time is written in seconds, not milliseconds.
long lastUpdatedInMilliseconds = TimeUnit.SECONDS.toMillis(lastUpdated);
Date lastUpdatedAsDate = new Date(lastUpdatedInMilliseconds);
json.put(CREATED_ON, createdOn);
json.put(LAST_UPDATED, lastUpdated);
Change change = new Change();
change.fromJson(json);

assertEquals(createdOnAsDate, change.getCreatedOn());
assertEquals(lastUpdatedAsDate, change.getLastUpdated());
}

/**
* Tests {@link com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change#Change(net.sf.json.JSONObject)}.
* @throws Exception if so.
Expand Down
Loading

0 comments on commit a154e14

Please sign in to comment.