-
Notifications
You must be signed in to change notification settings - Fork 5
Schema integration test #1 #37
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lv.ctco.tpl.bff.graphql; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.val; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
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.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@RunWith(SpringRunner.class) | ||
@AutoConfigureMockMvc | ||
public class GraphQLIntegrationTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually an e2e test, please rename accordingly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can I ensure that e2e tests are not executed in the same test run as unit and integration tests? |
||
|
||
private static final String GRAPHQL_PATH = "/graphql"; | ||
|
||
private static final String QUERY = "{jokes {jokeById(id: \"77\"){id,text}}}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please extract to a |
||
|
||
private static final String JOKE_ID = "77"; | ||
|
||
private static final String JOKE_TEXT = "Chuck Norris can divide by zero."; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By going through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please see this comment for a visual representation re e2e testing scope |
||
|
||
@Test | ||
@SneakyThrows | ||
public void testGetJokeById() { | ||
val result = graphQLPost(); | ||
|
||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.errors").doesNotExist()) | ||
.andExpect(jsonPath("$.data.jokes.jokeById.id").value(JOKE_ID)) | ||
.andExpect(jsonPath("$.data.jokes.jokeById.text").value(JOKE_TEXT)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case of an error, |
||
} | ||
|
||
@SneakyThrows | ||
private ResultActions graphQLPost() { | ||
return mockMvc.perform(post(GRAPHQL_PATH).content(request())); | ||
} | ||
|
||
@SneakyThrows | ||
private String request() { | ||
val jsonQuery = new JSONObject(); | ||
jsonQuery.put("query", QUERY); | ||
return jsonQuery.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package lv.ctco.tpl.bff.graphql; | ||
|
||
import com.netflix.hystrix.HystrixCommand; | ||
import com.netflix.hystrix.HystrixCommandGroupKey; | ||
import lombok.SneakyThrows; | ||
import lombok.val; | ||
import lv.ctco.tpl.bff.configuration.AppProperties; | ||
import lv.ctco.tpl.bff.connectors.icndb.ICNDB; | ||
import lv.ctco.tpl.bff.connectors.icndb.ICNDBJoke; | ||
import lv.ctco.tpl.bff.connectors.icndb.ICNDBJokeEnvelope; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
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.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@RunWith(SpringRunner.class) | ||
@AutoConfigureMockMvc | ||
public class GraphQLQueryTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually an integration test, please rename accordingly |
||
|
||
private static final String GRAPHQL_PATH = "/graphql"; | ||
|
||
private static final String QUERY = "{jokes {jokeById(id: \"77\"){id,text}}}"; | ||
|
||
private static final String JOKE_ID = "77"; | ||
|
||
private static final String JOKE_TEXT = "Chuck Norris can divide by zero."; | ||
|
||
@Autowired | ||
private AppProperties appProperties; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not auto wire required app properties directly? i.e. @Value("icndb.url") private String icndbUrl; after all we only need a single value from the props, let's be straight about it |
||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Mock | ||
private ICNDB icndb; | ||
|
||
@Test | ||
@SneakyThrows | ||
public void testGetJokeById() { | ||
doReturn(mockResponse()).when(icndb).getJokeById(JOKE_ID); | ||
|
||
val result = graphQLPost(); | ||
|
||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.errors").doesNotExist()) | ||
.andExpect(jsonPath("$.data.jokes.jokeById.id").value(JOKE_ID)) | ||
.andExpect(jsonPath("$.data.jokes.jokeById.text").value(JOKE_TEXT)); | ||
} | ||
|
||
@SneakyThrows | ||
private ResultActions graphQLPost() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the fact that you actually do a HTTP |
||
return mockMvc.perform(post(GRAPHQL_PATH).content(request())); | ||
} | ||
|
||
@SneakyThrows | ||
private String request() { | ||
val jsonQuery = new JSONObject(); | ||
jsonQuery.put("query", QUERY); | ||
return jsonQuery.toString(); | ||
} | ||
|
||
private HystrixCommand<ICNDBJokeEnvelope> mockResponse() { | ||
return new HystrixCommand<ICNDBJokeEnvelope>(HystrixCommandGroupKey.Factory.asKey(appProperties.getIcndbUrl())) { | ||
@Override | ||
protected ICNDBJokeEnvelope run() throws Exception { | ||
ICNDBJokeEnvelope icndbJokeEnvelope = new ICNDBJokeEnvelope(); | ||
icndbJokeEnvelope.setType("success"); | ||
ICNDBJoke value = new ICNDBJoke(); | ||
value.setId(JOKE_ID); | ||
value.setJoke(JOKE_TEXT); | ||
icndbJokeEnvelope.setValue(value); | ||
return icndbJokeEnvelope; | ||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package lv.ctco.tpl.bff.graphql; | ||
|
||
import graphql.servlet.GraphQLServlet; | ||
import lombok.SneakyThrows; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Controller | ||
public class GraphQLTestController { | ||
|
||
@Autowired | ||
private GraphQLServlet graphQLServlet; | ||
|
||
@RequestMapping(value = "${graphql.servlet.mapping:/graphql}") | ||
@SneakyThrows | ||
public void service(HttpServletRequest request, HttpServletResponse response) { | ||
graphQLServlet.service(request, response); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
app: | ||
icndb-url: https://example.com | ||
icndb-url: https://api.icndb.com | ||
|
||
logging: | ||
level: | ||
feign: DEBUG | ||
graphql: DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Getter
and@Setter
can be folded in to@Data