Skip to content

Commit 24b4586

Browse files
authored
fix: Video.toString() outputs an illegal json string (#3032)
refactor: json string concat add an extra line at EOF
1 parent 8bd3175 commit 24b4586

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

partial-response/src/main/java/com/iluwatar/partialresponse/FieldJsonMapper.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.iluwatar.partialresponse;
2626

2727
import java.lang.reflect.Field;
28+
import java.util.StringJoiner;
2829

2930
/**
3031
* Map a video to json.
@@ -39,18 +40,15 @@ public class FieldJsonMapper {
3940
* @return json of required fields from video
4041
*/
4142
public String toJson(Video video, String[] fields) throws Exception {
42-
var json = new StringBuilder().append("{");
43+
var json = new StringJoiner(",", "{", "}");
4344

4445
var i = 0;
4546
var fieldsLength = fields.length;
4647
while (i < fieldsLength) {
47-
json.append(getString(video, Video.class.getDeclaredField(fields[i])));
48-
if (i != fieldsLength - 1) {
49-
json.append(",");
50-
}
48+
json.add(getString(video, Video.class.getDeclaredField(fields[i])));
5149
i++;
5250
}
53-
json.append("}");
51+
5452
return json.toString();
5553
}
5654

partial-response/src/main/java/com/iluwatar/partialresponse/Video.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String toString() {
4343
+ "\"length\": " + length + ","
4444
+ "\"description\": \"" + description + "\","
4545
+ "\"director\": \"" + director + "\","
46-
+ "\"language\": \"" + language + "\","
46+
+ "\"language\": \"" + language + "\""
4747
+ "}";
4848
}
4949
}

partial-response/src/test/java/com/iluwatar/partialresponse/VideoResourceTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void shouldGiveVideoDetailsById() throws Exception {
6363
var actualDetails = resource.getDetails(1);
6464

6565
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
66-
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
66+
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
6767
Assertions.assertEquals(expectedDetails, actualDetails);
6868
}
6969

@@ -78,4 +78,17 @@ void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
7878

7979
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
8080
}
81-
}
81+
82+
@Test
83+
void shouldAllSpecifiedFieldsInformationOfVideo() throws Exception {
84+
var fields = new String[]{"id", "title", "length", "description", "director", "language"};
85+
86+
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
87+
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
88+
Mockito.when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
89+
90+
var actualFieldsDetails = resource.getDetails(1, fields);
91+
92+
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
93+
}
94+
}

0 commit comments

Comments
 (0)