diff --git a/src/main/java/com/crowdin/client/sourcestrings/SourceStringsApi.java b/src/main/java/com/crowdin/client/sourcestrings/SourceStringsApi.java index e21857566..21d850931 100644 --- a/src/main/java/com/crowdin/client/sourcestrings/SourceStringsApi.java +++ b/src/main/java/com/crowdin/client/sourcestrings/SourceStringsApi.java @@ -5,6 +5,7 @@ import com.crowdin.client.core.http.exceptions.HttpBadRequestException; import com.crowdin.client.core.http.exceptions.HttpException; import com.crowdin.client.core.model.*; +import com.crowdin.client.sourcefiles.model.UpdateOption; import com.crowdin.client.sourcestrings.model.*; import java.util.List; @@ -165,22 +166,57 @@ public void deleteSourceString(Long projectId, Long stringId) throws HttpExcepti * */ public ResponseObject editSourceString(Long projectId, Long stringId, List request) throws HttpException, HttpBadRequestException { - SourceStringResponseObject sourceStringResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/strings/" + stringId, request, new HttpRequestConfig(), SourceStringResponseObject.class); - return ResponseObject.of(sourceStringResponseObject.getData()); + return editSourceString(projectId, stringId, request, null); } /** * @param projectId project identifier + * @param stringId string identifier * @param request request object + * @param updateOption defines whether existing translations and approvals are kept when the string is updated (applied only when {@code text} or {@code identifier} is changed) * @return updated source string * @see + */ + public ResponseObject editSourceString(Long projectId, Long stringId, List request, UpdateOption updateOption) throws HttpException, HttpBadRequestException { + HttpRequestConfig config = new HttpRequestConfig(HttpRequestConfig.buildUrlParams( + "updateOption", Optional.ofNullable(updateOption) + )); + SourceStringResponseObject sourceStringResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/strings/" + stringId, request, config, SourceStringResponseObject.class); + return ResponseObject.of(sourceStringResponseObject.getData()); + } + + /** + * @param projectId project identifier + * @param request request object + * @return list of updated source strings + * @see */ public ResponseList stringBatchOperations(Long projectId, List request) throws HttpException, HttpBadRequestException { + return stringBatchOperations(projectId, request, null); + } + + /** + * @param projectId project identifier + * @param request request object + * @param updateOption defines whether existing translations and approvals are kept when a string is updated (applied only when {@code text} or {@code identifier} is changed) + * @return list of updated source strings + * @see + */ + public ResponseList stringBatchOperations(Long projectId, List request, UpdateOption updateOption) throws HttpException, HttpBadRequestException { + HttpRequestConfig config = new HttpRequestConfig(HttpRequestConfig.buildUrlParams( + "updateOption", Optional.ofNullable(updateOption) + )); String url = this.url + "/projects/" + projectId + "/strings"; - SourceStringResponseList sourceStringResponseList = this.httpClient.patch(url, request, new HttpRequestConfig(), SourceStringResponseList.class); + SourceStringResponseList sourceStringResponseList = this.httpClient.patch(url, request, config, SourceStringResponseList.class); return SourceStringResponseList.to(sourceStringResponseList); } } diff --git a/src/test/java/com/crowdin/client/sourcestrings/SourceStringsApiTest.java b/src/test/java/com/crowdin/client/sourcestrings/SourceStringsApiTest.java index f134cb8bb..7677b12dc 100644 --- a/src/test/java/com/crowdin/client/sourcestrings/SourceStringsApiTest.java +++ b/src/test/java/com/crowdin/client/sourcestrings/SourceStringsApiTest.java @@ -14,7 +14,9 @@ import java.util.*; +import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -53,7 +55,9 @@ public List getMocks() { RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpGet.METHOD_NAME, "api/strings/string.json"), RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpDelete.METHOD_NAME), RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpPatch.METHOD_NAME, "api/strings/editString.json", "api/strings/string.json"), - RequestMock.build(this.url + "/projects/" + projectId + "/strings", HttpPatch.METHOD_NAME, "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json") + RequestMock.build(this.url + "/projects/" + projectId + "/strings", HttpPatch.METHOD_NAME, "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json"), + new RequestMock(this.url + "/projects/" + project2Id + "/strings/" + id, "api/strings/editString.json", "api/strings/string.json", HttpPatch.METHOD_NAME, singletonMap("updateOption", UpdateOption.KEEP_TRANSLATIONS_AND_APPROVALS), emptyMap()), + new RequestMock(this.url + "/projects/" + project2Id + "/strings", "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json", HttpPatch.METHOD_NAME, singletonMap("updateOption", UpdateOption.CLEAR_TRANSLATIONS_AND_APPROVALS), emptyMap()) ); } @@ -325,4 +329,54 @@ public void stringBatchOperationsTest() { assertEquals(48, item.getFileId()); assertEquals(667, item.getBranchId()); } + + @Test + public void editStringWithUpdateOptionTest() { + PatchRequest request = new PatchRequest(); + request.setOp(PatchOperation.REPLACE); + request.setValue(text); + request.setPath("/text"); + ResponseObject sourceStringResponseObject = this.getSourceStringsApi() + .editSourceString(project2Id, id, singletonList(request), UpdateOption.KEEP_TRANSLATIONS_AND_APPROVALS); + assertEquals(sourceStringResponseObject.getData().getId(), id); + assertEquals(sourceStringResponseObject.getData().getText(), text); + } + + @Test + public void stringBatchOperationsWithUpdateOptionTest() { + List request = new ArrayList() {{ + add(new PatchRequest() {{ + setOp(PatchOperation.REPLACE); + setPath("/2814/isHidden"); + setValue(true); + }}); + add(new PatchRequest() {{ + setOp(PatchOperation.REPLACE); + setPath("/2814/context"); + setValue("some context"); + }}); + add(new PatchRequest() {{ + setOp(PatchOperation.ADD); + setPath("/-"); + setValue(new SourceStringForm() {{ + setText("new added string"); + setIdentifier("a.b.c"); + setContext("context for new string"); + setFileId(5L); + setIsHidden(false); + }}); + }}); + add(new PatchRequest() {{ + setOp(PatchOperation.REMOVE); + setPath("/2815"); + }}); + }}; + + ResponseList response = this.getSourceStringsApi() + .stringBatchOperations(project2Id, request, UpdateOption.CLEAR_TRANSLATIONS_AND_APPROVALS); + + SourceString item = response.getData().get(0).getData(); + assertNotNull(item); + assertEquals(2814, item.getId()); + } }