1
0
Fork 0

Adding test for the Board/Sprint resources

master
Pierre-Luc Dupont 2016-05-19 16:01:14 -04:00
parent 69592a9d4e
commit 04a1cbb6fa
9 changed files with 282 additions and 139 deletions

View File

@ -1,17 +1,17 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* <p>
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@ -65,5 +65,19 @@ public class AgileClient {
public List<Board> getBoards() throws JiraException {
return Board.getAll(restclient);
}
/**
* Retreives the sprint with the given ID.
*
* @param id Sprint ID
*
* @return a Sprint instance
*
* @throws JiraException when something goes wrong
*/
public Sprint getSprint(int id) throws JiraException {
return Sprint.get(restclient, id);
}
}

View File

@ -217,7 +217,7 @@ public abstract class AgileResource {
try {
result = restclient.get(url);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve boards", ex);
throw new JiraException("Failed to retrieve a list of " + type.getSimpleName() + " : " + url, ex);
}
return getResourceArray(
@ -240,7 +240,7 @@ public abstract class AgileResource {
try {
result = restclient.get(url);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve boards", ex);
throw new JiraException("Failed to retrieve " + type.getSimpleName() + " : " + url, ex);
}
return getResource(

View File

@ -83,23 +83,14 @@ public class Board extends AgileResource {
return this.type;
}
/**
* @return All sprints related to the current board.
* @throws JiraException when the retrieval fails
*/
public List<Sprint> getSprints() throws JiraException {
return AgileResource.list(getRestclient(), Sprint.class, RESOURCE_URI + "board/" + getId() + "/sprint");
return Sprint.getAll(getRestclient(), getId());
}
// /**
// * Retrieves the sprint report for the given sprint.
// *
// * @param sprint Sprint to lookup
// *
// * @return the sprint report
// *
// * @throws JiraException when the retrieval fails
// */
// public SprintReport getSprintReport(Sprint sprint) throws JiraException {
// return SprintReport.get(restclient, this, sprint);
// }
//
// /**
// * Retrieves the backlog data for this rapid view.
// *

View File

@ -19,12 +19,12 @@
package net.rcarz.jiraclient.agile;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.Date;
import java.util.List;
/**
@ -34,7 +34,17 @@ import java.util.List;
*/
public class Sprint extends AgileResource {
public static final String ATTR_TYPE = "type";
public static final String ATTR_STATE = "state";
public static final String ATTR_ORIGIN_BOARD_ID = "originBoardId";
public static final String ATTR_START_DATE = "startDate";
public static final String ATTR_END_DATE = "endDate";
public static final String ATTR_COMPLETE_DATE = "completeDate";
private String state;
private int originBoardId;
private Date startDate;
private Date endDate;
private Date completeDate;
/**
* Creates a rapid view from a JSON payload.
@ -46,124 +56,58 @@ public class Sprint extends AgileResource {
super(restclient, json);
}
// /**
// * Retrieves the given rapid view.
// *
// * @param restclient REST client instance
// * @param id Internal JIRA ID of the rapid view
// * @return a rapid view instance
// * @throws JiraException when the retrieval fails
// */
// public static Sprint get(RestClient restclient, int id) throws JiraException {
//
// JSON result = null;
//
// try {
// result = restclient.get(RESOURCE_URI + "board/" + id);
// } catch (Exception ex) {
// throw new JiraException("Failed to retrieve sprint, from board " + id, ex);
// }
//
// if (!(result instanceof JSONObject)) {
// throw new JiraException("JSON payload is malformed");
// }
//
// return new Sprint(restclient, (JSONObject) result);
// }
//
// /**
// * Retrieves all boards visible to the session user.
// *
// * @param restclient REST client instance
// * @return a list of boards
// * @throws JiraException when the retrieval fails
// */
// public static List<Sprint> getAll(RestClient restclient) throws JiraException {
//
// JSON result = null;
// try {
// result = restclient.get(RESOURCE_URI + "board");
// } catch (Exception ex) {
// throw new JiraException("Failed to retrieve boards", ex);
// }
//
// if (!(result instanceof JSONObject)) {
// throw new JiraException("JSON payload is malformed");
// }
//
// JSONObject jo = (JSONObject) result;
//
// if (!jo.containsKey("views") || !(jo.get("views") instanceof JSONArray)) {
// throw new JiraException("Board result is malformed");
// }
//
// return AgileField.getResourceArray(
// Sprint.class,
// jo.get("views"),
// restclient
// );
// }
/**
* @return The board type.
* Retrieve all sprints related to the specified board.
*
* @param restclient REST client instance
* @param sprintId The Internal JIRA sprint ID.
* @return The sprint for the specified ID.
* @throws JiraException when the retrieval fails
*/
public String getType() {
return getAttribute(ATTR_TYPE);
public static Sprint get(RestClient restclient, int sprintId) throws JiraException {
return AgileResource.get(restclient, Sprint.class, RESOURCE_URI + "sprint/" + sprintId);
}
// /**
// * Retrieves all sprints associated with this rapid view.
// *
// * @return a list of sprints
// *
// * @throws JiraException when the retrieval fails
// */
// public List<Sprint> getSprints() throws JiraException {
// JSON result = null;
//
// try {
// result = restclient.get(RESOURCE_URI + "sprintquery/" + id);
// } catch (Exception ex) {
// throw new JiraException("Failed to retrieve sprints", ex);
// }
//
// if (!(result instanceof JSONObject))
// throw new JiraException("JSON payload is malformed");
//
// JSONObject jo = (JSONObject)result;
//
// if (!jo.containsKey("sprints") || !(jo.get("sprints") instanceof JSONArray))
// throw new JiraException("Sprints result is malformed");
//
// return AgileField.getResourceArray(
// Sprint.class,
// jo.get("sprints"),
// restclient
// );
// }
//
// /**
// * Retrieves the sprint report for the given sprint.
// *
// * @param sprint Sprint to lookup
// *
// * @return the sprint report
// *
// * @throws JiraException when the retrieval fails
// */
// public SprintReport getSprintReport(Sprint sprint) throws JiraException {
// return SprintReport.get(restclient, this, sprint);
// }
//
// /**
// * Retrieves the backlog data for this rapid view.
// *
// * @return the backlog
// *
// * @throws JiraException when the retrieval fails
// */
// public Backlog getBacklogData() throws JiraException {
// return Backlog.get(restclient, this);
// }
/**
* Retrieve all sprints related to the specified board.
*
* @param restclient REST client instance
* @param boardId The Internal JIRA board ID.
* @return The list of sprints associated to the board.
* @throws JiraException when the retrieval fails
*/
public static List<Sprint> getAll(RestClient restclient, int boardId) throws JiraException {
return AgileResource.list(restclient, Sprint.class, RESOURCE_URI + "board/" + boardId + "/sprint");
}
@Override
protected void deserialize(JSONObject json) {
super.deserialize(json);
state = Field.getString(json.get(ATTR_STATE));
originBoardId = Field.getInteger(json.get(ATTR_ORIGIN_BOARD_ID));
startDate = Field.getDateTime(json.get(ATTR_START_DATE));
endDate = Field.getDateTime(json.get(ATTR_END_DATE));
completeDate = Field.getDateTime(json.get(ATTR_COMPLETE_DATE));
}
public String getState() {
return state;
}
public int getOriginBoardId() {
return originBoardId;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public Date getCompleteDate() {
return completeDate;
}
}

View File

@ -5,6 +5,7 @@ import net.rcarz.jiraclient.RestClient
import org.hamcrest.core.IsEqual
import org.hamcrest.core.IsNot
import org.hamcrest.core.IsNull
import org.junit.Test
import static org.junit.Assert.assertThat
import static org.mockito.Mockito.mock
@ -35,6 +36,24 @@ class AbstractResourceTest {
assertThat board.getName(), new IsEqual<Integer>(JSONResources.BOARD_84_NAME)
assertThat board.getType(), new IsEqual<Integer>(JSONResources.BOARD_84_TYPE)
assertThat board.getSelfURL(), new IsEqual<Integer>(JSONResources.BOARD_84_SELF)
assertThat board.toString(), new IsEqual<String>(
String.format("Board{id=%s, name='%s'}",
JSONResources.BOARD_84_ID, JSONResources.BOARD_84_NAME))
}
void "Assert equals to Sprint 37"(Sprint sprint) {
assertThat sprint, new IsNot<>(new IsNull())
assertThat sprint.getId(), new IsEqual<Integer>(JSONResources.SPRINT_37_ID)
assertThat sprint.getName(), new IsEqual<Integer>(JSONResources.SPRINT_37_NAME)
assertThat sprint.getSelfURL(), new IsEqual<Integer>(JSONResources.SPRINT_37_SELF)
assertThat sprint.getState(), new IsEqual<Integer>(JSONResources.SPRINT_37_STATE)
assertThat sprint.getOriginBoardId(), new IsEqual<Integer>(JSONResources.SPRINT_37_ORIGIN_BOARD_ID)
assertThat sprint.getStartDate(), new IsEqual<Date>(JSONResources.SPRINT_37_START_DATE)
assertThat sprint.getEndDate(), new IsEqual<Date>(JSONResources.SPRINT_37_END_DATE)
assertThat sprint.getCompleteDate(), new IsEqual<Date>(JSONResources.SPRINT_37_COMPLETE_DATE)
assertThat sprint.toString(), new IsEqual<String>(
String.format("Sprint{id=%s, name='%s'}",
JSONResources.SPRINT_37_ID, JSONResources.SPRINT_37_NAME))
}
}

View File

@ -24,6 +24,7 @@ class AgileClientTest extends AbstractResourceTest {
assertThat boards, new IsNot<>(new IsNull())
assertThat boards.size(), new IsEqual<Integer>(2)
"Assert equals to Board 84"(boards.get(0))
}
@Test
@ -35,5 +36,18 @@ class AgileClientTest extends AbstractResourceTest {
Board board = agileClient.getBoard(84);
assertThat board, new IsNot<>(new IsNull())
"Assert equals to Board 84"(board)
}
@Test
void "Given an agileClient, when calling getSprint(37), then receive one Sprint."() {
"given an Agile Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "sprint/37"))
.thenReturn(JSONSerializer.toJSON(JSONResources.SPRINT_37))
Sprint sprint = agileClient.getSprint(37);
assertThat sprint, new IsNot<>(new IsNull())
"Assert equals to Sprint 37"(sprint)
}
}

View File

@ -1,11 +1,18 @@
package net.rcarz.jiraclient.agile
import net.rcarz.jiraclient.JiraException
import net.rcarz.jiraclient.RestClient
import net.rcarz.jiraclient.RestException
import net.sf.json.JSONSerializer
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.core.IsEqual
import org.hamcrest.core.IsNot
import org.hamcrest.core.IsNull
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.mockito.internal.matchers.Contains
import static org.junit.Assert.assertThat
import static org.mockito.Mockito.when
@ -15,8 +22,11 @@ import static org.mockito.Mockito.when
*/
class BoardTest extends AbstractResourceTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
void "Given an RestClient, when calling getAll(), then receive a list of Board."() {
void "Given a RestClient, when calling getAll(), then receive a list of Board."() {
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board"))
.thenReturn(JSONSerializer.toJSON(JSONResources.LIST_OF_BOARDS))
@ -29,7 +39,19 @@ class BoardTest extends AbstractResourceTest {
}
@Test
void "Given an agileClient, when calling getBoard(84), then receive one Board."() {
void "Given a RestClient, when calling getAll() and use doesn't have access, then throws an 401 error."() {
RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized")
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board"))
.thenThrow(unauthorized)
expectedException.expect(JiraException.class);
expectedException.expectMessage("Failed to retrieve a list of Board : /rest/agile/1.0/board");
List<Board> boards = Board.getAll(mockRestClient);
}
@Test
void "Given a RestClient, when calling getBoard(84), then receive one Board."() {
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/84"))
.thenReturn(JSONSerializer.toJSON(JSONResources.BOARD_84))
@ -38,4 +60,34 @@ class BoardTest extends AbstractResourceTest {
"Assert equals to Board 84"(board)
}
@Test
void "Given a RestClient, when calling getBoard(666), then throws an 404 error."() {
RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized")
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/666"))
.thenThrow(unauthorized)
expectedException.expect(JiraException.class);
expectedException.expectMessage("Failed to retrieve Board : /rest/agile/1.0/board/666");
Board board = Board.get(mockRestClient, 666);
}
@Test
void "Given a valid Board, when calling getSprints(), then receive a list of Sprints."() {
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/84"))
.thenReturn(JSONSerializer.toJSON(JSONResources.BOARD_84))
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/84/sprint"))
.thenReturn(JSONSerializer.toJSON(JSONResources.LIST_OF_SPRINTS))
Board board = Board.get(mockRestClient, 84);
"Assert equals to Board 84"(board)
List<Sprint> sprints = board.getSprints();
assertThat sprints, new IsNot<>(new IsNull())
assertThat sprints.size(), new IsEqual<Integer>(2)
"Assert equals to Sprint 37"(sprints.get(0))
}
}

View File

@ -1,5 +1,7 @@
package net.rcarz.jiraclient.agile
import net.rcarz.jiraclient.Field
/**
* Created by pldupont on 2016-05-19.
*/
@ -31,4 +33,39 @@ interface JSONResources {
}
]
}"""
String SPRINT_37 = """{
"id": 37,
"self": "http://www.example.com/jira/rest/agile/1.0/sprint/23",
"state": "closed",
"name": "sprint 1",
"startDate": "2015-04-11T15:22:00.000+10:00",
"endDate": "2015-04-20T01:22:00.000+10:00",
"completeDate": "2015-04-20T11:04:00.000+10:00",
"originBoardId": 84
}"""
int SPRINT_37_ID = 37
String SPRINT_37_NAME = "sprint 1"
String SPRINT_37_SELF = "http://www.example.com/jira/rest/agile/1.0/sprint/23"
String SPRINT_37_STATE = "closed"
int SPRINT_37_ORIGIN_BOARD_ID = 84
Date SPRINT_37_START_DATE = Field.getDateTime("2015-04-11T15:22:00.000+10:00")
Date SPRINT_37_END_DATE = Field.getDateTime("2015-04-20T01:22:00.000+10:00")
Date SPRINT_37_COMPLETE_DATE = Field.getDateTime("2015-04-20T11:04:00.000+10:00")
String LIST_OF_SPRINTS = """{
"maxResults": 2,
"startAt": 1,
"total": 5,
"isLast": false,
"values": [
${SPRINT_37},
{
"id": 72,
"self": "http://www.example.com/jira/rest/agile/1.0/sprint/73",
"state": "future",
"name": "sprint 2"
}
]
}"""
}

View File

@ -0,0 +1,72 @@
package net.rcarz.jiraclient.agile
import net.rcarz.jiraclient.JiraException
import net.rcarz.jiraclient.RestClient
import net.rcarz.jiraclient.RestException
import net.sf.json.JSONSerializer
import org.hamcrest.core.IsEqual
import org.hamcrest.core.IsNot
import org.hamcrest.core.IsNull
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import static org.junit.Assert.assertThat
import static org.mockito.Mockito.when
/**
* Created by pldupont on 2016-05-19.
*/
class SprintTest extends AbstractResourceTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
void "Given a RestClient, when calling getAll(), then receive a list of Sprint."() {
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/" + JSONResources.SPRINT_37_ORIGIN_BOARD_ID + "/sprint"))
.thenReturn(JSONSerializer.toJSON(JSONResources.LIST_OF_SPRINTS))
List<Sprint> sprints = Sprint.getAll(mockRestClient, JSONResources.SPRINT_37_ORIGIN_BOARD_ID);
assertThat sprints, new IsNot<>(new IsNull())
assertThat sprints.size(), new IsEqual<Integer>(2)
"Assert equals to Sprint 37"(sprints.get(0))
}
@Test
void "Given a RestClient, when calling getAll() and use doesn't have access, then throws an 401 error."() {
RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized")
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/" + JSONResources.SPRINT_37_ORIGIN_BOARD_ID + "/sprint"))
.thenThrow(unauthorized)
expectedException.expect(JiraException.class);
expectedException.expectMessage("Failed to retrieve a list of Sprint : /rest/agile/1.0/board/" + JSONResources.SPRINT_37_ORIGIN_BOARD_ID + "/sprint");
List<Sprint> sprints = Sprint.getAll(mockRestClient, JSONResources.SPRINT_37_ORIGIN_BOARD_ID);
}
@Test
void "Given a RestClient, when calling getSprint(84), then receive one Sprint."() {
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "sprint/37"))
.thenReturn(JSONSerializer.toJSON(JSONResources.SPRINT_37))
Sprint sprint = Sprint.get(mockRestClient, 37);
"Assert equals to Sprint 37"(sprint)
}
@Test
void "Given a RestClient, when calling getSprint(666), then throws an 404 error."() {
RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized")
RestClient mockRestClient = "given a REST Client"()
when(mockRestClient.get(AgileResource.RESOURCE_URI + "sprint/666"))
.thenThrow(unauthorized)
expectedException.expect(JiraException.class);
expectedException.expectMessage("Failed to retrieve Sprint : /rest/agile/1.0/sprint/666");
Sprint sprint = Sprint.get(mockRestClient, 666);
}
}