Skip to content

Commit

Permalink
test: Add tests for delete operations
Browse files Browse the repository at this point in the history
Add tests for delete operations and change the commands to use userId instead of freelancer_id
  • Loading branch information
MahmoudHosamGaber committed May 8, 2024
1 parent ee31ec6 commit b78098a
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class RemoveFreelancerAchievementCommand
extends UserCommand<RemoveFreelancerAchievementRequest, RemoveFreelancerAchievementResponse> {
@Override
public RemoveFreelancerAchievementResponse Run(RemoveFreelancerAchievementRequest request) {
Optional<Freelancer> freelancerOptional =
freelancerRepository.findById(request.getFreelancerId());
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUserId());
if (freelancerOptional.isEmpty())
return RemoveFreelancerAchievementResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class RemoveFreelancerEducationCommand

@Override
public RemoveFreelancerEducationResponse Run(RemoveFreelancerEducationRequest request) {
Optional<Freelancer> freelancerOptional =
freelancerRepository.findById(request.getFreelancer_id());
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUserId());
if (freelancerOptional.isEmpty())
return RemoveFreelancerEducationResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class RemoveFreelancerExperienceCommand
extends UserCommand<RemoveFreelancerExperienceRequest, RemoveFreelancerExperienceResponse> {
@Override
public RemoveFreelancerExperienceResponse Run(RemoveFreelancerExperienceRequest request) {
Optional<Freelancer> freelancerOptional =
freelancerRepository.findById(request.getFreelancer_id());
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUserId());
if (freelancerOptional.isEmpty())
return RemoveFreelancerExperienceResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RemoveFreelancerLanguageCommand
extends UserCommand<RemoveFreelancerLanguageRequest, RemoveFreelancerLanguageResponse> {
@Override
public RemoveFreelancerLanguageResponse Run(RemoveFreelancerLanguageRequest request) {
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUser_id());
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUserId());
if (freelancerOptional.isEmpty())
return RemoveFreelancerLanguageResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RemoveFreelancerSkillCommand
extends UserCommand<RemoveFreelancerSkillRequest, RemoveFreelancerSkillResponse> {
@Override
public RemoveFreelancerSkillResponse Run(RemoveFreelancerSkillRequest request) {
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUser_id());
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.getUserId());
if (freelancerOptional.isEmpty())
return RemoveFreelancerSkillResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,114 @@ void testUpdateExperience() {
Experience updatedExperience = freelancer.getExperiences().get(0);
UsersTestUtils.assertExperienceEquals(expectedExperience, updatedExperience);
}

@Test
void testRemoveAchievement() {
Freelancer freelancer = freelancerRepository.save(UsersTestUtils.createTestFreelancer());
Achievement achievement = achievementRepository.save(UsersTestUtils.createTestAchievement());
freelancer.getAchievements().add(achievement);
freelancerRepository.save(freelancer);

RemoveFreelancerAchievementRequest request =
RemoveFreelancerAchievementRequest.builder()
.withUserId(freelancer.getId().toString())
.withAchievementId(achievement.getId().toString())
.build();
RemoveFreelancerAchievementResponse response =
(RemoveFreelancerAchievementResponse)
template.convertSendAndReceive(ServiceQueueNames.USERS, request);

Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.OK, response.getStatusCode());
freelancer = freelancerRepository.findById(freelancer.getId().toString()).get();
Assertions.assertTrue(freelancer.getAchievements().isEmpty());
}

@Test
void testRemoveEducation() {
Freelancer freelancer = freelancerRepository.save(UsersTestUtils.createTestFreelancer());
Education education = educationRepository.save(UsersTestUtils.createTestEducation());
freelancer.getEducations().add(education);
freelancerRepository.save(freelancer);

RemoveFreelancerEducationRequest request =
RemoveFreelancerEducationRequest.builder()
.withUserId(freelancer.getId().toString())
.withEducation_id(education.getId().toString())
.build();
RemoveFreelancerEducationResponse response =
(RemoveFreelancerEducationResponse)
template.convertSendAndReceive(ServiceQueueNames.USERS, request);

Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.OK, response.getStatusCode());
freelancer = freelancerRepository.findById(freelancer.getId().toString()).get();
Assertions.assertTrue(freelancer.getEducations().isEmpty());
}

@Test
void testRemoveExperience() {
Freelancer freelancer = freelancerRepository.save(UsersTestUtils.createTestFreelancer());
Experience experience = experienceRepository.save(UsersTestUtils.createTestExperience());
freelancer.getExperiences().add(experience);
freelancerRepository.save(freelancer);

RemoveFreelancerExperienceRequest request =
RemoveFreelancerExperienceRequest.builder()
.withUserId(freelancer.getId().toString())
.withExperience_id(experience.getId().toString())
.build();
RemoveFreelancerExperienceResponse response =
(RemoveFreelancerExperienceResponse)
template.convertSendAndReceive(ServiceQueueNames.USERS, request);

Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.OK, response.getStatusCode());
freelancer = freelancerRepository.findById(freelancer.getId().toString()).get();
Assertions.assertTrue(freelancer.getExperiences().isEmpty());
}

@Test
void testRemoveSkill() {
Freelancer freelancer = freelancerRepository.save(UsersTestUtils.createTestFreelancer());
String skill = "Java";
freelancer.getSkills().add(skill);
freelancerRepository.save(freelancer);

RemoveFreelancerSkillRequest request =
RemoveFreelancerSkillRequest.builder()
.withUserId(freelancer.getId().toString())
.withSkillToRemove(skill)
.build();
RemoveFreelancerSkillResponse response =
(RemoveFreelancerSkillResponse)
template.convertSendAndReceive(ServiceQueueNames.USERS, request);

Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.OK, response.getStatusCode());
freelancer = freelancerRepository.findById(freelancer.getId().toString()).get();
Assertions.assertTrue(freelancer.getSkills().isEmpty());
}

@Test
void testRemoveLanguage() {
Freelancer freelancer = freelancerRepository.save(UsersTestUtils.createTestFreelancer());
String language = "English";
freelancer.getLanguages().add(language);
freelancerRepository.save(freelancer);

RemoveFreelancerLanguageRequest request =
RemoveFreelancerLanguageRequest.builder()
.withUserId(freelancer.getId().toString())
.withLanguageToRemove(language)
.build();
RemoveFreelancerLanguageResponse response =
(RemoveFreelancerLanguageResponse)
template.convertSendAndReceive(ServiceQueueNames.USERS, request);

Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.OK, response.getStatusCode());
freelancer = freelancerRepository.findById(freelancer.getId().toString()).get();
Assertions.assertTrue(freelancer.getLanguages().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class RemoveFreelancerAchievementRequest extends CommandRequest {
String freelancerId;
String achievementId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class RemoveFreelancerEducationRequest extends CommandRequest {
String freelancer_id;
String education_id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class RemoveFreelancerExperienceRequest extends CommandRequest {
String freelancer_id;
String experience_id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class RemoveFreelancerLanguageRequest extends CommandRequest {
private String user_id;
private String languageToRemove;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class RemoveFreelancerSkillRequest extends CommandRequest {
String user_id;
String skillToRemove;
}

0 comments on commit b78098a

Please sign in to comment.