Source

Chello / Chello.Tests / TestTelloService.cs

Full commit
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using Chello.Core;

namespace Chello.Tests
{
	[ServiceContract]
	[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] // This means one instance per test (since TestInitialize/Cleanup recreates for each test), to allow state changes.
	public class TestTrelloService
	{
		Dictionary<string, TestTrelloServiceData> data = new Dictionary<string, TestTrelloServiceData>();

		public TestTrelloService()
		{
			// TODO: Make this better. For now, we just want to get the existing tests working against the test service.
			var basicTestData = new TestTrelloServiceData("BASIC");
			basicTestData.Add(new Board { Id = "TESTBOARD1", Name = "Test Board" });
			basicTestData.Add(new List { Id = "TESTLIST1", IdBoard = "TESTBOARD1", Name = "Test List" });
			basicTestData.Add(new Card { Id = "TESTCARD1", IdBoard = "TESTBOARD1", IdList = "TESTLIST1", Name = "Test Card Name" });
			basicTestData.Add(new Organization { Id = "TESTORG1", Name = "Test Org Name", DisplayName = "Test Org DisplayName", Desc = "We Can Remember It for You Wholesale" });
			basicTestData.Add(new Member { Id = "TESTMEMB1", Username = "TESTMEMB1", FullName = "Test Member", Bio = "Test", Gravatar = "gra", Url = "http://test" });
			basicTestData.AddRelationship<Member, Organization>("TESTMEMB1", "TESTORG1");

			data.Add(basicTestData.AuthKey, basicTestData);
		}

		#region Boards

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "boards/{boardId}?key={key}")]
		public Board Board(string key, string boardId)
		{
			return data[key].Boards.Single(b => b.Id == boardId);
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "organizations/{organizationName}/boards?key={key}")]
		public List<Board> BoardsForOrganization(string key, string organizationName)
		{
			return data[key].Boards.ToList(); // TODO: Filter
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "members/{username}/boards?key={key}")]
		public List<Board> BoardsForUser(string key, string username)
		{
			return data[key].Boards.ToList(); // TODO: Filter
		}

		#endregion

		#region Lists

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "lists/{listId}?key={key}")]
		public List List(string key, string listId)
		{
			return data[key].Lists.Single(l => l.Id == listId);
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "boards/{boardId}/lists?key={key}")]
		public List<List> ListsForBoard(string key, string boardId)
		{
			return data[key].Lists.Where(l => l.IdBoard == boardId).ToList();
		}

		#endregion

		#region Cards

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "cards/{cardId}?key={key}")]
		public Card Card(string key, string cardId)
		{
			return data[key].Cards.Single(c => c.Id == cardId);
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "boards/{boardId}/cards?key={key}")]
		public List<Card> CardsForBoard(string key, string boardId)
		{
			return data[key].Cards.Where(c => c.IdBoard == boardId).ToList();
		}

		[OperationContract]
		[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "cards/{cardId}?key={key}")]
		public void UpdateCard(string key, string cardId, Card updatedCard)
		{
			// Make sure we store the card with the ID we were passed.
			updatedCard.Id = cardId;
			data[key].Update(updatedCard);
		}

		[OperationContract]
		[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "cards/{cardId}/members?key={key}")]
		public void CardAddMember(string key, string cardId, AddMemberData memberId)
		{
			data[key].AddRelationship<Card, Member>(cardId, memberId.Value);
		}

		[DataContract]
		public class AddMemberData
		{
			[DataMember(Name = "value")]
			public string Value { get; set; }
		}

		[OperationContract]
		[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "cards/{cardId}/members/{memberId}?key={key}")]
		public void CardRemoveMember(string key, string cardId, string memberId)
		{
			data[key].RemoveRelationship<Card, Member>(cardId, memberId);
		}

		#endregion

		#region Organizations

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "organizations/{organizationId}?key={key}")]
		public Organization Organization(string key, string organizationId)
		{
			// TODO: Make this work on org name as well as ID.
			return data[key].Get<Organization>(organizationId);
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "members/{memberId}/organizations?key={key}")]
		public List<Organization> OrganizationsForMember(string key, string memberId)
		{
			// TODO: Make this work with username as well as ID.
			return data[key].GetRelated<Member, Organization>(memberId).ToList();
		}

		#endregion

		#region Members

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "members/{memberId}?key={key}")]
		public Member Member(string key, string memberId)
		{
			// TODO: Support username *and* user ID.
			return data[key].Get<Member>(memberId);
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "organizations/{organizationId}/members?key={key}")]
		public List<Member> MembersForOrganization(string key, string organizationId)
		{
			return data[key].GetRelated<Organization, Member>(organizationId).ToList();
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "boards/{boardId}/members?key={key}")]
		public List<Member> MembersForBoard(string key, string boardId)
		{
			return data[key].GetRelated<Board, Member>(boardId).ToList();
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "cards/{cardId}/members?key={key}")]
		public List<Member> MembersForCard(string key, string cardId)
		{
			return data[key].GetRelated<Card, Member>(cardId).ToList();
		}

		#endregion
	}
}