|
| 1 | +/* |
| 2 | + * Copyright 2023 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.bot.audience.client; |
| 18 | + |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.containing; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 26 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 27 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 28 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 29 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 30 | + |
| 31 | +import java.net.URI; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.Timeout; |
| 37 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 38 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 39 | +import org.slf4j.bridge.SLF4JBridgeHandler; |
| 40 | + |
| 41 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 42 | + |
| 43 | +import com.linecorp.bot.client.base.UploadFile; |
| 44 | + |
| 45 | +@ExtendWith(MockitoExtension.class) |
| 46 | +@Timeout(5) |
| 47 | +public class ManageAudienceBlobClientExTest { |
| 48 | + static { |
| 49 | + SLF4JBridgeHandler.removeHandlersForRootLogger(); |
| 50 | + SLF4JBridgeHandler.install(); |
| 51 | + } |
| 52 | + |
| 53 | + private WireMockServer wireMockServer; |
| 54 | + private ManageAudienceBlobClient target; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void setUp() { |
| 58 | + wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); |
| 59 | + wireMockServer.start(); |
| 60 | + configureFor("localhost", wireMockServer.port()); |
| 61 | + |
| 62 | + target = ManageAudienceBlobClient.builder("MY_OWN_TOKEN") |
| 63 | + .apiEndPoint(URI.create(wireMockServer.baseUrl())) |
| 64 | + .build(); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterEach |
| 68 | + public void tearDown() { |
| 69 | + wireMockServer.stop(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testAddUserIdsToAudience() { |
| 74 | + stubFor(put(urlEqualTo("/v2/bot/audienceGroup/upload/byFile")) |
| 75 | + .withMultipartRequestBody(aMultipart() |
| 76 | + .withName("file") |
| 77 | + .withBody(equalTo("foobar")) |
| 78 | + .withHeader("Content-Disposition", containing("filename=\"file\"")) |
| 79 | + ).willReturn( |
| 80 | + aResponse() |
| 81 | + .withStatus(200) |
| 82 | + .withHeader("content-type", "application/json") |
| 83 | + .withBody("{}"))); |
| 84 | + |
| 85 | + // Do |
| 86 | + target.addUserIdsToAudience(4649L, "Hello", UploadFile.fromString("foobar", "text/plain")) |
| 87 | + .join(); |
| 88 | + |
| 89 | + // Verify |
| 90 | + verify( |
| 91 | + putRequestedFor( |
| 92 | + urlEqualTo("/v2/bot/audienceGroup/upload/byFile") |
| 93 | + ).withHeader("Authorization", equalTo("Bearer MY_OWN_TOKEN")) |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments