Skip to content

Commit 2dde6d3

Browse files
authored
Merge pull request #388 from josdem/feature/380
[small]Feature/380
2 parents 456b383 + 7a74ffa commit 2dde6d3

9 files changed

+280
-10
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ext {
2424
}
2525

2626
group = 'com.josdem.vetlog'
27-
version = '1.9.3'
27+
version = '1.9.4'
2828

2929
configurations {
3030
compileOnly {

src/main/java/com/josdem/vetlog/VetlogApplication.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 Jose Morales [email protected]
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package com.josdem.vetlog;
218

319
import org.springframework.boot.SpringApplication;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2024 Jose Morales [email protected]
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.josdem.vetlog;
18+
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
21+
import lombok.RequiredArgsConstructor;
22+
import lombok.extern.slf4j.Slf4j;
23+
import org.junit.jupiter.api.DisplayName;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.TestInfo;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.context.ApplicationContext;
29+
30+
@Slf4j
31+
@SpringBootTest
32+
@RequiredArgsConstructor(onConstructor_ = @Autowired)
33+
class VetlogApplicationTest {
34+
35+
private final ApplicationContext applicationContext;
36+
37+
@Test
38+
@DisplayName("it loads the application")
39+
void shouldLoadApplication(TestInfo testInfo) {
40+
log.info("Running {}", testInfo.getDisplayName());
41+
VetlogApplication.main(new String[] {});
42+
assertNotNull(applicationContext, "it should have a context");
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2024 Jose Morales [email protected]
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.josdem.vetlog.controller;
18+
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
22+
23+
import lombok.extern.slf4j.Slf4j;
24+
import org.junit.jupiter.api.DisplayName;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.TestInfo;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.security.test.context.support.WithMockUser;
31+
import org.springframework.test.web.servlet.MockMvc;
32+
33+
@Slf4j
34+
@SpringBootTest
35+
@AutoConfigureMockMvc
36+
class ErrorControllerTest {
37+
38+
@Autowired
39+
private MockMvc mockMvc;
40+
41+
@Test
42+
@DisplayName("showing error page")
43+
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
44+
void shouldShowHomePage(TestInfo testInfo) throws Exception {
45+
log.info("Running: {}", testInfo.getDisplayName());
46+
mockMvc.perform(get("/error")).andExpect(status().isOk()).andExpect(view().name("error"));
47+
}
48+
}

src/test/java/com/josdem/vetlog/controller/PetControllerTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ void shouldGiveForAdoption(TestInfo testInfo) throws Exception {
166166
.andExpect(view().name("pet/giveForAdoption"));
167167
}
168168

169+
@Test
170+
@DisplayName("showing create pet form")
171+
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
172+
void shouldShowCreatePetForm(TestInfo testInfo) throws Exception {
173+
log.info("Running: {}", testInfo.getDisplayName());
174+
mockMvc.perform(get("/pet/create"))
175+
.andExpect(status().isOk())
176+
.andExpect(model().attributeExists("petCommand"))
177+
.andExpect(model().attributeExists("breeds"))
178+
.andExpect(model().attributeExists("breedsByTypeUrl"))
179+
.andExpect(view().name("pet/create"));
180+
}
181+
182+
@Test
183+
@Transactional
184+
@DisplayName("not deleting a pet due to is in adoption")
185+
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
186+
void shouldShowDeletePetForm(TestInfo testInfo) throws Exception {
187+
log.info("Running: {}", testInfo.getDisplayName());
188+
189+
registerPet();
190+
191+
mockMvc.perform(get("/pet/delete").param("uuid", PET_UUID))
192+
.andExpect(status().isOk())
193+
.andExpect(view().name("error"));
194+
}
195+
169196
private void registerPet() throws Exception {
170197

171198
mockMvc.perform(MockMvcRequestBuilders.multipart("/pet/save")

src/test/java/com/josdem/vetlog/controller/PetLogControllerTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ void shouldNotListPetLogs(TestInfo testInfo) throws Exception {
115115
.andExpect(view().name("error"));
116116
}
117117

118+
@Test
119+
@Transactional
120+
@DisplayName("listing pet logs")
121+
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
122+
void shouldListPetLogs(TestInfo testInfo) throws Exception {
123+
log.info("Running: {}", testInfo.getDisplayName());
124+
125+
registerPet();
126+
127+
mockMvc.perform(get("/petlog/list").param("uuid", PET_UUID))
128+
.andExpect(status().isOk())
129+
.andExpect(model().attributeExists("petLogs"))
130+
.andExpect(model().attributeExists("uuid"))
131+
.andExpect(view().name("petlog/list"));
132+
}
133+
118134
private void registerPet() throws Exception {
119135
mockMvc.perform(MockMvcRequestBuilders.multipart("/pet/save")
120136
.file(image)

src/test/java/com/josdem/vetlog/controller/RecoveryControllerTest.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.josdem.vetlog.controller;
1818

19+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
1920
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2022
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2123
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
2224

@@ -37,15 +39,6 @@ class RecoveryControllerTest {
3739
@Autowired
3840
private MockMvc mockMvc;
3941

40-
@Test
41-
@DisplayName("validating token")
42-
void shouldValidateToken(TestInfo testInfo) throws Exception {
43-
log.info("Running: {}", testInfo.getDisplayName());
44-
mockMvc.perform(get("/recovery/activate/token"))
45-
.andExpect(status().isOk())
46-
.andExpect(view().name("error"));
47-
}
48-
4942
@Test
5043
@DisplayName("getting email to change password")
5144
void shouldRequestEmailToChangePassword(TestInfo testInfo) throws Exception {
@@ -63,4 +56,35 @@ void shouldShowChangePasswordForms(TestInfo testInfo) throws Exception {
6356
.andExpect(status().isOk())
6457
.andExpect(view().name("recovery/changePassword"));
6558
}
59+
60+
@Test
61+
@DisplayName("user token not found")
62+
void shouldNotFindUserToken(TestInfo testInfo) throws Exception {
63+
log.info("Running: {}", testInfo.getDisplayName());
64+
mockMvc.perform(get("/recovery/activate/token"))
65+
.andExpect(status().isOk())
66+
.andExpect(view().name("error"));
67+
}
68+
69+
@Test
70+
@DisplayName("not generating token for changing password due to user not found")
71+
void shouldNotGenerateTokenToChangePassword(TestInfo testInfo) throws Exception {
72+
log.info("Running: {}", testInfo.getDisplayName());
73+
mockMvc.perform(post("/recovery/password").with(csrf()).param("email", "[email protected]"))
74+
.andExpect(status().isOk())
75+
.andExpect(view().name("error"));
76+
}
77+
78+
@Test
79+
@DisplayName("not changing password due to token not found")
80+
void shouldNotChangePassword(TestInfo testInfo) throws Exception {
81+
log.info("Running: {}", testInfo.getDisplayName());
82+
mockMvc.perform(post("/recovery/change")
83+
.with(csrf())
84+
.param("token", "18c58288-cb57-46dc-b14f-e3ebc2d9b8ce")
85+
.param("password", "12345678")
86+
.param("passwordConfirmation", "12345678"))
87+
.andExpect(status().isOk())
88+
.andExpect(view().name("error"));
89+
}
6690
}

src/test/java/com/josdem/vetlog/controller/VetControllerTest.java

+60
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,32 @@
1616

1717
package com.josdem.vetlog.controller;
1818

19+
import static com.josdem.vetlog.controller.PetControllerTest.PET_UUID;
20+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
21+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
1922
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2024
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
2125
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2226
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
2327

28+
import com.josdem.vetlog.enums.PetStatus;
29+
import com.josdem.vetlog.enums.PetType;
30+
import jakarta.transaction.Transactional;
2431
import lombok.extern.slf4j.Slf4j;
32+
import org.junit.jupiter.api.BeforeEach;
2533
import org.junit.jupiter.api.DisplayName;
2634
import org.junit.jupiter.api.Test;
2735
import org.junit.jupiter.api.TestInfo;
2836
import org.springframework.beans.factory.annotation.Autowired;
2937
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
3038
import org.springframework.boot.test.context.SpringBootTest;
39+
import org.springframework.mock.web.MockMultipartFile;
3140
import org.springframework.security.test.context.support.WithMockUser;
3241
import org.springframework.test.web.servlet.MockMvc;
42+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
43+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
44+
import org.springframework.web.context.WebApplicationContext;
3345

3446
@Slf4j
3547
@SpringBootTest
@@ -39,6 +51,19 @@ class VetControllerTest {
3951
@Autowired
4052
private MockMvc mockMvc;
4153

54+
@Autowired
55+
private WebApplicationContext webApplicationContext;
56+
57+
private final MockMultipartFile image =
58+
new MockMultipartFile("mockImage", "image.jpg", "image/jpeg", "image".getBytes());
59+
60+
@BeforeEach
61+
public void setUp() {
62+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
63+
.apply(springSecurity())
64+
.build();
65+
}
66+
4267
@Test
4368
@DisplayName("showing create vet form")
4469
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
@@ -49,4 +74,39 @@ void shouldShowCreateVetForm(TestInfo testInfo) throws Exception {
4974
.andExpect(model().attributeExists("usernameCommand"))
5075
.andExpect(view().name("vet/form"));
5176
}
77+
78+
@Test
79+
@Transactional
80+
@DisplayName("searching by user")
81+
@WithMockUser(username = "admin", password = "12345678", roles = "ADMIN")
82+
void shouldSearchPetsByUser(TestInfo testInfo) throws Exception {
83+
log.info("Running: {}", testInfo.getDisplayName());
84+
85+
registerPet();
86+
87+
mockMvc.perform(post("/vet/search").with(csrf()).param("username", "josdem"))
88+
.andExpect(status().isOk())
89+
.andExpect(model().attributeExists("pets"))
90+
.andExpect(model().attributeExists("defaultImage"))
91+
.andExpect(view().name("vet/list"));
92+
}
93+
94+
private void registerPet() throws Exception {
95+
96+
mockMvc.perform(MockMvcRequestBuilders.multipart("/pet/save")
97+
.file(image)
98+
.with(csrf())
99+
.param("name", "Cremita")
100+
.param("uuid", PET_UUID)
101+
.param("birthDate", "2024-08-22T09:28:00")
102+
.param("dewormed", "true")
103+
.param("vaccinated", "true")
104+
.param("sterilized", "true")
105+
.param("breed", "11")
106+
.param("user", "1")
107+
.param("status", PetStatus.OWNED.toString())
108+
.param("type", PetType.DOG.toString()))
109+
.andExpect(status().isOk())
110+
.andExpect(view().name("pet/create"));
111+
}
52112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2024 Jose Morales [email protected]
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.josdem.vetlog.enums;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
class PetStatusTest {
25+
26+
private final PetStatus petStatus = PetStatus.OWNED;
27+
28+
@Test
29+
@DisplayName("returning pet status")
30+
void shouldReturnPetStatus() {
31+
assertEquals("OWNED", petStatus.name());
32+
assertEquals("Owned", petStatus.getValue());
33+
assertEquals(PetStatus.OWNED, PetStatus.getPetStatusByValue("Owned"));
34+
}
35+
}

0 commit comments

Comments
 (0)