Skip to content

Commit

Permalink
[SYNCOPE-1847] added PropagationTaskInfoSerializer to avoid serializa…
Browse files Browse the repository at this point in the history
…tion errors, especially on audit (#929)
  • Loading branch information
andrea-patricelli committed Dec 5, 2024
1 parent 4e02c64 commit 6c2a994
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.framework.common.objects.Attribute;
import org.identityconnectors.framework.common.objects.AttributeDelta;
Expand All @@ -46,6 +47,7 @@ public final class POJOHelper {
pojoModule.addSerializer(Attribute.class, new AttributeSerializer());
pojoModule.addSerializer(AttributeDelta.class, new AttributeDeltaSerializer());
pojoModule.addSerializer(SyncToken.class, new SyncTokenSerializer());
pojoModule.addSerializer(PropagationTaskInfo.class, new PropagationTaskInfoSerializer());
pojoModule.addDeserializer(GuardedString.class, new GuardedStringDeserializer());
pojoModule.addDeserializer(Attribute.class, new AttributeDeserializer());
pojoModule.addDeserializer(AttributeDelta.class, new AttributeDeltaDeserializer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.syncope.core.provisioning.api.serialization;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;

class PropagationTaskInfoSerializer extends JsonSerializer<PropagationTaskInfo> {

@Override
public void serialize(final PropagationTaskInfo propagationTaskInfo, final JsonGenerator jgen,
final SerializerProvider serializerProvider) throws IOException {

jgen.writeStartObject();

jgen.writeStringField("key", propagationTaskInfo.getKey());

if (propagationTaskInfo.getResource() != null) {
jgen.writeStringField("resource", propagationTaskInfo.getResource().getKey());
}
if (propagationTaskInfo.getOperation() != null) {
jgen.writeStringField("operation", propagationTaskInfo.getOperation().name());
}
if (propagationTaskInfo.getObjectClass() != null) {
jgen.writeStringField("objectClass", propagationTaskInfo.getObjectClass().getObjectClassValue());
}
if (propagationTaskInfo.getAnyTypeKind() != null) {
jgen.writeStringField("anyTypeKind", propagationTaskInfo.getAnyTypeKind().name());
}
jgen.writeStringField("anyType", propagationTaskInfo.getAnyType());
jgen.writeStringField("entityKey", propagationTaskInfo.getEntityKey());
jgen.writeStringField("connObjectKey", propagationTaskInfo.getConnObjectKey());
jgen.writeStringField("oldConnObjectKey", propagationTaskInfo.getOldConnObjectKey());
jgen.writeObjectField("propagationData", propagationTaskInfo.getPropagationData());
if (propagationTaskInfo.getConnector() != null) {
jgen.writeObjectField("connector", propagationTaskInfo.getConnector().getConnInstance().getKey());
}
jgen.writeObjectField("beforeObj", propagationTaskInfo.getBeforeObj());

jgen.writeEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.syncope.core.provisioning.api.serialization;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.UUID;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.ResourceOperation;
import org.apache.syncope.core.persistence.api.entity.ConnInstance;
import org.apache.syncope.core.persistence.api.entity.ExternalResource;
import org.apache.syncope.core.persistence.api.entity.task.PropagationData;
import org.apache.syncope.core.provisioning.api.AbstractTest;
import org.apache.syncope.core.provisioning.api.Connector;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

public class PropagationTaskInfoSerializerTest extends AbstractTest {

@Test
public void propagationTaskInfoSerializer(final @Mock JsonGenerator jgen, final @Mock SerializerProvider sp)
throws IOException {

PropagationTaskInfoSerializer serializer = new PropagationTaskInfoSerializer();
ExternalResource resource = mock(ExternalResource.class);
when(resource.getKey()).thenReturn("resource-ldap");
PropagationData propagationData = mock(PropagationData.class);
PropagationTaskInfo source =
new PropagationTaskInfo(resource,
ResourceOperation.UPDATE,
ObjectClass.ACCOUNT,
AnyTypeKind.USER,
AnyTypeKind.USER.name(),
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
propagationData);
source.setKey(UUID.randomUUID().toString());
Connector connector = mock(Connector.class);
ConnInstance connInstance = mock(ConnInstance.class);
when(connInstance.getKey()).thenReturn(UUID.randomUUID().toString());
when(connector.getConnInstance()).thenReturn(connInstance);
source.setConnector(connector);
serializer.serialize(source, jgen, sp);
verify(jgen).writeStartObject();
verify(jgen).writeStringField("key", source.getKey());
verify(jgen).writeStringField("anyType", source.getAnyType());
verify(jgen).writeStringField("entityKey", source.getEntityKey());
verify(jgen).writeStringField("connObjectKey", source.getConnObjectKey());
verify(jgen).writeStringField("oldConnObjectKey", source.getOldConnObjectKey());
verify(jgen).writeObjectField("propagationData", source.getPropagationData());
verify(jgen).writeObjectField("connector", source.getConnector().getConnInstance().getKey());
verify(jgen).writeEndObject();
}
}

0 comments on commit 6c2a994

Please sign in to comment.