Skip to content

Commit

Permalink
Merge pull request #816 from FabienVSymphony/BYC-1642
Browse files Browse the repository at this point in the history
BYC-1642 Add support for list of ApiClientBodyPart
  • Loading branch information
sbenmoussati authored Feb 4, 2025
2 parents f0948e2 + 42b172e commit 34c68de
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.symphony.bdk.http.api.util;

import com.symphony.bdk.http.api.ApiClientBodyPart;

import org.apiguardian.api.API;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -55,12 +57,22 @@ public static void addDefaultRootCaCertificates(KeyStore trustStore) throws Gene
}

public static boolean isCollectionOfFiles(Object paramValue) {
return isCollectionOf(paramValue, File.class);
}

public static boolean isCollectionOfApiClientBodyPart(Object paramValue) {
return isCollectionOf(paramValue, ApiClientBodyPart.class);
}


private static boolean isCollectionOf(Object paramValue, Class clazz) {

if (!(paramValue instanceof Collection<?>)) {
return false;
}

final Collection<?> collection = (Collection<?>) paramValue;
return !collection.isEmpty() && collection.stream().allMatch(p -> p instanceof File);
return !collection.isEmpty() && collection.stream().allMatch(clazz::isInstance);
}

private static String getBdkVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.symphony.bdk.http.api.util.ApiUtils.isCollectionOfApiClientBodyPart;
import static com.symphony.bdk.http.api.util.ApiUtils.isCollectionOfFiles;

/**
Expand Down Expand Up @@ -241,8 +242,12 @@ private void serializeMultiPartDataEntry(String paramKey, Object paramValue,
}
} else if (paramValue instanceof ApiClientBodyPart) {
serializeApiClientBodyPart(paramKey, (ApiClientBodyPart) paramValue, formValueMap);
} else if (isCollectionOfApiClientBodyPart(paramValue)) {
for (Object o : (Collection<?>) paramValue) {
serializeApiClientBodyPart(paramKey, (ApiClientBodyPart) o, formValueMap);
}
} else {
formValueMap.add(paramKey, parameterToString(paramValue));
formValueMap.add(paramKey, parameterToString(paramValue));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.ArgumentMatchers.anyString;

import com.symphony.bdk.http.api.ApiClient;
import com.symphony.bdk.http.api.ApiClientBodyPart;
import com.symphony.bdk.http.api.ApiException;
import com.symphony.bdk.http.api.ApiResponse;
import com.symphony.bdk.http.api.Pair;
Expand All @@ -24,8 +25,10 @@
import org.mockserver.model.ParameterBody;
import org.springframework.http.MediaType;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -286,6 +289,34 @@ void testInvokeApiWithFormValueTest(final BdkMockServer mockServer, @TempDir Pat
assertEquals("success", response.getData().getMessage());
}

@Test
void testInvokeApiWithApiClientBodyPart(final BdkMockServer mockServer) throws ApiException {
mockServer.onRequestModifierWithResponse(200,
httpRequest -> httpRequest
.withMethod("POST")
.withPath("/test-api")
.withHeader(Header.header("sessionToken", "test-token"))
.withBody(anyString())
.withCookie("cookie", "test-cookie"),
httpResponse -> httpResponse
.withBody("{\"code\": 200, \"message\": \"success\"}"));
Map<String, Object> formParams = new HashMap<>();

formParams.put("attachment", Collections.singletonList(new ApiClientBodyPart(new ByteArrayInputStream("test".getBytes()), "filename")));


ApiResponse<Response> response =
this.apiClient.invokeAPI("/test-api", "POST", null, null,
Collections.singletonMap("sessionToken", "test-token"),
Collections.singletonMap("cookie", "test-cookie"), formParams, null, MediaType.MULTIPART_FORM_DATA_VALUE,
new String[] {},
new TypeReference<Response>() {});


assertEquals(200, response.getData().getCode());
assertEquals("success", response.getData().getMessage());
}

@Test
void shouldClearTraceIdIfNotSet(final BdkMockServer mockServer) throws ApiException {
mockServer.onRequestModifierWithResponse(200,
Expand Down

0 comments on commit 34c68de

Please sign in to comment.