Skip to content

Commit ea9de1a

Browse files
authored
Merge pull request #126 from commercetools/refactor_objectmapper
2 parents 3a86614 + bfde4f5 commit ea9de1a

File tree

83 files changed

+1332
-473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1332
-473
lines changed

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/MiddlewareTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.vrap.rmf.base.client.error.NotFoundException;
2020
import io.vrap.rmf.base.client.http.RetryMiddleware;
2121
import io.vrap.rmf.base.client.oauth2.ClientCredentials;
22-
import io.vrap.rmf.base.client.oauth2.ClientCredentialsTokenSupplier;
2322

2423
import org.assertj.core.api.Assertions;
2524
import org.assertj.core.util.Lists;
@@ -61,7 +60,7 @@ public void testUnauthorizedToken() throws ExecutionException, InterruptedExcept
6160
.build();
6261

6362
VrapHttpClient httpClient = HttpClientSupplier.of().get();
64-
ApiHttpClient client = ClientFactory.create(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl(), new VrapHttpClient() {
63+
ApiHttpClient client = ClientBuilder.of(new VrapHttpClient() {
6564
private boolean firstCall = true;
6665

6766
@Override
@@ -73,8 +72,10 @@ public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request
7372
}
7473
return httpClient.execute(request);
7574
}
76-
}, new ClientCredentialsTokenSupplier(credentials.getClientId(), credentials.getClientSecret(),
77-
credentials.getScopes(), ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), HttpClientSupplier.of().get()));
75+
})
76+
.defaultClient(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
77+
.withClientCredentials(credentials, ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), httpClient)
78+
.build();
7879

7980
final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET,
8081
URI.create("/" + CommercetoolsTestUtils.getProjectKey() + "/"), new ApiHttpHeaders(), null);

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/custom_object/CustomObjectFixtures.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import com.commercetools.api.models.custom_object.CustomObjectDraft;
99
import com.commercetools.api.models.custom_object.CustomObjectDraftBuilder;
1010
import commercetools.utils.CommercetoolsTestUtils;
11-
12-
import io.vrap.rmf.base.client.utils.json.JsonUtils;
11+
import commercetools.utils.ValueObject;
1312

1413
import org.junit.Assert;
1514

