Skip to content

Commit

Permalink
Merge pull request #1023 from micronaut-projects/apache-client-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andriy-dmytruk authored Nov 19, 2024
2 parents bdc75bd + c817e34 commit c2cb342
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
*/
package io.micronaut.oraclecloud.httpclient.apache.core.serde;

import com.oracle.bmc.auth.internal.GetResourcePrincipalSessionTokenRequest;
import com.oracle.bmc.auth.internal.JWK;
import com.oracle.bmc.auth.internal.X509FederationClient;
import com.oracle.bmc.auth.okeworkloadidentity.internal.GetOkeResourcePrincipalSessionTokenDetails;
import com.oracle.bmc.auth.okeworkloadidentity.internal.OkeResourcePrincipalSessionToken;
import com.oracle.bmc.http.internal.ResponseHelper;
import com.oracle.bmc.model.RegionSchema;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.serde.annotation.SerdeImport;
import io.micronaut.serde.config.SerdeConfiguration;

/**
Expand All @@ -29,6 +37,14 @@
@Bean(typed = OciSerdeConfiguration.class)
@Internal
@BootstrapContextCompatible
@SerdeImport(GetResourcePrincipalSessionTokenRequest.class)
@SerdeImport(JWK.class)
@SerdeImport(RegionSchema.class)
@SerdeImport(ResponseHelper.ErrorCodeAndMessage.class)
@SerdeImport(X509FederationClient.SecurityToken.class)
@SerdeImport(X509FederationClient.X509FederationRequest.class)
@SerdeImport(GetOkeResourcePrincipalSessionTokenDetails.class)
@SerdeImport(OkeResourcePrincipalSessionToken.class)
public interface OciSerdeConfiguration extends SerdeConfiguration {
@Override
@Bindable(defaultValue = "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@ import com.oracle.bmc.auth.internal.GetResourcePrincipalSessionTokenRequest
import com.oracle.bmc.auth.internal.JWK
import com.oracle.bmc.auth.internal.X509FederationClient
import com.oracle.bmc.model.RegionSchema
import io.micronaut.runtime.server.EmbeddedServer

class AdditionalModelsSerdeSpec extends SerdeSpecBase {

void "test additional models are serdeable"() {
given:
EmbeddedServer embeddedServer = initContext()

expect:
json == echoTest(embeddedServer, value)
json == serialize(value)

when:
echoTest(embeddedServer, json, value.getClass())
deserialize(json, value.getClass())

then:
noExceptionThrown()

cleanup:
embeddedServer.close()

where:
value | json
new RegionSchema("key", "comp", "region key", "identifier")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package io.micronaut.oraclecloud.serde

import com.oracle.bmc.auth.SessionTokenAuthenticationDetailsProvider
import com.oracle.bmc.http.client.HttpProvider
import com.oracle.bmc.http.client.Serializer


class AuthSerdeSpec extends SerdeSpecBase {

// We cannot change the session token authentication details provider's endpoint
// so we just verify that we can serialize the needed beans.
void "session token authentication request serialization test"() throws IOException {
given:

Serializer serializer = HttpProvider.getDefault().getSerializer()

when:
String request = serializer.writeValueAsString(
String request = serialize(
new SessionTokenAuthenticationDetailsProvider.SessionTokenRefreshRequest.SessionTokenRequest("my-token"))

then:
"{\"currentToken\":\"my-token\"}" == request
}

void "session token authentication response deserialization test"() throws IOException {
given:
Serializer serializer = HttpProvider.getDefault().getSerializer()

when:
SessionTokenAuthenticationDetailsProvider.SessionToken response =
serializer.readValue("{\"token\":\"new-token\"}", SessionTokenAuthenticationDetailsProvider.SessionToken.class)
deserialize("{\"token\":\"new-token\"}", SessionTokenAuthenticationDetailsProvider.SessionToken.class)

then:
"new-token" == response.getToken()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.micronaut.oraclecloud.serde

import com.oracle.bmc.http.client.internal.ExplicitlySetBmcModel
import io.micronaut.runtime.server.EmbeddedServer

import java.time.Instant
import java.time.LocalDateTime
Expand All @@ -12,30 +11,22 @@ import java.time.ZonedDateTime
class DateSerdeSpec extends SerdeSpecBase {

void "test Date serialization"() {
given:
EmbeddedServer embeddedServer = initContext()

when:
def dateTime = ZonedDateTime.of(
LocalDateTime.of(2000, Month.JANUARY, 2, 3, 4, 5, (int) 678e6),
ZoneId.of("America/Toronto")
)
def date = new Date(dateTime.toInstant().toEpochMilli())

var value = echoTest(embeddedServer, date).replaceAll('"', '')
var value = serialize(date).replaceAll('"', '')
var requiredValue = "2000-01-02T08:04:05.678Z"

then:
requiredValue == value

cleanup:
embeddedServer.close()
}

void "test Date deserialization '#dateString'"() {
given:
EmbeddedServer embeddedServer = initContext()

Calendar calendar = Calendar.getInstance(
TimeZone.getTimeZone(ZoneId.of('-05:00')),
Locale.CANADA
Expand All @@ -45,36 +36,27 @@ class DateSerdeSpec extends SerdeSpecBase {
Date requiredDate = calendar.getTime()

when:
var dateModel = echoTest(embeddedServer, dateString, DateModel)
var dateModel = deserialize(dateString, DateModel)

then:
requiredDate == dateModel.date

cleanup:
embeddedServer.close()

where:
dateString | _
'{"date":"2000-01-02T03:04:56.789-05:00"}' | _
'{"date":"2000-01-02T08:04:56.789Z"}' | _
'{"date":"2000-01-02T09:04:56.789+01:00"}' | _
dateString | _
'{"date":"2000-01-02T03:04:56.789-05:00"}' | _
'{"date":"2000-01-02T08:04:56.789Z"}' | _
'{"date":"2000-01-02T09:04:56.789+01:00"}' | _
}

void "test Date deserialization '#dateText'"() {
given:
EmbeddedServer embeddedServer = initContext()

when:
var body = "{\"date\":\"${dateText}\"}"
var dateModel = echoTest(embeddedServer, body.toString(), DateModel)
var dateModel = deserialize(body.toString(), DateModel)
var date = Date.from(Instant.parse(dateText))

then:
date == dateModel.date

cleanup:
embeddedServer.close()

where:
dateText | _
'1937-01-01T12:00:27.873834939+00:20' | _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@ package io.micronaut.oraclecloud.serde


import io.micronaut.oraclecloud.serde.model.TestStateEnum
import io.micronaut.runtime.server.EmbeddedServer

class EnumSerdeSpec extends SerdeSpecBase {

void "test enum serialization"() {
given:
EmbeddedServer embeddedServer = initContext()

expect:
echoTest(embeddedServer, TestStateEnum.Active) == '"active"'
echoTest(embeddedServer, TestStateEnum.Inactive) == '"inactive"'
echoTest(embeddedServer, TestStateEnum.Deleted) == '"deleted"'
echoTest(embeddedServer, TestStateEnum.UnknownEnumValue) == 'null'

cleanup:
embeddedServer.close()
serialize(TestStateEnum.Active) == '"active"'
serialize(TestStateEnum.Inactive) == '"inactive"'
serialize(TestStateEnum.Deleted) == '"deleted"'
serialize(TestStateEnum.UnknownEnumValue) == 'null'
}

void "test enum deserializatioin"() {
given:
EmbeddedServer embeddedServer = initContext()

void "test enum deserialization"() {
expect:
echoTest(embeddedServer, '"active"', TestStateEnum) == TestStateEnum.Active
echoTest(embeddedServer, '"inactive"', TestStateEnum) == TestStateEnum.Inactive
echoTest(embeddedServer, '"deleted"', TestStateEnum) == TestStateEnum.Deleted
echoTest(embeddedServer, 'null', TestStateEnum) == TestStateEnum.UnknownEnumValue
echoTest(embeddedServer, '"unknown value"', TestStateEnum) == TestStateEnum.UnknownEnumValue

cleanup:
embeddedServer.close()
deserialize('"active"', TestStateEnum) == TestStateEnum.Active
deserialize('"inactive"', TestStateEnum) == TestStateEnum.Inactive
deserialize('"deleted"', TestStateEnum) == TestStateEnum.Deleted
deserialize('null', TestStateEnum) == TestStateEnum.UnknownEnumValue
deserialize('"unknown value"', TestStateEnum) == TestStateEnum.UnknownEnumValue
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ package io.micronaut.oraclecloud.serde


import com.oracle.bmc.http.internal.ResponseHelper
import io.micronaut.runtime.server.EmbeddedServer

class ErrorResponseSerdeSpec extends SerdeSpecBase {

void "ErrorCodeAndMessage deserialization test"() throws Exception {
given:
EmbeddedServer embeddedServer = initContext()

when:
var response = echoTest(embeddedServer, body, ResponseHelper.ErrorCodeAndMessage)
var response = deserialize(body, ResponseHelper.ErrorCodeAndMessage)

then:
builder.build() == response

cleanup:
embeddedServer.close()

where:
body | builder
'{"code":"Unauthorized","message":"Access not allowed"}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,53 @@ import com.fasterxml.jackson.annotation.JsonFilter
import com.oracle.bmc.http.client.internal.ExplicitlySetBmcModel
import io.micronaut.oraclecloud.serde.model.BaseModel
import io.micronaut.oraclecloud.serde.model.ComplexModel
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.Unroll

class ExplicitlySetSerdeSpec extends SerdeSpecBase {

void "OCI SDK Model serialization test"() throws Exception {
given:
EmbeddedServer embeddedServer = initContext()
MyModel myModel = new MyModel("value")

when:
var body = echoTest(embeddedServer, myModel)
var body = serialize(myModel)

then:
'{"string":"value"}' == body

cleanup:
embeddedServer.close()
}

void "Explicitly set OCI SDK Model serialization test"() throws Exception {
given:
EmbeddedServer embeddedServer = initContext()

when:
var body = echoTest(embeddedServer, new MyModel(null))
var body = serialize(new MyModel(null))

then:
'{"string":null}' == body

when:
body = echoTest(embeddedServer, new MyModel())
body = serialize(new MyModel())

then:
'{}' == body

cleanup:
embeddedServer.close()
}

void "Test extra properties deserialization does not throw exception"() {
given:
EmbeddedServer embeddedServer = initContext()

when:
var value = '{"string":"value","extraString":"extraValue"}'
var myModel = echoTest(embeddedServer, value, MyModel)
var myModel = deserialize(value, MyModel)

then:
notThrown(Exception)
myModel.string == "value"

cleanup:
embeddedServer.close()
}

@Unroll
void "Complex model serialization test #modelBuilder"() {
given:
EmbeddedServer embeddedServer = initContext()

when:
var response = echoTest(embeddedServer, modelBuilder.build())
var response = serialize(modelBuilder.build())

then:
response == json

cleanup:
embeddedServer.close()

where:
modelBuilder | json
ComplexModel.builder() | '{"type":"complex"}'
Expand All @@ -87,17 +64,12 @@ class ExplicitlySetSerdeSpec extends SerdeSpecBase {

@Unroll
void "Simple model deserialization for #modelBuilder"() {
EmbeddedServer embeddedServer = initContext()

when:
var response = echoTest(embeddedServer, json, BaseModel)
var response = deserialize(json, BaseModel)

then:
modelBuilder.build().equals(response, false)

cleanup:
embeddedServer.close()

where:
modelBuilder | json
BaseModel.builder() | '{}'
Expand All @@ -111,17 +83,12 @@ class ExplicitlySetSerdeSpec extends SerdeSpecBase {

@Unroll
void "Complex model deserialization for #modelBuilder"() {
EmbeddedServer embeddedServer = initContext()

when:
var response = echoTest(embeddedServer, json, BaseModel)
var response = deserialize(json, BaseModel)

then:
modelBuilder.build().equals(response, false)

cleanup:
embeddedServer.close()

where:
modelBuilder | json
ComplexModel.builder() | '{"type":"complex"}'
Expand Down
Loading

0 comments on commit c2cb342

Please sign in to comment.