import com.netflix.graphql.dgs.DgsQueryExecutor
import com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest(classes = [DgsAutoConfiguration::class, ShowsDataFetcher::class])
class ShowsDataFetcherTest {
@Autowired
lateinit var dgsQueryExecutor: DgsQueryExecutor
@Test
fun shows() {
val titles : List<String> = dgsQueryExecutor.executeAndExtractJsonPath("""
{
shows {
title
releaseYear
}
}
""".trimIndent(), "data.shows[*].title")
assertThat(titles).contains("Ozark")
}
}
@Test
public void showsWithQueryApi() {
GraphQLQueryRequest graphQLQueryRequest = new GraphQLQueryRequest(
new ShowsGraphQLQuery.Builder().titleFilter("Oz").build(),
new ShowsProjectionRoot().title()
);
List<String> titles = dgsQueryExecutor.executeAndExtractJsonPath(graphQLQueryRequest.serialize(), "data.shows[*].title");
assertThat(titles).containsExactly("Ozark");
}
@Test
fun showsWithQueryApi() {
val graphQLQueryRequest = GraphQLQueryRequest(
ShowsGraphQLQuery.Builder()
.titleFilter("Oz")
.build(),
ShowsProjectionRoot().title())
val titles = dgsQueryExecutor.executeAndExtractJsonPath<List<String>>(graphQLQueryRequest.serialize(), "data.shows[*].title")
assertThat(titles).containsExactly("Ozark")
}
public interface ShowsService {
List<Show> shows();
}
@Service
public class ShowsServiceImpl implements ShowsService {
@Override
public List<Show> shows() {
return List.of(
new Show("Stranger Things", 2016),
new Show("Ozark", 2017),
new Show("The Crown", 2016),
new Show("Dead to Me", 2019),
new Show("Orange is the New Black", 2013)
);
}
}
interface ShowsService {
fun shows(): List<ShowsDataFetcher.Show>
}
@Service
class BasicShowsService : ShowsService {
override fun shows(): List<ShowsDataFetcher.Show> {
return listOf(
ShowsDataFetcher.Show("Stranger Things", 2016),
ShowsDataFetcher.Show("Ozark", 2017),
ShowsDataFetcher.Show("The Crown", 2016),
ShowsDataFetcher.Show("Dead to Me", 2019),
ShowsDataFetcher.Show("Orange is the New Black", 2013)
)
}
}
@DgsComponent
class ShowsDataFetcher {
@Autowired
lateinit var showsService: ShowsService
@DgsData(parentType = "Query", field = "shows")
fun shows(@InputArgument("titleFilter") titleFilter: String?): List<Show> {
return if (titleFilter != null) {
showsService.shows().filter { it.title.contains(titleFilter) }
} else {
showsService.shows()
}
}
}
@SpringBootTest(classes = {DgsAutoConfiguration.class, ShowsDataFetcher.class})
public class ShowsDataFetcherTests {
@Autowired
DgsQueryExecutor dgsQueryExecutor;
@MockBean
ShowsService showsService;
@BeforeEach
public void before() {
Mockito.when(showsService.shows()).thenAnswer(invocation -> List.of(new Show("mock title", 2020)));
}
@Test
public void showsWithQueryApi() {
GraphQLQueryRequest graphQLQueryRequest = new GraphQLQueryRequest(
new ShowsGraphQLQuery.Builder().build(),
new ShowsProjectionRoot().title()
);
List<String> titles = dgsQueryExecutor.executeAndExtractJsonPath(graphQLQueryRequest.serialize(), "data.shows[*].title");
assertThat(titles).containsExactly("mock title");
}
}
@SpringBootTest(classes = [DgsAutoConfiguration::class, ShowsDataFetcher::class])
class ShowsDataFetcherTest {
@Autowired
lateinit var
dgsQueryExecutor:DgsQueryExecutor
@MockBean
lateinit var
showsService:ShowsService
@BeforeEach
fun before() {
Mockito.`when`(showsService.shows()).thenAnswer {
listOf(ShowsDataFetcher.Show("mock title", 2020))
}
}
@Test
fun shows() {
val titles :List<String> =dgsQueryExecutor.executeAndExtractJsonPath("""
{
shows {
title
releaseYear
}
}
""".trimIndent(), "data.shows[*].title")
assertThat(titles).contains("mock title")
}
}
@Test
void showsWithException() {
Mockito.when(showsService.shows()).thenThrow(new RuntimeException("nothing to see here"));
ExecutionResult result = dgsQueryExecutor.execute(" { shows { title releaseYear }}");
assertThat(result.getErrors()).isNotEmpty();
assertThat(result.getErrors().get(0).getMessage()).isEqualTo("java.lang.RuntimeException: nothing to see here");
}
@Test
fun showsWithException() {
Mockito.`when`(showsService.shows()).thenThrow(RuntimeException("nothing to see here"))
val result = dgsQueryExecutor.execute("""
{
shows {
title
releaseYear
}
}
""".trimIndent())
assertThat(result.errors).isNotEmpty
assertThat(result.errors[0].message).isEqualTo("java.lang.RuntimeException: nothing to see here")
}