Skip to content

Commit 095cf1c

Browse files
authored
Merge pull request #3 from ZaharKalosha/feature/create-tests
Problem with tests
2 parents 050cb9a + 3cd1219 commit 095cf1c

File tree

3 files changed

+229
-25
lines changed

3 files changed

+229
-25
lines changed

pom.xml

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,38 @@
2424
<java.version>11</java.version>
2525
</properties>
2626

27-
<dependencies>
28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-data-jpa</artifactId>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-starter-web</artifactId>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter-validation</artifactId>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.postgresql</groupId>
42-
<artifactId>postgresql</artifactId>
43-
<scope>runtime</scope>
44-
</dependency>
45-
<dependency>
46-
<groupId>org.springframework.boot</groupId>
47-
<artifactId>spring-boot-starter-test</artifactId>
48-
<scope>test</scope>
49-
</dependency>
50-
</dependencies>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-validation</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
47+
<dependency>
48+
<groupId>org.postgresql</groupId>
49+
<artifactId>postgresql</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.h2database</groupId>
54+
<artifactId>h2</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
57+
58+
</dependencies>
5159

5260
<build>
5361
<plugins>
@@ -58,5 +66,4 @@
5866
</plugins>
5967
</build>
6068

61-
6269
</project>
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package com.example.postgresdemo.controller;
2+
3+
import com.example.postgresdemo.model.Question;
4+
import com.example.postgresdemo.repository.QuestionRepository;
5+
6+
import org.hamcrest.Matchers;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.http.MediaType;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
18+
import java.nio.CharBuffer;
19+
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
23+
24+
@SpringBootTest
25+
26+
@AutoConfigureMockMvc
27+
public class QuestionControllerTest {
28+
@Autowired
29+
private QuestionRepository questionRepository;
30+
31+
@Autowired
32+
private MockMvc mockMvc;
33+
34+
@AfterEach
35+
void deleteQuestions() {
36+
questionRepository.deleteAll();
37+
}
38+
39+
@Test
40+
void testGetQuestionsWithAmountLessThanPageSize() throws Exception {
41+
int assertionNumber = 10;
42+
int pageSize = 20;
43+
44+
fillQuestions(assertionNumber);
45+
46+
mockMvc.perform(get("/questions")
47+
.contentType(MediaType.APPLICATION_JSON))
48+
.andExpect(status().isOk())
49+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
50+
.andExpect(MockMvcResultMatchers.jsonPath("$.content.length()", Matchers.equalTo(assertionNumber)))
51+
.andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber)))
52+
.andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo(1)));
53+
}
54+
55+
@Test
56+
void testGetQuestionsWithAmountMoreThanPageSize() throws Exception {
57+
int assertionNumber = 30;
58+
int pageSize = 20;
59+
int totalPages = (int) Math.ceil(assertionNumber / (double) pageSize);
60+
61+
fillQuestions(assertionNumber);
62+
63+
mockMvc.perform(get("/questions")
64+
.contentType(MediaType.APPLICATION_JSON))
65+
.andExpect(status().isOk())
66+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
67+
.andExpect(MockMvcResultMatchers.jsonPath("$.content.length()", Matchers.equalTo(pageSize)))
68+
.andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber)))
69+
.andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo(totalPages)));
70+
}
71+
72+
@Test
73+
void testCreateCorrectQuestion() throws Exception {
74+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
75+
.contentType(MediaType.APPLICATION_JSON)
76+
.content("{\n" +
77+
" \"title\": \"Question 1\",\n" +
78+
" \"description\": \"Description 1\"\n" +
79+
"}"))
80+
.andExpect(status().isOk())
81+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
82+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1")))
83+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Description 1")));
84+
}
85+
86+
@Test
87+
void testCreateQuestionWithoutTitle() throws Exception {
88+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
89+
.contentType(MediaType.APPLICATION_JSON)
90+
.content("{\n" +
91+
" \"description\": \"Description\"\n" +
92+
"}"))
93+
.andExpect(status().is4xxClientError());
94+
}
95+
96+
@Test
97+
void testCreateQuestionWithTitleLesThenThreeChars() throws Exception {
98+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
99+
.contentType(MediaType.APPLICATION_JSON)
100+
.content("{\n" +
101+
" \"title\": \"Te\",\n" +
102+
" \"description\": \"Description\"\n" +
103+
"}"))
104+
.andExpect(status().is4xxClientError());
105+
}
106+
107+
@Test
108+
void testCreateQuestionWithTitleMoreThenHundredChars() throws Exception {
109+
int numberOfChars = 101;
110+
String title = CharBuffer.allocate(numberOfChars).toString().replace('\0', 'T');
111+
112+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
113+
.contentType(MediaType.APPLICATION_JSON)
114+
.content("{\n" +
115+
" \"title\": \"" + title + "\",\n" +
116+
" \"description\": \"Description\"\n" +
117+
"}"))
118+
.andExpect(status().is4xxClientError());
119+
}
120+
121+
@Test
122+
void testCreateQuestionWithoutDescription() throws Exception {
123+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
124+
.contentType(MediaType.APPLICATION_JSON)
125+
.content("{\n" +
126+
" \"title\": \"Question 1\"\n" +
127+
"}"))
128+
.andExpect(status().isOk())
129+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
130+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1")))
131+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo(null)));
132+
}
133+
134+
@Test
135+
void testUpdateQuestion() throws Exception {
136+
fillQuestions(1);
137+
long questionId = questionRepository.findAll().get(0).getId();
138+
139+
mockMvc.perform(MockMvcRequestBuilders.put("/questions/" + questionId)
140+
.contentType(MediaType.APPLICATION_JSON)
141+
.content("{\n" +
142+
" \"title\": \"Edited Question 1\",\n" +
143+
" \"description\": \"Edited Description 1\"\n" +
144+
"}"))
145+
.andExpect(status().isOk())
146+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
147+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Edited Question 1")))
148+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Edited Description 1")));
149+
}
150+
151+
@Test
152+
void testUpdateQuestionWithNonExistingId() throws Exception {
153+
fillQuestions(1);
154+
long questionId = questionRepository.findAll().get(0).getId();
155+
156+
mockMvc.perform(MockMvcRequestBuilders.put("/questions/" + (questionId + 1))
157+
.contentType(MediaType.APPLICATION_JSON)
158+
.content("{\n" +
159+
" \"title\": \"Edited Question 1\",\n" +
160+
" \"description\": \"Edited Description 1\"\n" +
161+
"}"))
162+
.andExpect(status().is4xxClientError());
163+
}
164+
165+
@Test
166+
void testDeleteQuestion() throws Exception {
167+
fillQuestions(1);
168+
long questionId = questionRepository.findAll().get(0).getId();
169+
170+
mockMvc.perform(MockMvcRequestBuilders.delete("/questions/" + questionId)
171+
.contentType(MediaType.APPLICATION_JSON))
172+
.andExpect(status().isOk());
173+
}
174+
175+
private void fillQuestions(Integer number) {
176+
for (int i = 0; i < number; i++) {
177+
Question question = new Question();
178+
question.setTitle("Question " + i);
179+
question.setDescription("Description " + i);
180+
questionRepository.save(question);
181+
}
182+
}
183+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spring.datasource.url=jdbc:h2:mem:test;MODE=PostgreSQL;
2+
spring.datasource.driver-class-name=org.h2.Driver
3+
spring.datasource.username=${DB_USER}
4+
spring.datasource.password=${DB_PASSWORD}
5+
# We add the MySQL Dialect so that it understands and generates the query based on MySQL
6+
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
7+
8+
spring.h2.console.enabled=true
9+
spring.jpa.hibernate.ddl-auto=update
10+
spring.jpa.properties.hibernate.format_sql=true
11+
#spring.jpa.properties.hibernate.show_sql=true
12+
13+
14+
spring.sql.init.mode=always

0 commit comments

Comments
 (0)