Skip to content

Commit aa1b1ae

Browse files
committed
Merge branch 'release/2.1.0'
2 parents b24ddd9 + 72f962d commit aa1b1ae

25 files changed

+130
-60
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ final ShopifySdk shopifySdk = ShopifySdk.newBuilder()
2020
final ShopifyShop shopifyShop = shopifySdk.getShop();
2121
```
2222

23+
For private apps, `accessToken` should be the private app's Admin API password.
24+
2325
## Optional Configuration
2426
The final parameters of the SDK builder are optional and will use default values when not supplied:
2527

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.channelape</groupId>
66
<artifactId>shopify-sdk</artifactId>
7-
<version>2.0.1</version>
7+
<version>2.1.0</version>
88

99
<name>Shopify SDK</name>
1010
<description>Java SDK for Shopify REST API.</description>
@@ -56,7 +56,7 @@
5656
<maven.compiler.source>1.8</maven.compiler.source>
5757
<maven.compiler.target>1.8</maven.compiler.target>
5858
<java.version>1.8</java.version>
59-
<junit.version>4.12</junit.version>
59+
<junit.version>4.13.1</junit.version>
6060
<slf4j.version>1.7.22</slf4j.version>
6161
<jersey.version>2.25.1</jersey.version>
6262
<pitest.version>1.4.3</pitest.version>
@@ -94,6 +94,12 @@
9494
<artifactId>commons-lang3</artifactId>
9595
<version>3.4</version>
9696
</dependency>
97+
<dependency>
98+
<groupId>org.apache.commons</groupId>
99+
<artifactId>commons-io</artifactId>
100+
<version>1.3.2</version>
101+
</dependency>
102+
97103
<dependency>
98104
<groupId>joda-time</groupId>
99105
<artifactId>joda-time</artifactId>

src/main/java/com/shopify/ShopifySdk.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.github.rholder.retry.WaitStrategies;
3838
import com.shopify.exceptions.ShopifyClientException;
3939
import com.shopify.exceptions.ShopifyErrorResponseException;
40+
import com.shopify.mappers.ResponseEntityToStringMapper;
4041
import com.shopify.mappers.ShopifySdkObjectMapper;
4142
import com.shopify.model.Count;
4243
import com.shopify.model.Image;
@@ -996,9 +997,8 @@ private static boolean isServerError(final Response response) {
996997
}
997998

998999
private static boolean hasNotBeenSaved(final Response response) {
999-
response.bufferEntity();
10001000
if ((UNPROCESSABLE_ENTITY_STATUS_CODE == response.getStatus()) && response.hasEntity()) {
1001-
final String shopifyErrorResponse = response.readEntity(String.class);
1001+
final String shopifyErrorResponse = ResponseEntityToStringMapper.map(response);
10021002
LOGGER.debug(shopifyErrorResponse);
10031003
return shopifyErrorResponse.contains(COULD_NOT_BE_SAVED_SHOPIFY_ERROR_MESSAGE);
10041004
}
@@ -1064,8 +1064,7 @@ public <V> void onRetry(final Attempt<V> attempt) {
10641064
if (attempt.hasResult()) {
10651065
final Response response = (Response) attempt.getResult();
10661066

1067-
response.bufferEntity();
1068-
final String responseBody = response.readEntity(String.class);
1067+
final String responseBody = ResponseEntityToStringMapper.map(response);
10691068

10701069
if (LOGGER.isWarnEnabled() && !hasExceededRateLimit(response) && shouldRetryResponse(response)) {
10711070

src/main/java/com/shopify/exceptions/ShopifyErrorResponseException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import javax.ws.rs.core.Response;
66

7+
import com.shopify.mappers.ResponseEntityToStringMapper;
8+
79
public class ShopifyErrorResponseException extends RuntimeException {
810

911
static final String MESSAGE = "Received unexpected Response Status Code of %d\nResponse Headers of:\n%s\nResponse Body of:\n%s";
@@ -14,15 +16,13 @@ public class ShopifyErrorResponseException extends RuntimeException {
1416

1517
public ShopifyErrorResponseException(final Response response) {
1618
super(buildMessage(response));
17-
response.bufferEntity();
18-
this.responseBody = response.readEntity(String.class);
19+
this.responseBody = ResponseEntityToStringMapper.map(response);
1920
this.shopifyErrorCodes = ShopifyErrorCodeFactory.create(responseBody);
2021
this.statusCode = response.getStatus();
2122
}
2223

2324
private static String buildMessage(final Response response) {
24-
response.bufferEntity();
25-
final String readEntity = response.readEntity(String.class);
25+
final String readEntity = ResponseEntityToStringMapper.map(response);
2626
return String.format(MESSAGE, response.getStatus(), response.getStringHeaders(), readEntity);
2727
}
2828

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.shopify.mappers;
2+
3+
import java.io.InputStream;
4+
import java.nio.charset.StandardCharsets;
5+
6+
import javax.ws.rs.core.Response;
7+
8+
import org.apache.commons.io.IOUtils;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class ResponseEntityToStringMapper {
13+
14+
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseEntityToStringMapper.class);
15+
16+
private ResponseEntityToStringMapper() {
17+
}
18+
19+
/**
20+
* JAXRS Jersey Client implementation closes stream buffers when reading entity
21+
* by string. To combat this and be able to read entities via a string more than
22+
* once, this deals with the input streams involved and resets where necessary.
23+
*
24+
* @param response
25+
* @return
26+
*/
27+
public static String map(final Response response) {
28+
29+
try {
30+
response.bufferEntity();
31+
final InputStream entityBytes = (InputStream) response.getEntity();
32+
final String responseBody = IOUtils.toString(entityBytes, StandardCharsets.UTF_8.toString());
33+
entityBytes.reset();
34+
return responseBody;
35+
} catch (final Exception e) {
36+
LOGGER.error("There was an error parsing response body on response with status {}, returning null",
37+
response.getStatus());
38+
return null;
39+
}
40+
}
41+
42+
}

src/test/java/com/shopify/ShopifySdkDriver.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.math.BigDecimal;
1010
import java.util.Arrays;
1111
import java.util.Collections;
12+
import java.util.LinkedList;
1213
import java.util.List;
1314
import java.util.Random;
1415
import java.util.UUID;
@@ -691,6 +692,19 @@ public void givenSomeValuesWhenUpdatingACustomerThenExpectValuesToBeUpdatedOnCus
691692
assertEquals("RyanTest", updatedCustomer.getFirstName());
692693
}
693694

695+
@Test
696+
public void givenSomeErrorOccurrsWhenCreatingFulfillmentThenExpectCorrectErrors() {
697+
try {
698+
shopifySdk.createFulfillment(ShopifyFulfillmentCreationRequest.newBuilder().withOrderId("2854620102717")
699+
.withTrackingCompany("UPS").withTrackingNumber("ABC-123").withNotifyCustomer(false)
700+
.withLineItems(new LinkedList<>()).withLocationId("5523767400")
701+
.withTrackingUrls(Arrays.asList("http://google.com/123")).build());
702+
} catch (final Exception e) {
703+
e.printStackTrace();
704+
}
705+
706+
}
707+
694708
@After
695709
public void after() {
696710
System.out.println(TEST_DELIMITER);

src/test/java/com/shopify/exceptions/ShopifyErrorResponseExceptionTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import static org.mockito.Mockito.mock;
66
import static org.mockito.Mockito.when;
77

8+
import java.io.ByteArrayInputStream;
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
811
import java.util.Arrays;
912

1013
import javax.ws.rs.core.MultivaluedHashMap;
@@ -32,7 +35,9 @@ public void givenResponseWith422StatusCodeAndSomeResponseBodyAndSomeResponsHeade
3235
when(response.getStringHeaders()).thenReturn(responseHeaders);
3336

3437
final String expectedResponseBodyString = "{\"error\": \"something went wrong.\"}";
35-
when(response.readEntity(String.class)).thenReturn(expectedResponseBodyString);
38+
final InputStream expectedResponseStream = new ByteArrayInputStream(
39+
expectedResponseBodyString.getBytes(StandardCharsets.UTF_8));
40+
when(response.getEntity()).thenReturn(expectedResponseStream);
3641

3742
final ShopifyErrorResponseException actualShopifyErrorResponseException = new ShopifyErrorResponseException(
3843
response);
@@ -65,7 +70,9 @@ public void givenResponseWith422StatusCodeAndSomeResponseBodyAndSomeResponsHeade
6570
when(response.getStringHeaders()).thenReturn(responseHeaders);
6671

6772
final String expectedResponseBodyString = "Some unaprseable error";
68-
when(response.readEntity(String.class)).thenReturn(expectedResponseBodyString);
73+
final InputStream expectedResponseStream = new ByteArrayInputStream(
74+
expectedResponseBodyString.getBytes(StandardCharsets.UTF_8));
75+
when(response.getEntity()).thenReturn(expectedResponseStream);
6976
final ShopifyErrorResponseException actualShopifyErrorResponseException = new ShopifyErrorResponseException(
7077
response);
7178

@@ -100,7 +107,9 @@ public void givenResponseWith422StatusCodeAndSomeResponseBodyAndSomeResponsHeade
100107
+ " \"address1 can't be blank, zip is not valid for united states, and city can't be blank\"\n"
101108
+ " ]\n" + " }\n" + "}";
102109

103-
when(response.readEntity(String.class)).thenReturn(expectedResponseBodyString);
110+
final InputStream expectedResponseStream = new ByteArrayInputStream(
111+
expectedResponseBodyString.getBytes(StandardCharsets.UTF_8));
112+
when(response.getEntity()).thenReturn(expectedResponseStream);
104113
final ShopifyErrorResponseException actualShopifyErrorResponseException = new ShopifyErrorResponseException(
105114
response);
106115

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.shopify.mappers;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
11+
12+
import javax.ws.rs.core.Response;
13+
14+
import org.junit.Test;
15+
16+
public class ResponseEntityToStringMapperTest {
17+
18+
@Test
19+
public void givenSomeValuesWhenMappingResponseEntityToAStringThenExpectCorrectValues() throws Exception {
20+
final Response response = mock(Response.class);
21+
22+
final String expectedResponseBodyString = "{\"error\": \"something went wrong.\"}";
23+
final InputStream expectedResponseStream = new ByteArrayInputStream(
24+
expectedResponseBodyString.getBytes(StandardCharsets.UTF_8));
25+
when(response.getEntity()).thenReturn(expectedResponseStream);
26+
final String actualResponseBodyString = ResponseEntityToStringMapper.map(response);
27+
assertEquals(expectedResponseBodyString, actualResponseBodyString);
28+
29+
}
30+
31+
@Test
32+
public void givenSomeExceptionOccursWhenMappingResponseEntityToAStringThenExpectNullValue() throws Exception {
33+
final Response response = mock(Response.class);
34+
35+
when(response.getEntity()).thenThrow(new NullPointerException());
36+
final String actualResponseBodyString = ResponseEntityToStringMapper.map(response);
37+
assertNull(actualResponseBodyString);
38+
39+
}
40+
41+
}

src/test/java/com/shopify/model/ImageAltTextCreationRequestTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
import org.junit.Test;
88

9-
import com.shopify.model.ImageAltTextCreationRequest;
10-
import com.shopify.model.Metafield;
11-
129
public class ImageAltTextCreationRequestTest {
1310

1411
@Test

src/test/java/com/shopify/model/ShopifyFulfillmentCreationRequestTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
import org.junit.Test;
1111

12-
import com.shopify.model.ShopifyFulfillment;
13-
import com.shopify.model.ShopifyFulfillmentCreationRequest;
14-
import com.shopify.model.ShopifyLineItem;
15-
1612
public class ShopifyFulfillmentCreationRequestTest {
1713

1814
private static final List<String> SOME_TRACKING_URLS = Arrays.asList("https://ups.com/123", "https://ups.com/456");

0 commit comments

Comments
 (0)