mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 16:14:58 +00:00
fix: Video.toString() outputs an illegal json string (#3032)
refactor: json string concat add an extra line at EOF
This commit is contained in:
3 files changed
+19
-8
No files matched your search
@@ -25,6 +25,7 @@
|
|||||||
package com.iluwatar.partialresponse;
|
package com.iluwatar.partialresponse;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a video to json.
|
* Map a video to json.
|
||||||
@@ -39,18 +40,15 @@ public class FieldJsonMapper {
|
|||||||
* @return json of required fields from video
|
* @return json of required fields from video
|
||||||
*/
|
*/
|
||||||
public String toJson(Video video, String[] fields) throws Exception {
|
public String toJson(Video video, String[] fields) throws Exception {
|
||||||
var json = new StringBuilder().append("{");
|
var json = new StringJoiner(",", "{", "}");
|
||||||
|
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var fieldsLength = fields.length;
|
var fieldsLength = fields.length;
|
||||||
while (i < fieldsLength) {
|
while (i < fieldsLength) {
|
||||||
json.append(getString(video, Video.class.getDeclaredField(fields[i])));
|
json.add(getString(video, Video.class.getDeclaredField(fields[i])));
|
||||||
if (i != fieldsLength - 1) {
|
|
||||||
json.append(",");
|
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
json.append("}");
|
|
||||||
return json.toString();
|
return json.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public record Video(Integer id, String title, Integer length, String description
|
|||||||
+ "\"length\": " + length + ","
|
+ "\"length\": " + length + ","
|
||||||
+ "\"description\": \"" + description + "\","
|
+ "\"description\": \"" + description + "\","
|
||||||
+ "\"director\": \"" + director + "\","
|
+ "\"director\": \"" + director + "\","
|
||||||
+ "\"language\": \"" + language + "\","
|
+ "\"language\": \"" + language + "\""
|
||||||
+ "}";
|
+ "}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ class VideoResourceTest {
|
|||||||
var actualDetails = resource.getDetails(1);
|
var actualDetails = resource.getDetails(1);
|
||||||
|
|
||||||
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||||
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
|
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||||
Assertions.assertEquals(expectedDetails, actualDetails);
|
Assertions.assertEquals(expectedDetails, actualDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,4 +78,17 @@ class VideoResourceTest {
|
|||||||
|
|
||||||
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
|
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldAllSpecifiedFieldsInformationOfVideo() throws Exception {
|
||||||
|
var fields = new String[]{"id", "title", "length", "description", "director", "language"};
|
||||||
|
|
||||||
|
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||||
|
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||||
|
Mockito.when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
|
||||||
|
|
||||||
|
var actualFieldsDetails = resource.getDetails(1, fields);
|
||||||
|
|
||||||
|
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user