Skip to content

Commit 279ee37

Browse files
authored
Merge pull request #566 from ashwanikumar04/dev
Issue#565: fixed the iterator issue
2 parents 08abb6b + 9ed8de3 commit 279ee37

File tree

5 files changed

+66
-13
lines changed

5 files changed

+66
-13
lines changed

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,12 @@ private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHe
555555
public static String streamToString(final InputStream input) {
556556
final String httpStreamEncoding = "UTF-8";
557557
final String endOfFile = "\\A";
558-
final Scanner scanner = new Scanner(input, httpStreamEncoding);
559558
String scannerString = "";
560-
try {
559+
try (final Scanner scanner = new Scanner(input, httpStreamEncoding)) {
561560
scanner.useDelimiter(endOfFile);
562-
scannerString = scanner.next();
563-
} finally {
564-
scanner.close();
561+
if (scanner.hasNext()) {
562+
scannerString = scanner.next();
563+
}
565564
}
566565
return scannerString;
567566
}

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,12 @@ void setConnectionFactory(final IConnectionFactory factory) {
470470
public static String streamToString(final InputStream input) {
471471
final String httpStreamEncoding = "UTF-8";
472472
final String endOfFile = "\\A";
473-
final Scanner scanner = new Scanner(input, httpStreamEncoding);
474473
String scannerString = "";
475-
try {
476-
scanner.useDelimiter(endOfFile);
477-
scannerString = scanner.next();
478-
} finally {
479-
scanner.close();
474+
try (final Scanner scanner = new Scanner(input, httpStreamEncoding)) {
475+
scanner.useDelimiter(endOfFile);
476+
if (scanner.hasNext()) {
477+
scannerString = scanner.next();
478+
}
480479
}
481480
return scannerString;
482481
}

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
366366
final String responseMessage = connection.getResponseMessage();
367367
String rawOutput = "{}";
368368
if(connection.getInputStream() != null) {
369-
rawOutput = DefaultHttpProvider.streamToString(connection.getInputStream());
369+
rawOutput = CoreHttpProvider.streamToString(connection.getInputStream());
370370
}
371371
GraphErrorResponse error;
372372
try {

src/test/java/com/microsoft/graph/http/CoreHttpProviderTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
import static org.junit.Assert.assertTrue;
66
import static org.junit.Assert.fail;
77

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

15+
import com.google.common.collect.ImmutableMap;
16+
import com.google.common.collect.Maps;
17+
import com.google.gson.Gson;
18+
import com.google.gson.GsonBuilder;
1219
import org.junit.Ignore;
1320
import org.junit.Test;
1421

@@ -28,6 +35,7 @@ public class CoreHttpProviderTests {
2835

2936
private MockAuthenticationProvider mAuthenticationProvider;
3037
private CoreHttpProvider mProvider;
38+
private Gson GSON = new GsonBuilder().create();
3139

3240
@Test
3341
public void testErrorResponse() throws Exception {
@@ -96,7 +104,27 @@ public void testHasHeaderReturnsFalse() {
96104
HeaderOption h = new HeaderOption("name", "value");
97105
assertFalse(DefaultHttpProvider.hasHeader(Arrays.asList(h), "blah"));
98106
}
99-
107+
108+
@Test
109+
public void testStreamToStringReturnsData() {
110+
String data = GSON.toJson(Maps.newHashMap(
111+
ImmutableMap.<String, String>builder()
112+
.put("key", "value")
113+
.build()));
114+
final InputStream inputStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
115+
116+
String convertedData = CoreHttpProvider.streamToString(inputStream);
117+
assertEquals(data, convertedData);
118+
}
119+
120+
@Test
121+
public void testStreamToStringReturnsEmpty() {
122+
final InputStream inputStream = new ByteArrayInputStream(new byte[0]);
123+
124+
String convertedData = CoreHttpProvider.streamToString(inputStream);
125+
assertEquals("", convertedData);
126+
}
127+
100128

101129
/**
102130
* Configures the http provider for test cases

src/test/java/com/microsoft/graph/http/DefaultHttpProviderTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@
2828
import static org.junit.Assert.assertTrue;
2929
import static org.junit.Assert.fail;
3030

31+
import java.io.ByteArrayInputStream;
3132
import java.io.InputStream;
33+
import java.nio.charset.StandardCharsets;
3234
import java.util.Arrays;
3335
import java.util.HashMap;
3436
import java.util.Map;
3537
import java.util.concurrent.atomic.AtomicBoolean;
3638
import java.util.concurrent.atomic.AtomicInteger;
3739

40+
import com.google.common.collect.ImmutableMap;
41+
import com.google.common.collect.Maps;
42+
import com.google.gson.Gson;
43+
import com.google.gson.GsonBuilder;
3844
import org.junit.Test;
3945

4046
import com.google.gson.JsonObject;
@@ -58,6 +64,7 @@ public class DefaultHttpProviderTests {
5864

5965
private MockAuthenticationProvider mAuthenticationProvider;
6066
private DefaultHttpProvider mProvider;
67+
private Gson GSON = new GsonBuilder().create();
6168

6269
@Test
6370
public void testNoContentType() throws Exception {
@@ -369,6 +376,26 @@ public void testHasHeaderReturnsFalse() {
369376
HeaderOption h = new HeaderOption("name", "value");
370377
assertFalse(DefaultHttpProvider.hasHeader(Arrays.asList(h), "blah"));
371378
}
379+
380+
@Test
381+
public void testStreamToStringReturnsData() {
382+
String data = GSON.toJson(Maps.newHashMap(
383+
ImmutableMap.<String, String>builder()
384+
.put("key", "value")
385+
.build()));
386+
final InputStream inputStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
387+
388+
String convertedData = DefaultHttpProvider.streamToString(inputStream);
389+
assertEquals(data, convertedData);
390+
}
391+
392+
@Test
393+
public void testStreamToStringReturnsEmpty() {
394+
final InputStream inputStream = new ByteArrayInputStream(new byte[0]);
395+
396+
String convertedData = DefaultHttpProvider.streamToString(inputStream);
397+
assertEquals("", convertedData);
398+
}
372399

373400

374401
/**

0 commit comments

Comments
 (0)