From 3172b5646aa61e7acfd83d573d5d52add4c816e9 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Mon, 21 Aug 2023 14:00:39 +0900 Subject: [PATCH 1/2] Pass filename to the server. This is just a *hack*. but it works. https://github.com/square/retrofit/issues/1140 --- .../ManageAudienceBlobClientExTest.java | 99 +++++++++++++++++++ .../libraries/retrofit2/formParams.mustache | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java diff --git a/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java b/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java new file mode 100644 index 000000000..4147c161f --- /dev/null +++ b/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java @@ -0,0 +1,99 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.linecorp.bot.audience.client; + +import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.containing; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.put; +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; + +import java.net.URI; +import java.util.Arrays; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.bridge.SLF4JBridgeHandler; + +import com.github.tomakehurst.wiremock.WireMockServer; + +import com.linecorp.bot.audience.model.AddAudienceToAudienceGroupRequest; +import com.linecorp.bot.audience.model.Audience; +import com.linecorp.bot.client.base.UploadFile; + +@ExtendWith(MockitoExtension.class) +@Timeout(5) +public class ManageAudienceBlobClientExTest { + static { + SLF4JBridgeHandler.removeHandlersForRootLogger(); + SLF4JBridgeHandler.install(); + } + + private WireMockServer wireMockServer; + private ManageAudienceBlobClient target; + + @BeforeEach + public void setUp() { + wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + configureFor("localhost", wireMockServer.port()); + + target = ManageAudienceBlobClient.builder("MY_OWN_TOKEN") + .apiEndPoint(URI.create(wireMockServer.baseUrl())) + .build(); + } + + @AfterEach + public void tearDown() { + wireMockServer.stop(); + } + + @Test + public void testAddUserIdsToAudience() { + stubFor(put(urlEqualTo("/v2/bot/audienceGroup/upload/byFile")) + .withMultipartRequestBody(aMultipart() + .withName("file") + .withBody(equalTo("foobar")) + .withHeader("Content-Disposition", containing("filename=\"file\"")) + ).willReturn( + aResponse() + .withStatus(200) + .withHeader("content-type", "application/json") + .withBody("{}"))); + + // Do + target.addUserIdsToAudience(4649L, "Hello", UploadFile.fromString("foobar", "text/plain")) + .join(); + + // Verify + verify( + putRequestedFor( + urlEqualTo("/v2/bot/audienceGroup/upload/byFile") + ).withHeader("Authorization", equalTo("Bearer MY_OWN_TOKEN")) + ); + } +} diff --git a/templates/libraries/retrofit2/formParams.mustache b/templates/libraries/retrofit2/formParams.mustache index 6f6100353..081ace972 100644 --- a/templates/libraries/retrofit2/formParams.mustache +++ b/templates/libraries/retrofit2/formParams.mustache @@ -1,4 +1,4 @@ {{#isFormParam}} {{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isFile}} - {{#isFile}}{{#isMultipart}}@Part("{{baseName}}"){{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} UploadFile {{paramName}}{{/isFile}} + {{#isFile}}{{#isMultipart}}@Part("{{baseName}}\"; filename=\"{{baseName}}"){{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} UploadFile {{paramName}}{{/isFile}} {{/isFormParam}} From fd513f51155ad9bf02265f32c5390df05281a1a2 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Mon, 21 Aug 2023 15:36:23 +0900 Subject: [PATCH 2/2] Organize imports --- .../bot/audience/client/ManageAudienceBlobClientExTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java b/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java index 4147c161f..f3d8f6d0a 100644 --- a/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java +++ b/clients/line-bot-manage-audience-client/src/test/java/com/linecorp/bot/audience/client/ManageAudienceBlobClientExTest.java @@ -29,7 +29,6 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import java.net.URI; -import java.util.Arrays; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -41,8 +40,6 @@ import com.github.tomakehurst.wiremock.WireMockServer; -import com.linecorp.bot.audience.model.AddAudienceToAudienceGroupRequest; -import com.linecorp.bot.audience.model.Audience; import com.linecorp.bot.client.base.UploadFile; @ExtendWith(MockitoExtension.class)