Add document Id generator
- Clean uneccessary classes/interfaces - straighten code style. - Clean tests. Resolves #113
This commit is contained in:
committed by
Christian Tzolov
parent
2704d53909
commit
b0d8d2b9ce
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2024-2024 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.document.id;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IdGeneratorProviderTest {
|
||||
|
||||
@Test
|
||||
void hashGeneratorGenerateSimilarIdsForSimilarContent() {
|
||||
|
||||
var idGenerator1 = new JdkSha256HexIdGenerator();
|
||||
var idGenerator2 = new JdkSha256HexIdGenerator();
|
||||
|
||||
final String content = "Content";
|
||||
final Map<String, Object> metadata = Map.of("metadata", Set.of("META_DATA"));
|
||||
|
||||
String actualHashes1 = idGenerator1.generateId(content, metadata);
|
||||
String actualHashes2 = idGenerator2.generateId(content, metadata);
|
||||
|
||||
Assertions.assertEquals(actualHashes1, actualHashes2);
|
||||
|
||||
// Assert (other expected behaviors)
|
||||
Assertions.assertDoesNotThrow(() -> UUID.fromString(actualHashes1));
|
||||
Assertions.assertDoesNotThrow(() -> UUID.fromString(actualHashes2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hashGeneratorGenerateDifferentIdsForDifferentContent() {
|
||||
|
||||
var idGenerator1 = new JdkSha256HexIdGenerator();
|
||||
var idGenerator2 = new JdkSha256HexIdGenerator();
|
||||
|
||||
final String content1 = "Content";
|
||||
final Map<String, Object> metadata1 = Map.of("metadata", Set.of("META_DATA"));
|
||||
final String content2 = content1 + " ";
|
||||
final Map<String, Object> metadata2 = metadata1;
|
||||
|
||||
String actualHashes1 = idGenerator1.generateId(content1, metadata1);
|
||||
String actualHashes2 = idGenerator2.generateId(content2, metadata2);
|
||||
|
||||
Assertions.assertNotEquals(actualHashes1, actualHashes2);
|
||||
|
||||
// Assert (other expected behaviors)
|
||||
Assertions.assertDoesNotThrow(() -> UUID.fromString(actualHashes1));
|
||||
Assertions.assertDoesNotThrow(() -> UUID.fromString(actualHashes2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2024-2024 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.document.id;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JdkSha256HexIdGeneratorTest {
|
||||
|
||||
private final JdkSha256HexIdGenerator testee = new JdkSha256HexIdGenerator();
|
||||
|
||||
@Test
|
||||
void messageDigestReturnsDistinctInstances() {
|
||||
final MessageDigest md1 = testee.getMessageDigest();
|
||||
final MessageDigest md2 = testee.getMessageDigest();
|
||||
|
||||
Assertions.assertThat(md1 != md2).isTrue();
|
||||
|
||||
Assertions.assertThat(md1.getAlgorithm()).isEqualTo(md2.getAlgorithm());
|
||||
Assertions.assertThat(md1.getDigestLength()).isEqualTo(md2.getDigestLength());
|
||||
Assertions.assertThat(md1.getProvider()).isEqualTo(md2.getProvider());
|
||||
Assertions.assertThat(md1.toString()).isEqualTo(md2.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageDigestReturnsInstancesWithIndependentAndReproducibleDigests() {
|
||||
final String updateString1 = "md1_update";
|
||||
final String updateString2 = "md2_update";
|
||||
final Charset charset = StandardCharsets.UTF_8;
|
||||
|
||||
final byte[] md1BytesFirstTry = testee.getMessageDigest().digest(updateString1.getBytes(charset));
|
||||
final byte[] md2BytesFirstTry = testee.getMessageDigest().digest(updateString2.getBytes(charset));
|
||||
final byte[] md1BytesSecondTry = testee.getMessageDigest().digest(updateString1.getBytes(charset));
|
||||
final byte[] md2BytesSecondTry = testee.getMessageDigest().digest(updateString2.getBytes(charset));
|
||||
|
||||
Assertions.assertThat(md1BytesFirstTry).isNotEqualTo(md2BytesFirstTry);
|
||||
|
||||
Assertions.assertThat(md1BytesFirstTry).isEqualTo(md1BytesSecondTry);
|
||||
Assertions.assertThat(md2BytesFirstTry).isEqualTo(md2BytesSecondTry);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user