Skip to content

Commit 08fdada

Browse files
authored
Merge pull request #447 from bounswe/test/397-profile
Test/397 profile
2 parents 051a34d + 8b78be9 commit 08fdada

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Backend Tests
2+
3+
on:
4+
push:
5+
paths:
6+
- "apps/jobboard-backend/**"
7+
- ".github/workflows/backend-tests.yml"
8+
9+
jobs:
10+
test:
11+
name: Run Backend Tests
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up JDK 21
19+
uses: actions/setup-java@v4
20+
with:
21+
java-version: '21'
22+
distribution: 'temurin'
23+
cache: maven
24+
25+
- name: Run tests
26+
working-directory: ./apps/jobboard-backend
27+
env:
28+
SPRING_DATASOURCE_URL: jdbc:h2:mem:testdb
29+
SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.h2.Driver
30+
SPRING_DATASOURCE_USERNAME: username
31+
SPRING_DATASOURCE_PASSWORD: password
32+
MAIL_USERNAME: test
33+
MAIL_PASSWORD: test
34+
APP_MAIL_FROM: [email protected]
35+
APP_ENV: test
36+
APP_JWT_SECRET: test-secret-key-for-testing-purposes-only
37+
APP_VERIFY_EMAIL_URL: http://localhost/verify
38+
APP_RESET_PASSWORD_URL: http://localhost/reset
39+
APP_GCS_BUCKET: test-bucket
40+
APP_CORS_ALLOWED_ORIGINS: http://localhost:3000
41+
run: |
42+
chmod +x ./mvnw
43+
./mvnw -B test

apps/jobboard-backend/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@
121121
<artifactId>google-cloud-storage</artifactId>
122122
</dependency>
123123

124+
<dependency>
125+
<groupId>com.h2database</groupId>
126+
<artifactId>h2</artifactId>
127+
<scope>test</scope>
128+
</dependency>
129+
124130

125131
</dependencies>
126132

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.bounswe.jobboardbackend.profile.controller;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.bounswe.jobboardbackend.auth.model.Role;
5+
import org.bounswe.jobboardbackend.auth.model.User;
6+
import org.bounswe.jobboardbackend.auth.repository.UserRepository;
7+
import org.bounswe.jobboardbackend.profile.dto.CreateProfileRequestDto;
8+
import org.bounswe.jobboardbackend.profile.dto.ProfileResponseDto;
9+
import org.bounswe.jobboardbackend.profile.service.ProfileService;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.boot.test.mock.mockito.MockBean;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.security.test.context.support.WithMockUser;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
18+
import java.util.Optional;
19+
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyLong;
22+
import static org.mockito.Mockito.when;
23+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
27+
28+
@WebMvcTest(ProfileController.class)
29+
class ProfileControllerTest {
30+
31+
@Autowired
32+
private MockMvc mockMvc;
33+
34+
@Autowired
35+
private ObjectMapper objectMapper;
36+
37+
@MockBean
38+
private ProfileService profileService;
39+
40+
@MockBean
41+
private UserRepository userRepository;
42+
43+
@Test
44+
@WithMockUser(username = "testuser")
45+
void createProfile_whenValidRequest_returnsOk() throws Exception {
46+
CreateProfileRequestDto dto = CreateProfileRequestDto.builder()
47+
.firstName("John")
48+
.lastName("Doe")
49+
.bio("Test bio")
50+
.build();
51+
52+
ProfileResponseDto responseDto = ProfileResponseDto.builder()
53+
.id(1L)
54+
.userId(1L)
55+
.firstName("John")
56+
.lastName("Doe")
57+
.bio("Test bio")
58+
.build();
59+
60+
User user = User.builder()
61+
.id(1L)
62+
.username("testuser")
63+
64+
.role(Role.ROLE_JOBSEEKER)
65+
.build();
66+
67+
when(userRepository.findByUsername("testuser")).thenReturn(Optional.of(user));
68+
when(profileService.createProfile(anyLong(), any(CreateProfileRequestDto.class)))
69+
.thenReturn(responseDto);
70+
71+
mockMvc.perform(post("/api/profile")
72+
.with(csrf())
73+
.contentType(MediaType.APPLICATION_JSON)
74+
.content(objectMapper.writeValueAsString(dto)))
75+
.andExpect(status().isOk())
76+
.andExpect(jsonPath("$.firstName").value("John"))
77+
.andExpect(jsonPath("$.lastName").value("Doe"));
78+
}
79+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.bounswe.jobboardbackend.profile.repository;
2+
3+
import org.bounswe.jobboardbackend.auth.model.Role;
4+
import org.bounswe.jobboardbackend.auth.model.User;
5+
import org.bounswe.jobboardbackend.auth.repository.UserRepository;
6+
import org.bounswe.jobboardbackend.profile.model.Profile;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
10+
11+
import java.util.Optional;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
@DataJpaTest
16+
class ProfileRepositoryTest {
17+
18+
@Autowired
19+
private ProfileRepository profileRepository;
20+
21+
@Autowired
22+
private UserRepository userRepository;
23+
24+
@Test
25+
void findByUserId_whenProfileExists_returnsProfile() {
26+
User user = User.builder()
27+
28+
.username("testuser")
29+
.password("password")
30+
.role(Role.ROLE_JOBSEEKER)
31+
.build();
32+
user = userRepository.save(user);
33+
34+
Profile profile = Profile.builder()
35+
.user(user)
36+
.firstName("John")
37+
.lastName("Doe")
38+
.bio("Test bio")
39+
.build();
40+
profileRepository.save(profile);
41+
42+
Optional<Profile> found = profileRepository.findByUserId(user.getId());
43+
44+
assertThat(found).isPresent();
45+
assertThat(found.get().getFirstName()).isEqualTo("John");
46+
assertThat(found.get().getLastName()).isEqualTo("Doe");
47+
}
48+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.bounswe.jobboardbackend.profile.service;
2+
3+
import org.bounswe.jobboardbackend.auth.model.Role;
4+
import org.bounswe.jobboardbackend.auth.model.User;
5+
import org.bounswe.jobboardbackend.auth.repository.UserRepository;
6+
import org.bounswe.jobboardbackend.profile.dto.CreateProfileRequestDto;
7+
import org.bounswe.jobboardbackend.profile.dto.ProfileResponseDto;
8+
import org.bounswe.jobboardbackend.profile.model.Profile;
9+
import org.bounswe.jobboardbackend.profile.repository.ProfileRepository;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.junit.jupiter.MockitoExtension;
15+
16+
import java.util.Optional;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.when;
21+
22+
@ExtendWith(MockitoExtension.class)
23+
class ProfileServiceTest {
24+
25+
@Mock
26+
private UserRepository userRepository;
27+
28+
@Mock
29+
private ProfileRepository profileRepository;
30+
31+
@InjectMocks
32+
private ProfileService profileService;
33+
34+
@Test
35+
void createProfile_whenValidRequest_returnsProfileResponseDto() {
36+
Long userId = 1L;
37+
User user = User.builder()
38+
.id(userId)
39+
40+
.username("testuser")
41+
.role(Role.ROLE_JOBSEEKER)
42+
.build();
43+
44+
CreateProfileRequestDto dto = CreateProfileRequestDto.builder()
45+
.firstName("John")
46+
.lastName("Doe")
47+
.bio("Test bio")
48+
.build();
49+
50+
Profile profile = Profile.builder()
51+
.id(1L)
52+
.user(user)
53+
.firstName("John")
54+
.lastName("Doe")
55+
.bio("Test bio")
56+
.build();
57+
58+
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
59+
when(profileRepository.findByUserId(userId)).thenReturn(Optional.empty());
60+
when(profileRepository.save(any(Profile.class))).thenReturn(profile);
61+
62+
ProfileResponseDto result = profileService.createProfile(userId, dto);
63+
64+
assertThat(result).isNotNull();
65+
assertThat(result.getFirstName()).isEqualTo("John");
66+
assertThat(result.getLastName()).isEqualTo("Doe");
67+
assertThat(result.getBio()).isEqualTo("Test bio");
68+
}
69+
}

0 commit comments

Comments
 (0)