Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polymorphic deserialization support for LogStream #940

Merged
Merged
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 @@ -38,6 +38,8 @@
import com.okta.sdk.impl.deserializer.PolicyDeserializer;
import com.okta.sdk.impl.deserializer.UserFactorDeserializer;
import com.okta.sdk.impl.deserializer.UserProfileDeserializer;
import com.okta.sdk.impl.deserializer.PolicyRuleDeserializer;
import com.okta.sdk.impl.deserializer.PolymorphicMixIns;
import com.okta.sdk.impl.io.ClasspathResource;
import com.okta.sdk.impl.io.DefaultResourceFactory;
import com.okta.sdk.impl.io.Resource;
Expand Down Expand Up @@ -72,6 +74,7 @@

import com.okta.sdk.resource.model.Application;
import com.okta.sdk.resource.model.Policy;
import com.okta.sdk.resource.model.PolicyRule;
import com.okta.sdk.resource.model.UserFactor;
import com.okta.sdk.resource.model.UserProfile;
import org.slf4j.Logger;
Expand Down Expand Up @@ -468,7 +471,9 @@ private void addCustomSerializerAndDeserializers(ApiClient apiClient) {
module.addDeserializer(UserProfile.class, new UserProfileDeserializer());
module.addDeserializer(Application.class, new ApplicationDeserializer());
module.addDeserializer(Policy.class, new PolicyDeserializer());
module.addDeserializer(PolicyRule.class, new PolicyRuleDeserializer());
module.addDeserializer(UserFactor.class, new UserFactorDeserializer());
PolymorphicMixIns.MIX_INS.forEach(module::setMixInAnnotation);
mapper.registerModule(module);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023-Present Okta, Inc.
*
* 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 com.okta.sdk.impl.deserializer;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.okta.sdk.resource.model.LogStream;
import com.okta.sdk.resource.model.LogStreamAws;
import com.okta.sdk.resource.model.LogStreamSplunk;

import java.util.HashMap;
import java.util.Map;

import static com.fasterxml.jackson.annotation.JsonTypeInfo.*;

/**
* Mix-ins to add to polymorphic resources, so they are deserialized as the correct type.
*/
public interface PolymorphicMixIns {

/**
* Mix-ins to register in the ObjectMapper.
*/
Map<Class<?>, Class<?>> MIX_INS = new HashMap<Class<?>, Class<?>>() {{
put(LogStream.class, LogStreamMixIn.class);
}};

@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, visible = true, property = "type")
@JsonSubTypes({
@Type(name = "aws_eventbridge", value = LogStreamAws.class),
@Type(name = "splunk_cloud_logstreaming", value = LogStreamSplunk.class)
})

abstract class LogStreamMixIn {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023-Present Okta, Inc.
*
* 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 com.okta.sdk.impl.deserializer

import com.okta.sdk.impl.client.DefaultClientBuilder
import com.okta.sdk.impl.test.RestoreEnvironmentVariables
import com.okta.sdk.impl.test.RestoreSystemProperties
import com.okta.sdk.resource.model.AwsRegion
import com.okta.sdk.resource.model.LogStream
import com.okta.sdk.resource.model.LogStreamAws
import com.okta.sdk.resource.model.LogStreamSettingsAws
import com.okta.sdk.resource.model.LogStreamSettingsSplunk
import com.okta.sdk.resource.model.LogStreamSplunk
import com.okta.sdk.resource.model.LogStreamType
import com.okta.sdk.resource.model.SplunkEdition
import org.testng.annotations.Listeners
import org.testng.annotations.Test

import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.core.IsEqual.equalTo
import static org.hamcrest.core.IsInstanceOf.instanceOf

@Listeners([RestoreSystemProperties, RestoreEnvironmentVariables])
class PolymorphicMixInsTest {

def mapper = new DefaultClientBuilder().build().getObjectMapper()

@Test
void testLogStreamAws() {
def settings = new LogStreamSettingsAws()
.accountId("123456")
.region(AwsRegion.US_EAST_1)
.eventSourceName("source")
def logStream = new LogStreamAws()
.settings(settings)
.type(LogStreamType.AWS_EVENTBRIDGE)
.name("test")
def asString = mapper.writeValueAsString(logStream)
def afterRoundTrip = mapper.readValue(asString, LogStream.class)
assertThat(afterRoundTrip, instanceOf(LogStreamAws.class))
assertThat(((LogStreamAws)afterRoundTrip).settings, equalTo(settings))
}

@Test
void testLogStreamSplunk() {
def settings = new LogStreamSettingsSplunk()
.edition(SplunkEdition.AWS)
.host("host")
.token("token")
def logStream = new LogStreamSplunk()
.settings(settings)
.type(LogStreamType.SPLUNK_CLOUD_LOGSTREAMING)
.name("test")
def asString = mapper.writeValueAsString(logStream)
def afterRoundTrip = mapper.readValue(asString, LogStream.class)
assertThat(afterRoundTrip, instanceOf(LogStreamSplunk.class))
assertThat(((LogStreamSplunk)afterRoundTrip).settings, equalTo(settings))
}
}