Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 176 lines (159 sloc) 6.049 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;

namespace ServiceStack.Text.Tests.UseCases
{
[TestFixture]
public class GitHubRestTests
{
private const string JsonGitResponse = @"{
""pulls"": [
{
""state"": ""open"",
""base"": {
""label"": ""technoweenie:master"",
""ref"": ""master"",
""sha"": ""53397635da83a2f4b5e862b5e59cc66f6c39f9c6"",
},
""head"": {
""label"": ""smparkes:synchrony"",
""ref"": ""synchrony"",
""sha"": ""83306eef49667549efebb880096cb539bd436560"",
},
""discussion"": [
{
""type"": ""IssueComment"",
""gravatar_id"": ""821395fe70906c8290df7f18ac4ac6cf"",
""created_at"": ""2010/10/07 07:38:35 -0700"",
""body"": ""Did you intend to remove net/http? Otherwise, this looks good. Have you tried running the LIVE tests with it?\r\n\r\n ruby test/live_server.rb # start the demo server\r\n LIVE=1 rake"",
""updated_at"": ""2010/10/07 07:38:35 -0700"",
""id"": 453980,
},
{
""type"": ""Commit"",
""created_at"": ""2010-11-04T16:27:45-07:00"",
""sha"": ""83306eef49667549efebb880096cb539bd436560"",
""author"": ""Steven Parkes"",
""subject"": ""add em_synchrony support"",
""email"": ""smparkes@smparkes.net""
}
],
""title"": ""Synchrony"",
""body"": ""Here's the pull request.\r\n\r\nThis isn't generic EM: require's Ilya's synchrony and needs to be run on its own fiber, e.g., via synchrony or rack-fiberpool.\r\n\r\nI thought about a \""first class\"" em adapter, but I think the faraday api is sync right now, right? Interesting idea to add something like rack's async support to faraday, but that's an itch I don't have right now."",
""position"": 4.0,
""number"": 15,
""votes"": 0,
""comments"": 4,
""diff_url"": ""https://github.com/technoweenie/faraday/pull/15.diff"",
""patch_url"": ""https://github.com/technoweenie/faraday/pull/15.patch"",
""labels"": [],
""html_url"": ""https://github.com/technoweenie/faraday/pull/15"",
""issue_created_at"": ""2010-10-04T12:39:18-07:00"",
""issue_updated_at"": ""2010-11-04T16:35:04-07:00"",
""created_at"": ""2010-10-04T12:39:18-07:00"",
""updated_at"": ""2010-11-04T16:30:14-07:00""
}
]
}";

public class Discussion
{
public string Type { get; set; }
public string GravatarId { get; set; }
public string CreatedAt { get; set; }
public string Body { get; set; }
public string UpdatedAt { get; set; }

public int? Id { get; set; }
public string Sha { get; set; }
public string Author { get; set; }
public string Subject { get; set; }
public string Email { get; set; }
}

[Test]
public void Can_Parse_Discussion_using_JsonObject()
{
List<Discussion> discussions = JsonObject.Parse(JsonGitResponse)
.ArrayObjects("pulls")[0]
.ArrayObjects("discussion")
.ConvertAll(x => new Discussion
{
Type = x.Get("type"),
GravatarId = x.Get("gravatar_id"),
CreatedAt = x.Get("created_at"),
Body = x.Get("body"),
UpdatedAt = x.Get("updated_at"),

Id = x.JsonTo<int?>("id"),
Sha = x.Get("sha"),
Author = x.Get("author"),
Subject = x.Get("subject"),
Email = x.Get("email"),
});

Console.WriteLine(discussions.Dump()); //See what's been parsed
Assert.That(discussions.ConvertAll(x => x.Type), Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
}

[Test]
public void Can_Parse_Discussion_using_only_NET_collection_classes()
{
var jsonObj = JsonSerializer.DeserializeFromString<List<JsonObject>>(JsonGitResponse);
            var jsonPulls = JsonSerializer.DeserializeFromString<List<JsonObject>>(jsonObj[0].Child("pulls"));
            var discussions = JsonSerializer.DeserializeFromString<List<JsonObject>>(jsonPulls[0].Child("discussion"))
.ConvertAll(x => new Discussion
{
Type = x.Get("type"),
GravatarId = x.Get("gravatar_id"),
CreatedAt = x.Get("created_at"),
Body = x.Get("body"),
UpdatedAt = x.Get("updated_at"),

Id = x.JsonTo<int?>("id"),
Sha = x.Get("sha"),
Author = x.Get("author"),
Subject = x.Get("subject"),
Email = x.Get("email"),
});

Console.WriteLine(discussions.Dump()); //See what's been parsed
Assert.That(discussions.ConvertAll(x => x.Type), Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
}


public class GitHubResponse
{
public List<Pull> pulls { get; set; }
}

public class Pull
{
public List<discussion> discussion { get; set; }
public string title { get; set; }
public string body { get; set; }
public double position { get; set; }
public int number { get; set; }
public int votes { get; set; }
public int comments { get; set; }
public string diff_url { get; set; }
public string patch_url { get; set; }
public string html_url { get; set; }
public DateTime issue_created_date { get; set; }
public DateTime issue_updated_at { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}

public class discussion
{
public string type { get; set; }
public string gravatar_id { get; set; }
public string created_at { get; set; }
public string body { get; set; }
public string updated_at { get; set; }

public int? id { get; set; }
public string sha { get; set; }
public string author { get; set; }
public string subject { get; set; }
public string email { get; set; }
}

[Test]
public void Can_Parse_Discussion_using_custom_client_DTOs()
{
var gitHubResponse = JsonSerializer.DeserializeFromString<GitHubResponse>(JsonGitResponse);

Console.WriteLine(gitHubResponse.Dump()); //See what's been parsed
Assert.That(gitHubResponse.pulls.SelectMany(p => p.discussion).Map(x => x.type),
Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
}

}
}
Something went wrong with that request. Please try again.