@@ -31,7 +30,7 @@ public static CustomObject createCustomObject() {
3130
CustomObjectDraft customObjectDraft = CustomObjectDraftBuilder.of()
3231
.container("a")
3332
.key(CommercetoolsTestUtils.randomKey())
34-
.value(JsonUtils.getConfiguredObjectMapper().createObjectNode().put("value", "val"))
33+
.value((ValueObject) () -> "val")
3534
.build();
3635

3736
CustomObject customObject = CommercetoolsTestUtils.getProjectRoot()

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/custom_object/CustomObjectIntegrationTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.commercetools.api.models.custom_object.CustomObjectDraftBuilder;
99
import com.commercetools.api.models.custom_object.CustomObjectPagedQueryResponse;
1010
import commercetools.utils.CommercetoolsTestUtils;
11+
import commercetools.utils.ValueObject;
1112

1213
import io.vrap.rmf.base.client.utils.json.JsonUtils;
1314

@@ -46,7 +47,7 @@ public void update() {
4647
CustomObjectDraft customObjectDraft = CustomObjectDraftBuilder.of()
4748
.key(customObject.getKey())
4849
.container(customObject.getContainer())
49-
.value(JsonUtils.getConfiguredObjectMapper().createObjectNode().put("value", "val"))
50+
.value((ValueObject) () -> "foo")
5051
.build();
5152

5253
CustomObject updatedCustomObject = CommercetoolsTestUtils.getProjectRoot()
@@ -56,7 +57,29 @@ public void update() {
5657
.getBody();
5758

5859
Assert.assertNotNull(updatedCustomObject);
59-
Assert.assertEquals(((Map<String, Object>) updatedCustomObject.getValue()).get("value"), "val");
60+
Assert.assertEquals(((Map<String, Object>) updatedCustomObject.getValue()).get("value"), "foo");
61+
62+
return updatedCustomObject;
63+
});
64+
}
65+
66+
@Test
67+
public void updateWithJsonNode() {
68+
CustomObjectFixtures.withUpdateableCustomObject(customObject -> {
69+
CustomObjectDraft customObjectDraft = CustomObjectDraftBuilder.of()
70+
.key(customObject.getKey())
71+
.container(customObject.getContainer())
72+
.value(JsonUtils.getConfiguredObjectMapper().createObjectNode().put("value", "foo"))
73+
.build();
74+
75+
CustomObject updatedCustomObject = CommercetoolsTestUtils.getProjectRoot()
76+
.customObjects()
77+
.post(customObjectDraft)
78+
.executeBlocking()
79+
.getBody();
80+
81+
Assert.assertNotNull(updatedCustomObject);
82+
Assert.assertEquals(((Map<String, Object>) updatedCustomObject.getValue()).get("value"), "foo");
6083

6184
return updatedCustomObject;
6285
});

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/me/MyCartsIntegrationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import com.commercetools.api.models.me.MyCartDraftBuilder;
1313
import commercetools.utils.CommercetoolsTestUtils;
1414

15-
import io.vrap.rmf.base.client.ApiHttpClient;
16-
import io.vrap.rmf.base.client.ClientFactory;
17-
import io.vrap.rmf.base.client.HttpClientSupplier;
18-
import io.vrap.rmf.base.client.VrapHttpClient;
15+
import io.vrap.rmf.base.client.*;
1916
import io.vrap.rmf.base.client.oauth2.AnonymousSessionTokenSupplier;
2017
import io.vrap.rmf.base.client.oauth2.StaticTokenSupplier;
2118

@@ -35,8 +32,10 @@ public void signInWithAnonymousCartWithNewActiveCart() throws ExecutionException
3532
final StaticTokenSupplier staticTokenSupplier = new StaticTokenSupplier(
3633
anonymousSessionTokenSupplier.getToken().get());
3734

38-
final ApiHttpClient apiAnonymousHttpClient = ClientFactory
39-
.create("https://api.europe-west1.gcp.commercetools.com/", vrapHttpClient, staticTokenSupplier);
35+
final ApiHttpClient apiAnonymousHttpClient = ClientBuilder.of(vrapHttpClient)
36+
.withApiBaseUrl("https://api.europe-west1.gcp.commercetools.com/")
37+
.withTokenSupplier(staticTokenSupplier)
38+
.build();
4039
ApiRoot apiAnonymousRoot = ApiFactory.create(() -> apiAnonymousHttpClient);
4140

4241
MyCartDraft anonymousMyCartDraft = MyCartDraftBuilder.of().currency("EUR").country("DE").build();

commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/misc/ModelSerializationTest.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515
import com.commercetools.api.models.category.*;
1616
import com.commercetools.api.models.common.*;
1717
import com.commercetools.api.models.type.*;
18-
import com.fasterxml.jackson.core.JsonProcessingException;
19-
import com.google.gson.JsonElement;
2018
import com.google.gson.JsonParser;
2119

2220
import io.vrap.rmf.base.client.utils.json.JsonUtils;
2321

22+
import org.json.JSONException;
2423
import org.junit.Assert;
2524
import org.junit.Test;
25+
import org.skyscreamer.jsonassert.JSONAssert;
2626

2727
public class ModelSerializationTest {
2828

2929
@Test
30-
public void serializeCategoryDraftToJson() {
30+
public void serializeCategoryDraftToJson() throws IOException, JSONException {
3131
LocalizedString localizedString = new LocalizedStringImpl();
3232
localizedString.setValue("test-key", "test-value");
3333
String key = "test-key";
3434
String id = "test-id";
3535
String testString = "test-string";
3636

3737
Map<String, Object> fieldContainerValues = new HashMap<>();
38-
fieldContainerValues.put(key, JsonUtils.getConfiguredObjectMapper().createObjectNode().put("val", testString));
38+
fieldContainerValues.put(key, testString);
3939
FieldContainer fieldContainer = FieldContainerBuilder.of().values(fieldContainerValues).build();
4040

4141
AssetDraft assetDraft = AssetDraftBuilder.of()
@@ -71,26 +71,11 @@ public void serializeCategoryDraftToJson() {
7171
.slug(localizedString)
7272
.build();
7373

74-
JsonElement categoryDraftJson = null;
75-
try {
76-
categoryDraftJson = JsonParser.parseString(JsonUtils.toJsonString(categoryDraft));
77-
}
78-
catch (JsonProcessingException e) {
79-
e.printStackTrace();
80-
}
81-
82-
try {
83-
final URL url = Thread.currentThread()
84-
.getContextClassLoader()
85-
.getResource("json_examples/category-draft-example.json");
86-
JsonElement categoryDraftExample = JsonParser
87-
.parseString(new String(Files.readAllBytes(Paths.get(url.getPath()))));
88-
Assert.assertEquals(categoryDraftExample, categoryDraftJson);
89-
}
90-
catch (IOException e) {
91-
e.printStackTrace();
92-
Assert.fail(e.getMessage());
93-
}
74+
final URL url = Thread.currentThread()
75+
.getContextClassLoader()
76+
.getResource("json_examples/category-draft-example.json");
77+
JSONAssert.assertEquals(new String(Files.readAllBytes(Paths.get(url.getPath()))),
78+
JsonUtils.toJsonString(categoryDraft), true);
9479
}
9580

9681
@Test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
package commercetools.utils;
3+
4+
public interface ValueObject {
5+
String getValue();
6+
}

commercetools/commercetools-sdk-java-api/src/integrationTest/resources/json_examples/category-draft-example.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
],
1818
"custom": {
1919
"fields": {
20-
"test-key": {
21-
"val": "test-string"
22-
}
20+
"test-key": "test-string"
2321
},
2422
"type": {
2523
"key": "string type",
@@ -45,9 +43,7 @@
4543
"orderHint": "test-string",
4644
"custom": {
4745
"fields": {
48-
"test-key": {
49-
"val": "test-string"
50-
}
46+
"test-key": "test-string"
5147
},
5248
"type": {
5349
"key": "string type",

commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ApiInternalLoggerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
import io.vrap.rmf.base.client.ApiHttpMethod;
55
import io.vrap.rmf.base.client.ApiHttpRequest;
6-
import io.vrap.rmf.base.client.ClientFactory;
6+
import io.vrap.rmf.base.client.ClientBuilder;
77
import io.vrap.rmf.base.client.http.InternalLogger;
88

99
public class ApiInternalLoggerFactory {
1010

1111
public static InternalLogger get(final ApiHttpRequest httpRequest, final String topic) {
12-
return InternalLogger.getLogger(ClientFactory.COMMERCETOOLS + "." + getPathElement(httpRequest) + "." + topic
12+
return InternalLogger.getLogger(ClientBuilder.COMMERCETOOLS + "." + getPathElement(httpRequest) + "." + topic
1313
+ (topic.equals("request") ? "." + requestOrCommandScopeSegment(httpRequest) : ""));
1414
}
1515

commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/defaultconfig/ApiFactory.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99

1010
import com.commercetools.api.client.*;
1111

12-
import io.vrap.rmf.base.client.ApiHttpClient;
13-
import io.vrap.rmf.base.client.ClientFactory;
14-
import io.vrap.rmf.base.client.HttpClientSupplier;
15-
import io.vrap.rmf.base.client.VrapHttpClient;
12+
import io.vrap.rmf.base.client.*;
1613
import io.vrap.rmf.base.client.http.CorrelationIdProvider;
1714
import io.vrap.rmf.base.client.http.Middleware;
1815
import io.vrap.rmf.base.client.oauth2.ClientCredentials;
19-
import io.vrap.rmf.base.client.oauth2.ClientCredentialsTokenSupplier;
2016

2117
public class ApiFactory {
2218

@@ -155,19 +151,25 @@ public static ApiHttpClient defaultClient(final VrapHttpClient httpClient, final
155151
public static ApiHttpClient defaultClient(final Supplier<VrapHttpClient> httpClientSupplier,
156152
final ClientCredentials credentials, final String tokenEndpoint, final String apiEndpoint,
157153
final List<Middleware> middlewares, @Nullable final CorrelationIdProvider correlationIdProvider) {
158-
return ClientFactory.create(apiEndpoint, httpClientSupplier.get(),
159-
new ClientCredentialsTokenSupplier(credentials.getClientId(), credentials.getClientSecret(),
160-
credentials.getScopes(), tokenEndpoint, httpClientSupplier.get()),
161-
ApiInternalLoggerFactory::get, middlewares, correlationIdProvider);
154+
return ClientBuilder.of(httpClientSupplier.get())
155+
.defaultClient(apiEndpoint)
156+
.withClientCredentials(credentials, tokenEndpoint)
157+
.withInternalLoggerFactory(ApiInternalLoggerFactory::get)
158+
.addCorrelationIdProvider(correlationIdProvider)
159+
.addMiddlewares(middlewares)
160+
.build();
162161
}
163162

164163
public static ApiHttpClient defaultClient(final Supplier<VrapHttpClient> httpClientSupplier,
165164
final ClientCredentials credentials, final String tokenEndpoint, final String apiEndpoint,
166165
final Supplier<String> userAgentSupplier, final List<Middleware> middlewares,
167166
@Nullable final CorrelationIdProvider correlationIdProvider) {
168-
return ClientFactory.create(apiEndpoint, httpClientSupplier.get(),
169-
new ClientCredentialsTokenSupplier(credentials.getClientId(), credentials.getClientSecret(),
170-
credentials.getScopes(), tokenEndpoint, httpClientSupplier.get()),
171-
ApiInternalLoggerFactory::get, userAgentSupplier, middlewares, correlationIdProvider);
167+
return ClientBuilder.of(httpClientSupplier.get())
168+
.defaultClient(apiEndpoint, credentials, tokenEndpoint)
169+
.withInternalLoggerFactory(ApiInternalLoggerFactory::get)
170+
.withUserAgentSupplier(userAgentSupplier)
171+
.addCorrelationIdProvider(correlationIdProvider)
172+
.addMiddlewares(middlewares)
173+
.build();
172174
}
173175
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11

22
package com.commercetools.api.json;
33

4+
import java.util.Optional;
5+
46
import com.commercetools.api.models.product.AttributeImpl;
57
import com.commercetools.api.models.review.Review;
68
import com.commercetools.api.models.type.FieldContainerImpl;
79
import com.fasterxml.jackson.databind.module.SimpleModule;
810

11+
import io.vrap.rmf.base.client.utils.json.modules.ModuleOptions;
12+
913
public class ApiModule extends SimpleModule {
1014
private static final long serialVersionUID = 0L;
1115

12-
public ApiModule() {
13-
addDeserializer(AttributeImpl.class, new AtrributeDeserializer());
14-
addDeserializer(FieldContainerImpl.class, new CustomFieldDeserializer());
16+
public ApiModule(ModuleOptions options) {
17+
boolean attributeAsDateString = Boolean.parseBoolean(
18+
Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_DATE_ATTRIBUTE_AS_STRING))
19+
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_DATE_ATTRIBUTE_AS_STRING)));
20+
boolean customFieldAsDateString = Boolean
21+
.parseBoolean(Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_DATE_FIELD_AS_STRING))
22+
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_DATE_FIELD_AS_STRING)));
23+
addDeserializer(AttributeImpl.class, new AtrributeDeserializer(attributeAsDateString));
24+
addDeserializer(FieldContainerImpl.class, new CustomFieldDeserializer(customFieldAsDateString));
1525
setMixInAnnotation(Review.class, ReviewMixin.class);
1626
}
1727
}

0 commit comments

Comments
 (0)