Skip to content

Commit 49e96a5

Browse files
committed
feat: created service UpdatePostService
1 parent b8f456e commit 49e96a5

File tree

3 files changed

+299
-0
lines changed

3 files changed

+299
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.joaov1ct0r.restful_api_users_java.modules.posts.dtos;
2+
3+
public class UpdatePostDTO {
4+
private String content;
5+
6+
private String id;
7+
8+
public UpdatePostDTO() {}
9+
10+
public UpdatePostDTO(String id, String content) {
11+
this.id = id;
12+
this.content = content;
13+
}
14+
15+
public String getContent() {
16+
return this.content;
17+
}
18+
19+
public void setContent(String content) {
20+
this.content = content;
21+
}
22+
23+
public String getId() {
24+
return this.id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.joaov1ct0r.restful_api_users_java.modules.posts.services;
2+
3+
import com.joaov1ct0r.restful_api_users_java.modules.domain.services.BaseService;
4+
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.PostDTO;
5+
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.UpdatePostDTO;
6+
import com.joaov1ct0r.restful_api_users_java.modules.posts.entities.PostEntity;
7+
import com.joaov1ct0r.restful_api_users_java.modules.posts.mappers.PostMapper;
8+
import com.joaov1ct0r.restful_api_users_java.modules.posts.repositories.PostRepository;
9+
import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity;
10+
import com.joaov1ct0r.restful_api_users_java.modules.users.repositories.UserRepository;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Service;
13+
import java.util.Optional;
14+
import java.util.UUID;
15+
16+
@Service
17+
public class UpdatePostService extends BaseService {
18+
@Autowired
19+
private UserRepository userRepository;
20+
21+
@Autowired
22+
private PostRepository postRepository;
23+
24+
public PostDTO execute(UpdatePostDTO postDTO, UUID userId) {
25+
Optional<UserEntity> user = this.userRepository.findById(userId);
26+
27+
boolean userIsNotFound = user.isEmpty();
28+
29+
if (userIsNotFound) {
30+
this.generateErrorLog(
31+
userId,
32+
400,
33+
"Usuário com id: " + userId + " tentou atualizar um post, porem usuário não foi encontrado!"
34+
);
35+
36+
throw this.badRequestException("Usuário não encontrado!");
37+
}
38+
39+
Optional<PostEntity> post = this.postRepository.findById(UUID.fromString(postDTO.getId()));
40+
41+
boolean postIsNotFound = post.isEmpty();
42+
43+
if (postIsNotFound) {
44+
this.generateErrorLog(
45+
userId,
46+
400,
47+
"Usuário com id: " + userId + " tentou atualizar um post, porem post não foi encontrado!"
48+
);
49+
50+
throw this.badRequestException("Post não encontrado!");
51+
}
52+
53+
boolean userIsOwnerOfPost = user.get().getId().equals(post.get().getUserWhoCreatedId());
54+
55+
if (!userIsOwnerOfPost) {
56+
this.generateErrorLog(
57+
userId,
58+
403,
59+
"Usuário com id: " + userId + " tentou atualizar um post, porem não tem permissão"
60+
);
61+
62+
throw this.forbiddenException("Não permitido!");
63+
}
64+
65+
PostEntity postToUpdate = post.get();
66+
postToUpdate.setContent(postDTO.getContent());
67+
68+
PostEntity updatedPost = this.postRepository.save(postToUpdate);
69+
70+
return PostMapper.toDTO(updatedPost);
71+
}
72+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package com.joaov1ct0r.restful_api_users_java.modules.posts.services;
2+
3+
import com.joaov1ct0r.restful_api_users_java.modules.domain.entities.ErrorLogEntity;
4+
import com.joaov1ct0r.restful_api_users_java.modules.domain.exceptions.BadRequestException;
5+
import com.joaov1ct0r.restful_api_users_java.modules.domain.exceptions.ForbiddenException;
6+
import com.joaov1ct0r.restful_api_users_java.modules.domain.repositories.ErrorLogsRepository;
7+
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.PostDTO;
8+
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.UpdatePostDTO;
9+
import com.joaov1ct0r.restful_api_users_java.modules.posts.entities.PostEntity;
10+
import com.joaov1ct0r.restful_api_users_java.modules.posts.repositories.PostRepository;
11+
import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity;
12+
import com.joaov1ct0r.restful_api_users_java.modules.users.repositories.UserRepository;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
import org.mockito.InjectMocks;
18+
import org.mockito.Mock;
19+
import org.mockito.Mockito;
20+
import org.mockito.junit.jupiter.MockitoExtension;
21+
import org.mockito.junit.jupiter.MockitoSettings;
22+
import org.mockito.quality.Strictness;
23+
import java.time.LocalDateTime;
24+
import java.util.Optional;
25+
import java.util.UUID;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.mockito.Mockito.when;
28+
import static org.mockito.Mockito.any;
29+
30+
@ExtendWith(MockitoExtension.class)
31+
@MockitoSettings(strictness = Strictness.LENIENT)
32+
public class UpdatePostServiceTest {
33+
@InjectMocks
34+
private UpdatePostService sut;
35+
36+
@Mock
37+
private PostRepository postRepository;
38+
39+
@Mock
40+
private UserRepository userRepository;
41+
42+
@Mock
43+
private ErrorLogsRepository errorLogsRepository;
44+
45+
@BeforeEach
46+
public void beforeEachSetUp() {
47+
Mockito.reset(this.postRepository);
48+
Mockito.reset(this.userRepository);
49+
Mockito.reset(this.errorLogsRepository);
50+
51+
when(
52+
this.errorLogsRepository.save(
53+
any()
54+
)
55+
).thenReturn(
56+
new ErrorLogEntity(
57+
UUID.randomUUID(),
58+
UUID.randomUUID(),
59+
LocalDateTime.now(),
60+
400,
61+
"any_description"
62+
)
63+
);
64+
65+
}
66+
67+
@Test
68+
@DisplayName("Should not be able to update a post if user is not found")
69+
public void shouldNotBeAbleToUpdateAPostIfUserIsNotFound() {
70+
UUID userId = UUID.randomUUID();
71+
UpdatePostDTO updatePostDTO = new UpdatePostDTO();
72+
73+
when(this.userRepository.findById(any())).thenReturn(Optional.empty());
74+
75+
try {
76+
this.sut.execute(updatePostDTO, userId);
77+
} catch(Exception e) {
78+
assertThat(e).isInstanceOf(BadRequestException.class);
79+
}
80+
}
81+
82+
@Test
83+
@DisplayName("Should not be able to update a post if a post is not found")
84+
public void shouldNotBeAbleToUpdateAPostIfPostIfNotFound() {
85+
UUID userId = UUID.randomUUID();
86+
UpdatePostDTO updatePostDTO = new UpdatePostDTO(UUID.randomUUID().toString(), "any_content");
87+
88+
when(this.userRepository.findById(any())).thenReturn(
89+
Optional.of(
90+
new UserEntity(
91+
userId,
92+
"any_name",
93+
94+
"any_username",
95+
"any_password",
96+
"any_photo_url",
97+
LocalDateTime.now(),
98+
null
99+
)
100+
)
101+
);
102+
103+
when(this.postRepository.findById(any())).thenReturn(Optional.empty());
104+
105+
try {
106+
this.sut.execute(updatePostDTO, userId);
107+
} catch(Exception e) {
108+
assertThat(e).isInstanceOf(BadRequestException.class);
109+
}
110+
}
111+
112+
@Test
113+
@DisplayName("Should not be able to update a post if user is not owner of post")
114+
public void shouldNotBeAbleToUpdateAPostIfUserIsNotOwnerOfPost() {
115+
UUID userId = UUID.randomUUID();
116+
UUID postId = UUID.randomUUID();
117+
UpdatePostDTO updatePostDTO = new UpdatePostDTO(postId.toString(), "any_content");
118+
119+
when(this.userRepository.findById(any())).thenReturn(
120+
Optional.of(
121+
new UserEntity(
122+
userId,
123+
"any_name",
124+
125+
"any_username",
126+
"any_password",
127+
"any_photo_url",
128+
LocalDateTime.now(),
129+
null
130+
)
131+
)
132+
);
133+
134+
when(this.postRepository.findById(any())).thenReturn(Optional.of(
135+
new PostEntity(
136+
postId,
137+
"any_other_content",
138+
LocalDateTime.now(),
139+
null,
140+
UUID.randomUUID()
141+
)
142+
));
143+
144+
try {
145+
this.sut.execute(updatePostDTO, userId);
146+
} catch (Exception e) {
147+
assertThat(e).isInstanceOf(ForbiddenException.class);
148+
}
149+
}
150+
151+
@Test
152+
@DisplayName("Should be able to update a post")
153+
public void shouldBeAbleToUpdateAPost() {
154+
UUID userId = UUID.randomUUID();
155+
UUID postId = UUID.randomUUID();
156+
UpdatePostDTO updatePostDTO = new UpdatePostDTO(postId.toString(), "any_content");
157+
158+
when(this.userRepository.findById(any())).thenReturn(
159+
Optional.of(
160+
new UserEntity(
161+
userId,
162+
"any_name",
163+
164+
"any_username",
165+
"any_password",
166+
"any_photo_url",
167+
LocalDateTime.now(),
168+
null
169+
)
170+
)
171+
);
172+
173+
when(this.postRepository.findById(any())).thenReturn(Optional.of(
174+
new PostEntity(
175+
postId,
176+
"any_other_content",
177+
LocalDateTime.now(),
178+
null,
179+
userId
180+
)
181+
));
182+
183+
when(this.postRepository.save(any())).thenReturn(
184+
new PostEntity(
185+
postId,
186+
updatePostDTO.getContent(),
187+
LocalDateTime.now(),
188+
LocalDateTime.now(),
189+
userId
190+
)
191+
);
192+
193+
PostDTO updatedPost = this.sut.execute(updatePostDTO, userId);
194+
195+
assertThat(updatedPost.getUserWhoCreatedId()).isEqualTo(userId);
196+
}
197+
}

0 commit comments

Comments
 (0)