-
Notifications
You must be signed in to change notification settings - Fork 1
/
SummonerSpellsRestControllerTest.java
100 lines (80 loc) · 3.7 KB
/
SummonerSpellsRestControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.drumonii.loltrollbuild.api;
import com.drumonii.loltrollbuild.model.SummonerSpell;
import com.drumonii.loltrollbuild.repository.SummonerSpellsRepository;
import com.drumonii.loltrollbuild.riot.api.SummonerSpellsResponse;
import com.drumonii.loltrollbuild.test.api.WebMvcRestTest;
import com.drumonii.loltrollbuild.util.RandomizeUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.stream.Collectors;
import static com.drumonii.loltrollbuild.model.SummonerSpell.GameMode.CLASSIC;
import static java.util.function.Predicate.not;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcRestTest(SummonerSpellsRestController.class)
abstract class SummonerSpellsRestControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
protected SummonerSpellsRepository summonerSpellsRepository;
@Autowired
protected ObjectMapper objectMapper;
@Value("${api.base-path}")
private String apiPath;
protected SummonerSpellsResponse summonerSpellsResponse;
protected abstract void beforeEach();
@Test
void getSummonerSpells() throws Exception {
SummonerSpell summonerSpell = RandomizeUtil.getRandom(summonerSpellsResponse.getSummonerSpells().values().stream()
.filter(not(spell -> spell.getModes().isEmpty()))
.collect(Collectors.toSet()));
// qbe
mockMvc.perform(get("{apiPath}/summoner-spells", apiPath))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*]").isNotEmpty());
// qbe with name
mockMvc.perform(get("{apiPath}/summoner-spells", apiPath)
.param("name", summonerSpell.getName().toLowerCase()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$..name", everyItem(is(summonerSpell.getName()))));
// qbe with modes
mockMvc.perform(get("{apiPath}/summoner-spells", apiPath)
.param("modes", summonerSpell.getModes().iterator().next().name()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$..modes", everyItem(hasItem(summonerSpell.getModes().iterator().next().name()))));
// qbe with no results
mockMvc.perform(get("{apiPath}/summoner-spells", apiPath)
.param("name", "abcd1234"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(content().json("[]"));
}
@Test
void getSummonerSpell() throws Exception {
// find with non existing summoner spell Id
mockMvc.perform(get("{apiPath}/summoner-spells/{id}", apiPath, 0))
.andExpect(status().isNotFound());
SummonerSpell snowball = summonerSpellsResponse.getSummonerSpells().get("SummonerSnowball");
// find with existing summoner spell Id
mockMvc.perform(get("{apiPath}/summoner-spells/{id}", apiPath, snowball.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*]").isNotEmpty());
}
@Test
void getForTrollBuild() throws Exception {
mockMvc.perform(get("{apiPath}/summoner-spells/for-troll-build", apiPath)
.param("mode", CLASSIC.name()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*]").isNotEmpty());
}
}