2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2019-10-12 20:05:54 +03:00
|
|
|
* The MIT License
|
2021-02-14 15:27:57 +05:30
|
|
|
* Copyright © 2014-2021 Ilkka Seppälä
|
2017-09-14 10:09:02 +05:30
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
2019-10-12 20:05:54 +03:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-09-14 10:09:02 +05:30
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2019-10-12 20:05:54 +03:00
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2017-09-14 10:09:02 +05:30
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2017-09-14 10:09:02 +05:30
|
|
|
package com.iluwatar.partialresponse;
|
|
|
|
|
2021-03-06 04:12:46 -05:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
|
|
|
|
|
import java.util.Map;
|
2020-08-19 23:14:37 +04:00
|
|
|
import org.junit.jupiter.api.Assertions;
|
2021-03-06 04:12:46 -05:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2020-08-19 23:14:37 +04:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
2020-01-16 11:36:36 +05:30
|
|
|
|
2017-09-14 21:50:40 +05:30
|
|
|
/**
|
|
|
|
* tests {@link VideoResource}.
|
|
|
|
*/
|
2020-08-19 23:14:37 +04:00
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
class VideoResourceTest {
|
2017-09-14 19:45:56 +05:30
|
|
|
@Mock
|
2020-08-19 23:14:37 +04:00
|
|
|
private static FieldJsonMapper fieldJsonMapper;
|
2017-09-14 19:45:56 +05:30
|
|
|
|
2020-08-19 23:14:37 +04:00
|
|
|
private static VideoResource resource;
|
2017-09-14 10:09:02 +05:30
|
|
|
|
2021-03-06 04:12:46 -05:00
|
|
|
@BeforeEach
|
|
|
|
void setUp() {
|
2020-01-16 11:36:36 +05:30
|
|
|
var videos = Map.of(
|
|
|
|
1, new Video(1, "Avatar", 178, "epic science fiction film",
|
|
|
|
"James Cameron", "English"),
|
|
|
|
2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|",
|
|
|
|
"Hideaki Anno", "Japanese"),
|
|
|
|
3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi",
|
|
|
|
"Christopher Nolan", "English"));
|
2017-09-14 19:45:56 +05:30
|
|
|
resource = new VideoResource(fieldJsonMapper, videos);
|
2017-09-14 10:09:02 +05:30
|
|
|
}
|
2017-09-14 10:08:56 +05:30
|
|
|
|
2017-09-14 10:09:02 +05:30
|
|
|
@Test
|
2020-08-19 23:14:37 +04:00
|
|
|
void shouldGiveVideoDetailsById() throws Exception {
|
2020-01-16 11:36:36 +05:30
|
|
|
var actualDetails = resource.getDetails(1);
|
2017-09-14 19:27:46 +05:30
|
|
|
|
2020-01-16 11:36:36 +05:30
|
|
|
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
2017-09-14 21:50:40 +05:30
|
|
|
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
|
2020-08-19 23:14:37 +04:00
|
|
|
Assertions.assertEquals(expectedDetails, actualDetails);
|
2017-09-14 19:45:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-08-19 23:14:37 +04:00
|
|
|
void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
|
2020-01-16 11:36:36 +05:30
|
|
|
var fields = new String[]{"id", "title", "length"};
|
2017-09-14 19:45:56 +05:30
|
|
|
|
2020-01-16 11:36:36 +05:30
|
|
|
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}";
|
2021-03-06 04:12:46 -05:00
|
|
|
Mockito.when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
|
2017-09-14 19:45:56 +05:30
|
|
|
|
2020-01-16 11:36:36 +05:30
|
|
|
var actualFieldsDetails = resource.getDetails(2, fields);
|
2017-09-14 19:45:56 +05:30
|
|
|
|
2020-08-19 23:14:37 +04:00
|
|
|
Assertions.assertEquals(expectedDetails, actualFieldsDetails);
|
2017-09-14 10:09:02 +05:30
|
|
|
}
|
2017-09-14 10:08:56 +05:30
|
|
|
}
|