Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rodireich committed May 9, 2024
1 parent bdb083f commit 0776a05
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Collectors;

import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.UuidRepresentation;
import org.bson.internal.UuidHelper;
import org.bson.types.Binary;
Expand All @@ -22,11 +23,11 @@
*/
public enum IdType {

OBJECT_ID("objectId", "ObjectId", ObjectId::new),
STRING("string", "String", s -> s),
INT("int", "Integer", Integer::valueOf),
LONG("long", "Long", Long::valueOf),
BINARY("binData", "Binary", s -> s);
OBJECT_ID("objectId", "ObjectId"),
STRING("string", "String"),
INT("int", "Integer"),
LONG("long", "Long"),
BINARY("binData", "Binary");

private static final Map<String, IdType> byBsonType = new HashMap<>();
static {
Expand Down Expand Up @@ -54,17 +55,10 @@ public enum IdType {
private final String bsonType;
/** Java class name type */
private final String javaType;
/** Converter for converting a string value into an appropriate MongoDb type. */
private final Function<String, Object> converter;

IdType(final String bsonType, final String javaType, final Function<String, Object> converter) {
IdType(final String bsonType, final String javaType) {
this.bsonType = bsonType;
this.javaType = javaType;
this.converter = converter;
}

public Object convert(final String t) {
return converter.apply(t);
}

public static Optional<IdType> findByBsonType(final String bsonType) {
Expand All @@ -86,7 +80,7 @@ public static String idToStringRepresenation(final Object currentId, final IdTyp
final String id;
if (idType == IdType.BINARY) {
final var binLastId = (Binary) currentId;
if (binLastId.getType() == 4) {
if (binLastId.getType() == BsonBinarySubType.UUID_STANDARD.getValue()) {
id = UuidHelper.decodeBinaryToUuid(binLastId.getData(), binLastId.getType(), UuidRepresentation.STANDARD).toString();
} else {
id = getEncoder().encodeToString(binLastId.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ private List<AirbyteStreamState> generateStreamStateList(final Map<AirbyteStream

private AirbyteStreamState generateStreamState(final AirbyteStreamNameNamespacePair airbyteStreamNameNamespacePair,
final MongoDbStreamState streamState) {
LOGGER.info("*** {} to {}", streamState, Jsons.jsonNode(streamState));
return new AirbyteStreamState()
.withStreamDescriptor(
new StreamDescriptor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

package io.airbyte.integrations.source.mongodb.state;

import static io.airbyte.integrations.source.mongodb.state.IdType.BINARY;
import static org.bson.BsonBinarySubType.UUID_STANDARD;
import static org.junit.jupiter.api.Assertions.*;

import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.BsonInt32;
import org.bson.UuidRepresentation;
import org.bson.internal.UuidHelper;
import org.bson.types.Binary;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Test;

import java.util.UUID;

class IdTypeTest {

@Test
void convert() {
assertEquals(101, IdType.INT.convert("101"));
assertEquals(202L, IdType.LONG.convert("202"));
assertEquals("example", IdType.STRING.convert("example"));
assertEquals(new ObjectId("012301230123012301230123"), IdType.OBJECT_ID.convert("012301230123012301230123"));
// assertEquals(UUID.fromString("8f8c1010-4388-4663-9a58-d0b2a113b81a"), IdType.BINARY.convert("8f8c1010-4388-4663-9a58-d0b2a113b81a"));
}

@Test
void findByBsonType() {
assertTrue(IdType.findByBsonType("objectId").isPresent(), "objectId not found");
Expand All @@ -34,6 +33,7 @@ void findByJavaType() {
assertTrue(IdType.findByJavaType("objectId").isPresent(), "objectId not found");
assertTrue(IdType.findByJavaType("objectid").isPresent(), "should have found nothing as it is case-insensitive");
assertTrue(IdType.findByJavaType("Integer").isPresent(), "Integer not found");
assertTrue(IdType.findByJavaType("Binary").isPresent(), "Binary not found");
assertTrue(IdType.findByJavaType(null).isEmpty(), "passing in a null is fine");
}

Expand All @@ -42,4 +42,15 @@ void supported() {
assertEquals("objectId, string, int, long, binData", IdType.SUPPORTED);
}

@Test
void idToStringRepresenation() {
assertEquals("1234", IdType.idToStringRepresenation(1234, IdType.INT));
assertEquals("1234567890", IdType.idToStringRepresenation(1234567890L, IdType.LONG));
assertEquals("abcde", IdType.idToStringRepresenation("abcde", IdType.STRING));
assertEquals("012301230123012301230123", IdType.idToStringRepresenation(new ObjectId("012301230123012301230123"), IdType.OBJECT_ID));
assertEquals("AQIDBA==", IdType.idToStringRepresenation(new Binary(new byte[]{1, 2, 3, 4}), BINARY));
assertEquals("74c14d29-3d25-4e59-a621-73895ee859f9",
IdType.idToStringRepresenation(new Binary(UUID_STANDARD,
new BsonBinary(UUID.fromString("74C14D29-3D25-4E59-A621-73895EE859F9")).getData()), BINARY));
}
}

0 comments on commit 0776a05

Please sign in to comment.