[Notice] Announcing the End of Demo Server [Read me]
test: Add test cases for update issue API
@dfe5a657d0a743fa12fa88edc954c2330884cc4f
+++ test/controllers/api/IssueApiUpdateIssueTest.java
... | ... | @@ -0,0 +1,153 @@ |
1 | +package controllers.api; | |
2 | + | |
3 | +import com.fasterxml.jackson.databind.JsonNode; | |
4 | +import com.fasterxml.jackson.databind.node.ObjectNode; | |
5 | +import models.Issue; | |
6 | +import models.Project; | |
7 | +import models.User; | |
8 | +import org.junit.Before; | |
9 | +import org.junit.BeforeClass; | |
10 | +import org.junit.Test; | |
11 | +import org.junit.runner.RunWith; | |
12 | +import org.mockito.Mockito; | |
13 | +import org.powermock.api.mockito.PowerMockito; | |
14 | +import org.powermock.core.classloader.annotations.PrepareForTest; | |
15 | +import org.powermock.modules.junit4.PowerMockRunner; | |
16 | +import play.libs.Json; | |
17 | +import play.mvc.Http; | |
18 | +import play.mvc.Result; | |
19 | +import play.test.FakeApplication; | |
20 | +import play.test.Helpers; | |
21 | + | |
22 | +import static org.fest.assertions.Assertions.assertThat; | |
23 | +import static play.libs.Json.toJson; | |
24 | +import static play.mvc.Http.Status.UNAUTHORIZED; | |
25 | +import static play.mvc.Results.created; | |
26 | +import static play.test.Helpers.*; | |
27 | + | |
28 | +@RunWith(PowerMockRunner.class) | |
29 | +@PrepareForTest({IssueApi.class, UserApi.class, Project.class, Issue.class}) | |
30 | +public class IssueApiUpdateIssueTest { | |
31 | + | |
32 | + protected static FakeApplication app; | |
33 | + | |
34 | + private static final String OWNER = "p.mj"; | |
35 | + private static final String PROJECT_NAME = "dev"; | |
36 | + private static final long NUMBER = 10; | |
37 | + private static final String TITLE = "update title"; | |
38 | + | |
39 | + private static final String TOKEN = "abc9F2AAL+3d1FbSyJqO2bxX6QcFLBLNWTfOI07N00k="; | |
40 | + | |
41 | + @BeforeClass | |
42 | + public static void beforeClass() { | |
43 | + app = support.Helpers.makeTestApplication(); | |
44 | + Helpers.start(app); | |
45 | + } | |
46 | + | |
47 | + @Before | |
48 | + public void before() throws Exception { | |
49 | + PowerMockito.spy(UserApi.class); | |
50 | + PowerMockito.spy(IssueApi.class); | |
51 | + PowerMockito.spy(Project.class); | |
52 | + PowerMockito.spy(Issue.class); | |
53 | + } | |
54 | + | |
55 | + @Test | |
56 | + public void tokenIsNull_returnUnauthorizedError() throws Exception { | |
57 | + // GIVEN | |
58 | + ObjectNode requestBody = Json.newObject(); | |
59 | + requestBody.put("title", TITLE); | |
60 | + | |
61 | + PowerMockito.doReturn(false).when(UserApi.class, "isAuthored", Mockito.any()); | |
62 | + | |
63 | + // WHEN | |
64 | + Result result = callAction( | |
65 | + routes.ref.IssueApi.updateIssue(OWNER, PROJECT_NAME, NUMBER), | |
66 | + fakeRequest().withJsonBody(requestBody) | |
67 | + ); | |
68 | + | |
69 | + // THEN | |
70 | + assertThat(status(result)).isEqualTo(UNAUTHORIZED); | |
71 | + assertThat(contentAsString(result)).isEqualTo("{\"message\":\"unauthorized request\"}"); | |
72 | + } | |
73 | + | |
74 | + @Test | |
75 | + public void tokenIsEmpty_returnUnauthorizedError() throws Exception { | |
76 | + // GIVEN | |
77 | + String headerValue = "token "; | |
78 | + ObjectNode requestBody = Json.newObject(); | |
79 | + requestBody.put("title", TITLE); | |
80 | + | |
81 | + PowerMockito.doReturn(false).when(UserApi.class, "isAuthored", Mockito.any()); | |
82 | + | |
83 | + // WHEN | |
84 | + Result result = callAction( | |
85 | + routes.ref.IssueApi.updateIssue(OWNER, PROJECT_NAME, NUMBER), | |
86 | + fakeRequest().withHeader(Http.HeaderNames.AUTHORIZATION, headerValue).withJsonBody(requestBody) | |
87 | + ); | |
88 | + | |
89 | + // THEN | |
90 | + assertThat(status(result)).isEqualTo(UNAUTHORIZED); | |
91 | + assertThat(contentAsString(result)).isEqualTo("{\"message\":\"unauthorized request\"}"); | |
92 | + } | |
93 | + | |
94 | + @Test | |
95 | + public void requestBodyIsNull_returnBadRequestError() throws Exception { | |
96 | + // GIVEN | |
97 | + String headerValue = "token " + TOKEN; | |
98 | + | |
99 | + PowerMockito.doReturn(true).when(UserApi.class, "isAuthored", Mockito.any()); | |
100 | + | |
101 | + // WHEN | |
102 | + Result result = callAction( | |
103 | + routes.ref.IssueApi.updateIssue(OWNER, PROJECT_NAME, NUMBER), | |
104 | + fakeRequest().withHeader(Http.HeaderNames.AUTHORIZATION, headerValue) | |
105 | + ); | |
106 | + | |
107 | + // THEN | |
108 | + assertThat(status(result)).isEqualTo(BAD_REQUEST); | |
109 | + assertThat(contentAsString(result)).isEqualTo("{\"message\":\"Expecting Json data\"}"); | |
110 | + } | |
111 | + | |
112 | + @Test | |
113 | + public void updatingIssueISSucceeded_returnIssue() throws Exception { | |
114 | + // GIVEN | |
115 | + String headerValue = "token " + TOKEN; | |
116 | + ObjectNode requestBody = Json.newObject(); | |
117 | + requestBody.put("title", TITLE); | |
118 | + | |
119 | + User user = new User(); | |
120 | + Project project = new Project(); | |
121 | + Issue issue = new Issue(); | |
122 | + | |
123 | + JsonNode expected = getResultJsonNode(); | |
124 | + | |
125 | + PowerMockito.doReturn(true).when(UserApi.class, "isAuthored", Mockito.any()); | |
126 | + PowerMockito.doReturn(user).when(UserApi.class, "getAuthorizedUser", Mockito.anyString()); | |
127 | + PowerMockito.doReturn(project).when(Project.class, "findByOwnerAndProjectName", Mockito.anyString(), Mockito.anyString()); | |
128 | + PowerMockito.doReturn(issue).when(Issue.class, "findByNumber", Mockito.any(), Mockito.anyLong()); | |
129 | + PowerMockito.doReturn(created(expected)).when(IssueApi.class, "updateIssueNode", Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); | |
130 | + | |
131 | + // WHEN | |
132 | + Result result = callAction( | |
133 | + routes.ref.IssueApi.updateIssue(OWNER, PROJECT_NAME, NUMBER), | |
134 | + fakeRequest().withHeader(Http.HeaderNames.AUTHORIZATION, headerValue).withJsonBody(requestBody) | |
135 | + ); | |
136 | + | |
137 | + // THEN | |
138 | + assertThat(status(result)).isEqualTo(CREATED); | |
139 | + assertThat(contentAsString(result)).contains("result"); | |
140 | + } | |
141 | + | |
142 | + private JsonNode getResultJsonNode() { | |
143 | + ObjectNode result = Json.newObject(); | |
144 | + ObjectNode issueNode = Json.newObject(); | |
145 | + | |
146 | + issueNode.put("number", 1); | |
147 | + issueNode.put("id",2); | |
148 | + issueNode.put("title", TITLE); | |
149 | + | |
150 | + | |
151 | + return result.set("result", toJson(issueNode)); | |
152 | + } | |
153 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?