-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
biagio
committed
Nov 14, 2024
1 parent
29c3475
commit ac5d418
Showing
12 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
type Query { | ||
|
||
# Advanced search with projection | ||
projection(filters: [Filter]): [Book] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package app.tozzi.controller; | ||
|
||
import app.tozzi.datafetcher.BookDataFetcher; | ||
import app.tozzi.model.Book; | ||
import app.tozzi.repository.BookRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.anyMap; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class GraphQLTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private BookDataFetcher bookDataFetcher; | ||
|
||
@MockBean | ||
private BookRepository bookRepository; | ||
|
||
@Test | ||
public void test_ok() throws Exception { | ||
var book = Book.builder() | ||
.isbn("1234567891234567") | ||
.build(); | ||
|
||
when(bookRepository.projection(anyMap(), any(), any())).thenReturn(List.of(Map.of("isbn", "1234567890123456"))); | ||
when(bookDataFetcher.projection(any(), any())).thenReturn(List.of(book)); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.post("/graphql") | ||
.contentType("application/json") | ||
.content("{\"query\":\"query {\\n\\tprojection(\\n\\t\\tfilters: [{ key: \\\"isbn_in\\\", value: \\\"1234567890123456,2234567890123456\\\" }]\\n\\t) {\\n\\t\\tisbn\\n\\t}\\n}\\n\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.data.projection[0].isbn").exists()); | ||
|
||
verify(bookDataFetcher, times(1)).projection(any(), any()); | ||
} | ||
|
||
@Test | ||
public void test_ko() throws Exception { | ||
mockMvc.perform(MockMvcRequestBuilders.post("/graphql") | ||
.contentType("application/json") | ||
.content("{\"query\":\"{\\n projection(filters : [\\n { key: \\\"isbn_in\\\", value: \\\"1234567890123456,2234567890123456\\\" }\\n ]) {\\n isbna\\n }\\n }\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.errors").exists()) | ||
.andExpect(jsonPath("$.data").doesNotExist()); | ||
|
||
verify(bookDataFetcher, times(0)).projection(any(), any()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters