#631 - Partial Response : Add java doc
This commit is contained in:
parent
6d3dce065d
commit
c89bf0eb44
@ -24,7 +24,16 @@
|
|||||||
|
|
||||||
package com.iluwatar.partialresponse;
|
package com.iluwatar.partialresponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map a video to json
|
||||||
|
*/
|
||||||
public class FieldJsonMapper {
|
public class FieldJsonMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param video object containing video information
|
||||||
|
* @param fields fields information to get
|
||||||
|
* @return json of required fields from video
|
||||||
|
*/
|
||||||
public String toJson(Video video, String[] fields) {
|
public String toJson(Video video, String[] fields) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -53,15 +53,18 @@ public class Video {
|
|||||||
this.language = language;
|
this.language = language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return json representaion of video
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "{" +
|
return "{"
|
||||||
"\"id\": \"" + id + "\"," +
|
+ "\"id\": \"" + id + "\","
|
||||||
"\"title\": \"" + title + "\"," +
|
+ "\"title\": \"" + title + "\","
|
||||||
"\"length\": " + length + "," +
|
+ "\"length\": " + length + ","
|
||||||
"\"description\": \"" + description + "\"," +
|
+ "\"description\": \"" + description + "\","
|
||||||
"\"director\": \"" + director + "\"," +
|
+ "\"director\": \"" + director + "\","
|
||||||
"\"language\": \"" + language + "\"," +
|
+ "\"language\": \"" + language + "\","
|
||||||
"}";
|
+ "}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,15 +26,28 @@ package com.iluwatar.partialresponse;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The resource class which serves video information.
|
||||||
|
* This class act as server in the demo. Which has all video details.
|
||||||
|
*/
|
||||||
public class VideoResource {
|
public class VideoResource {
|
||||||
private FieldJsonMapper fieldJsonMapper;
|
private FieldJsonMapper fieldJsonMapper;
|
||||||
private Map<Integer, Video> videos;
|
private Map<Integer, Video> videos;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param fieldJsonMapper map object to json.
|
||||||
|
* @param videos initialize resource with existing videos. Act as database.
|
||||||
|
*/
|
||||||
public VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
|
public VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
|
||||||
this.fieldJsonMapper = fieldJsonMapper;
|
this.fieldJsonMapper = fieldJsonMapper;
|
||||||
this.videos = videos;
|
this.videos = videos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id video id
|
||||||
|
* @param fields fields to get information about
|
||||||
|
* @return json of specified fields of particular video by id
|
||||||
|
*/
|
||||||
public String getDetails(Integer id, String... fields) {
|
public String getDetails(Integer id, String... fields) {
|
||||||
if (fields.length == 0) {
|
if (fields.length == 0) {
|
||||||
return videos.get(id).toString();
|
return videos.get(id).toString();
|
||||||
|
@ -38,6 +38,9 @@ import static org.mockito.Matchers.any;
|
|||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tests {@link VideoResource}.
|
||||||
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class VideoResourceTest {
|
public class VideoResourceTest {
|
||||||
@Mock
|
@Mock
|
||||||
@ -58,7 +61,8 @@ public class VideoResourceTest {
|
|||||||
public void shouldGiveVideoDetailsById() {
|
public void shouldGiveVideoDetailsById() {
|
||||||
String actualDetails = resource.getDetails(1);
|
String actualDetails = resource.getDetails(1);
|
||||||
|
|
||||||
String expectedDetails = "{\"id\": \"1\",\"title\": \"Avatar\",\"length\": 178,\"description\": \"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
|
String expectedDetails = "{\"id\": \"1\",\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||||
|
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
|
||||||
assertEquals(expectedDetails, actualDetails);
|
assertEquals(expectedDetails, actualDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user