Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,7 +101,8 @@ public static Map<String, String> getRequestMetadata(Map<String, Object> metadat
private static Document convertMapToDocument(Map<String, Object> value) {
Map<String, Document> attr = value.entrySet()
.stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> convertObjectToDocument(e.getValue())));
.collect(Collectors.toMap(Map.Entry::getKey, e -> convertObjectToDocument(e.getValue()), (a, b) -> b,
LinkedHashMap::new));

return Document.fromMap(attr);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2023-present the original author or authors.
*
* Licensed 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
*
* https://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 org.springframework.ai.bedrock.converse.api;

import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.document.Document;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ConverseApiUtils}.
*
* @author yqz
*/
class ConverseApiUtilsTests {

@Test
void convertMapPreservesKeyOrder() {
Map<String, Object> source = new LinkedHashMap<>();
source.put("evidenceText", "text");
source.put("justification", "reason");
source.put("valid", true);

Document document = ConverseApiUtils.convertObjectToDocument(source);

assertThat(document.asMap().keySet()).containsExactly("evidenceText", "justification", "valid");
}

@Test
void convertNestedMapPreservesKeyOrder() {
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("zebra", "z");
properties.put("apple", "a");
properties.put("mango", "m");

Map<String, Object> schema = new LinkedHashMap<>();
schema.put("type", "object");
schema.put("properties", properties);

Document document = ConverseApiUtils.convertObjectToDocument(schema);

assertThat(document.asMap().keySet()).containsExactly("type", "properties");
assertThat(document.asMap().get("properties").asMap().keySet()).containsExactly("zebra", "apple", "mango");
}

}