-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/net/tirasa/connid/bundles/kafka/serialization/ConnectorObjectDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (C) 2024 ConnId (connid-dev@googlegroups.com) | ||
* | ||
* 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 | ||
* | ||
* http://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 net.tirasa.connid.bundles.kafka.serialization; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.identityconnectors.framework.common.objects.ConnectorObject; | ||
import org.identityconnectors.framework.common.objects.ConnectorObjectBuilder; | ||
import org.identityconnectors.framework.common.objects.ObjectClass; | ||
|
||
public class ConnectorObjectDeserializer extends JsonDeserializer<ConnectorObject> { | ||
|
||
private static final AttributeDeserializer ATTR_DESERIALIZER = new AttributeDeserializer(); | ||
|
||
@Override | ||
public ConnectorObject deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { | ||
ObjectNode tree = jp.readValueAsTree(); | ||
|
||
ConnectorObjectBuilder builder = new ConnectorObjectBuilder(); | ||
|
||
if (tree.has("objectClass")) { | ||
builder.setObjectClass(new ObjectClass(tree.get("objectClass").get("type").asText())); | ||
} | ||
|
||
if (tree.has("attributeMap")) { | ||
JsonParser parser = tree.get("attributeMap").traverse(); | ||
parser.setCodec(jp.getCodec()); | ||
Map<String, ObjectNode> attributeMap = parser.readValueAs(new TypeReference<Map<String, ObjectNode>>() { | ||
}); | ||
|
||
for (ObjectNode attribute : attributeMap.values()) { | ||
JsonParser p = attribute.traverse(); | ||
p.setCodec(jp.getCodec()); | ||
builder.addAttribute(ATTR_DESERIALIZER.deserialize(p, ctx)); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...main/java/net/tirasa/connid/bundles/kafka/serialization/SyncDeltaJacksonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (C) 2024 ConnId (connid-dev@googlegroups.com) | ||
* | ||
* 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 | ||
* | ||
* http://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 net.tirasa.connid.bundles.kafka.serialization; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import java.io.IOException; | ||
import org.identityconnectors.framework.common.objects.ObjectClass; | ||
import org.identityconnectors.framework.common.objects.SyncDelta; | ||
import org.identityconnectors.framework.common.objects.SyncDeltaBuilder; | ||
import org.identityconnectors.framework.common.objects.SyncDeltaType; | ||
import org.identityconnectors.framework.common.objects.Uid; | ||
|
||
public class SyncDeltaJacksonDeserializer extends JsonDeserializer<SyncDelta> { | ||
|
||
private static final AttributeDeserializer ATTR_DESERIALIZER = new AttributeDeserializer(); | ||
|
||
private static final SyncTokenDeserializer SYNC_TOKEN_DESERIALIZER = new SyncTokenDeserializer(); | ||
|
||
private static final ConnectorObjectDeserializer CONNECTOR_OBJECT_DESERIALIZER = new ConnectorObjectDeserializer(); | ||
|
||
@Override | ||
public SyncDelta deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { | ||
ObjectNode tree = jp.readValueAsTree(); | ||
|
||
SyncDeltaBuilder builder = new SyncDeltaBuilder(); | ||
|
||
if (tree.has("objectClass")) { | ||
builder.setObjectClass(new ObjectClass(tree.get("objectClass").get("type").asText())); | ||
} | ||
|
||
if (tree.has("uid")) { | ||
JsonParser parser = tree.get("uid").traverse(); | ||
parser.setCodec(jp.getCodec()); | ||
builder.setUid((Uid) ATTR_DESERIALIZER.deserialize(parser, ctx)); | ||
} | ||
|
||
if (tree.has("previousUid") && !tree.get("previousUid").isNull()) { | ||
JsonParser parser = tree.get("previousUid").traverse(); | ||
parser.setCodec(jp.getCodec()); | ||
builder.setPreviousUid((Uid) ATTR_DESERIALIZER.deserialize(parser, ctx)); | ||
} | ||
|
||
if (tree.has("token")) { | ||
JsonParser parser = tree.get("token").traverse(); | ||
parser.setCodec(jp.getCodec()); | ||
builder.setToken(SYNC_TOKEN_DESERIALIZER.deserialize(parser, ctx)); | ||
} | ||
|
||
if (tree.has("deltaType")) { | ||
builder.setDeltaType(SyncDeltaType.valueOf(tree.get("deltaType").asText())); | ||
} | ||
|
||
if (tree.has("object")) { | ||
JsonParser parser = tree.get("object").traverse(); | ||
parser.setCodec(jp.getCodec()); | ||
builder.setObject(CONNECTOR_OBJECT_DESERIALIZER.deserialize(parser, ctx)); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,5 +53,4 @@ public void serialize(final SyncToken source, final JsonGenerator jgen, final Se | |
|
||
jgen.writeEndObject(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/net/tirasa/connid/bundles/kafka/serialization/SyncDeltaSerDesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (C) 2024 ConnId (connid-dev@googlegroups.com) | ||
* | ||
* 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 | ||
* | ||
* http://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 net.tirasa.connid.bundles.kafka.serialization; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.identityconnectors.framework.common.objects.AttributeBuilder; | ||
import org.identityconnectors.framework.common.objects.ConnectorObjectBuilder; | ||
import org.identityconnectors.framework.common.objects.Name; | ||
import org.identityconnectors.framework.common.objects.ObjectClass; | ||
import org.identityconnectors.framework.common.objects.SyncDelta; | ||
import org.identityconnectors.framework.common.objects.SyncDeltaBuilder; | ||
import org.identityconnectors.framework.common.objects.SyncDeltaType; | ||
import org.identityconnectors.framework.common.objects.SyncToken; | ||
import org.identityconnectors.framework.common.objects.Uid; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SyncDeltaSerDesTests { | ||
|
||
@Test | ||
void serdes() { | ||
Uid uid = new Uid(UUID.randomUUID().toString()); | ||
SyncDelta syncDelta = new SyncDeltaBuilder(). | ||
setDeltaType(SyncDeltaType.CREATE). | ||
setObjectClass(ObjectClass.ACCOUNT). | ||
setUid(uid). | ||
setObject(new ConnectorObjectBuilder().addAttributes(Set.of( | ||
new Name("name"), | ||
AttributeBuilder.build("email", "connid-dev@googlegroups.com"))). | ||
setUid(uid).build()). | ||
setToken(new SyncToken(System.currentTimeMillis())). | ||
build(); | ||
|
||
byte[] serialized = new SyncDeltaSerializer().serialize("topic", syncDelta); | ||
SyncDelta deserialized = new SyncDeltaDeserializer().deserialize("topic", serialized); | ||
assertEquals(syncDelta, deserialized); | ||
} | ||
} |