|
| 1 | +package com.crowdin.client.tasks; |
| 2 | + |
| 3 | +import com.crowdin.client.core.model.PatchOperation; |
| 4 | +import com.crowdin.client.core.model.PatchRequest; |
| 5 | +import com.crowdin.client.core.model.ResponseList; |
| 6 | +import com.crowdin.client.core.model.ResponseObject; |
| 7 | +import com.crowdin.client.framework.RequestMock; |
| 8 | +import com.crowdin.client.framework.TestClient; |
| 9 | +import com.crowdin.client.tasks.model.*; |
| 10 | +import lombok.SneakyThrows; |
| 11 | +import org.apache.http.client.methods.HttpDelete; |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.client.methods.HttpPatch; |
| 14 | +import org.apache.http.client.methods.HttpPost; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.TimeZone; |
| 24 | + |
| 25 | +import static java.util.Collections.singletonList; |
| 26 | +import static org.junit.jupiter.api.Assertions.*; |
| 27 | + |
| 28 | +public class TaskCommentsApiTest extends TestClient { |
| 29 | + |
| 30 | + private final Long projectId = 12L; |
| 31 | + private final Long taskId = 203L; |
| 32 | + private final Long taskCommentId = 1233L; |
| 33 | + private final Long userId = 5L; |
| 34 | + |
| 35 | + private TimeZone originalTimeZone; |
| 36 | + |
| 37 | + private static final Instant EXPECTED_DATE = Instant.parse("2025-08-23T09:04:29Z"); |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + originalTimeZone = TimeZone.getDefault(); |
| 42 | + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + void tearDown() { |
| 47 | + TimeZone.setDefault(originalTimeZone); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<RequestMock> getMocks() { |
| 52 | + return Arrays.asList( |
| 53 | + // LIST |
| 54 | + RequestMock.build( |
| 55 | + formUrl_taskComments(projectId, taskId), |
| 56 | + HttpGet.METHOD_NAME, |
| 57 | + "api/tasks/comments/listTaskCommentsResponse.json", |
| 58 | + new HashMap<String, Integer>() {{ |
| 59 | + put("limit", 20); |
| 60 | + put("offset", 10); |
| 61 | + }} |
| 62 | + ), |
| 63 | + |
| 64 | + // ADD |
| 65 | + RequestMock.build( |
| 66 | + formUrl_taskComments(projectId, taskId), |
| 67 | + HttpPost.METHOD_NAME, |
| 68 | + "api/tasks/comments/addTaskCommentRequest.json", |
| 69 | + "api/tasks/comments/taskCommentsResponse_single.json" |
| 70 | + ), |
| 71 | + |
| 72 | + // GET |
| 73 | + RequestMock.build( |
| 74 | + formUrl_taskCommentId(projectId, taskId, taskCommentId), |
| 75 | + HttpGet.METHOD_NAME, |
| 76 | + "api/tasks/comments/taskCommentsResponse_single.json" |
| 77 | + ), |
| 78 | + |
| 79 | + // DELETE |
| 80 | + RequestMock.build( |
| 81 | + formUrl_taskCommentId(projectId, taskId, taskCommentId), |
| 82 | + HttpDelete.METHOD_NAME |
| 83 | + ), |
| 84 | + |
| 85 | + // PATCH |
| 86 | + RequestMock.build( |
| 87 | + formUrl_taskCommentId(projectId, taskId, taskCommentId), |
| 88 | + HttpPatch.METHOD_NAME, |
| 89 | + "api/tasks/comments/editTaskCommentTextRequest.json", |
| 90 | + "api/tasks/comments/taskCommentsResponse_single.json" |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @SneakyThrows |
| 97 | + public void listTaskCommentsTest() { |
| 98 | + ResponseList<TaskComment> response = this.getTasksApi().listTasksComments(projectId, taskId, 20, 10); |
| 99 | + |
| 100 | + assertNotNull(response.getData().get(0).getData()); |
| 101 | + assertEquals(1, response.getData().size()); |
| 102 | + |
| 103 | + TaskComment comment = response.getData().get(0).getData(); |
| 104 | + |
| 105 | + assertTaskComment(comment); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @SneakyThrows |
| 110 | + public void addTaskCommentTest() { |
| 111 | + CreateTaskCommentRequest request = new CreateTaskCommentRequest(); |
| 112 | + request.setText("translate task"); |
| 113 | + request.setTimeSpent(3600L); |
| 114 | + |
| 115 | + ResponseObject<TaskComment> response = this.getTasksApi().addTaskComment(projectId, taskId, request); |
| 116 | + |
| 117 | + assertNotNull(response.getData()); |
| 118 | + |
| 119 | + TaskComment comment = response.getData(); |
| 120 | + |
| 121 | + assertTaskComment(comment); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + @SneakyThrows |
| 126 | + public void getTaskCommentTest() { |
| 127 | + ResponseObject<TaskComment> response = this.getTasksApi().getTaskComment(projectId, taskId, taskCommentId); |
| 128 | + |
| 129 | + assertNotNull(response.getData()); |
| 130 | + |
| 131 | + TaskComment comment = response.getData(); |
| 132 | + |
| 133 | + assertTaskComment(comment); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + @SneakyThrows |
| 138 | + public void editTaskCommentTest() { |
| 139 | + PatchRequest request = new PatchRequest(); |
| 140 | + request.setOp(PatchOperation.REPLACE); |
| 141 | + request.setValue("translate task"); |
| 142 | + request.setPath("/text"); |
| 143 | + ResponseObject<TaskComment> response = this.getTasksApi().editTaskComment(projectId, taskId, taskCommentId, singletonList(request)); |
| 144 | + |
| 145 | + assertNotNull(response.getData()); |
| 146 | + |
| 147 | + TaskComment comment = response.getData(); |
| 148 | + |
| 149 | + assertTaskComment(comment); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @SneakyThrows |
| 154 | + public void deleteTaskCommentTest() { |
| 155 | + assertDoesNotThrow(() -> |
| 156 | + this.getTasksApi().deleteTaskComment(projectId, taskId, taskCommentId) |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + @SneakyThrows |
| 161 | + private void assertTaskComment(TaskComment comment) { |
| 162 | + assertEquals(taskCommentId, comment.getId()); |
| 163 | + assertEquals(taskId, comment.getTaskId()); |
| 164 | + assertEquals(userId, comment.getUserId()); |
| 165 | + assertEquals("translate task", comment.getText()); |
| 166 | + assertEquals(3600L, comment.getTimeSpent()); |
| 167 | + |
| 168 | + assertNotNull(comment.getCreatedAt()); |
| 169 | + assertNotNull(comment.getUpdatedAt()); |
| 170 | + assertEquals(EXPECTED_DATE, comment.getCreatedAt().toInstant()); |
| 171 | + assertEquals(EXPECTED_DATE, comment.getUpdatedAt().toInstant()); |
| 172 | + } |
| 173 | + |
| 174 | + //<editor-fold desc="Form Url methods for mocks"> |
| 175 | + private String formUrl_taskComments(Long projectId, Long taskId) { |
| 176 | + return this.url + "/projects/" + projectId + "/tasks/" + taskId + "/comments"; |
| 177 | + } |
| 178 | + |
| 179 | + private String formUrl_taskCommentId(Long projectId, Long taskId, Long taskCommentId) { |
| 180 | + return this.url + "/projects/" + projectId + "/tasks/" + taskId + "/comments/" + taskCommentId; |
| 181 | + } |
| 182 | + //</editor-fold> |
| 183 | +} |
0 commit comments