Skip to content

Commit

Permalink
create EndorsementController (#41)
Browse files Browse the repository at this point in the history
* create EndorsementController

* add env to ignore

* add DTO and DROs

* de.env

* create EndorsementListController

* remove EndorsementListController

* add EndorsementServiceTest.java

* add JsonManagedReference

* address some review comments

* added validation checks

* remove extra entries in EndorsementDRO.java

* handle NoEntityException

* add DB_NAME variable in application.properties

* handle exceptions in one catch block

* made ApiResponse class

* handle NoEntityException exception globally

* remove default false

* merge the tests

* delete usused files

* fix build, add missing imports

* fix EndorsementListServiceTest

* fix testCreateEndorsement test

* revert change

* add integration test

* update integration test

* update integration test

* revert integration test

* fix integration test

* use GenericResponse

* fix return type

* change endorsementModel to EndorsementDTO.toDto
  • Loading branch information
bhtibrewal authored Dec 31, 2023
1 parent 47f7d31 commit cfe9421
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.RDS.skilltree.Common.Response.GenericResponse;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -46,4 +47,14 @@ public ResponseEntity<GenericResponse<EndorsementDTO>> getEndorsementById(@PathV
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new GenericResponse<EndorsementDTO>(null, message));
}
}

@PostMapping(value="")
public ResponseEntity<GenericResponse<EndorsementDTO>> postEndorsement(@RequestBody @Valid EndorsementDRO endorsementDRO) {

EndorsementModel endorsementModel = endorsementService.createEndorsement(endorsementDRO);
if (endorsementModel != null)
return new ResponseEntity<>(new GenericResponse<EndorsementDTO>(EndorsementDTO.toDto(endorsementModel), ""), HttpStatus.CREATED);
return new ResponseEntity<>(new GenericResponse<EndorsementDTO>(null,"Failed to create endorsement"), HttpStatus.BAD_REQUEST);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.RDS.skilltree.Endorsement;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class EndorsementDRO {
@NotNull(message = "user id cannot be null")
private UUID userId;
@NotNull(message = "skill id cannot be null")
private UUID skillId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface EndorsementService {
EndorsementDTO getEndorsementById(UUID id);
Page<EndorsementModel> getEndorsements(PageRequest pageRequest);
EndorsementModel createEndorsement(EndorsementDRO endorsementDRO);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.RDS.skilltree.Endorsement;

import com.RDS.skilltree.Exceptions.NoEntityException;
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.Skill.SkillRepository;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -13,6 +18,8 @@
@RequiredArgsConstructor
public class EndorsementServiceImpl implements EndorsementService {
private final EndorsementRepository endorsementRepository;
private final UserRepository userRepository;
private final SkillRepository skillRepository;

@Override
public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException {
Expand All @@ -25,4 +32,24 @@ public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException {
public Page<EndorsementModel> getEndorsements(PageRequest pageRequest) {
return endorsementRepository.findAll(pageRequest);
}

@Override
public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) {
UUID userId = endorsementDRO.getUserId();
UUID skillId = endorsementDRO.getSkillId();
Optional<UserModel> userOptional = userRepository.findById(userId);
Optional<SkillModel> skillOptional = skillRepository.findById(skillId);
if (userOptional.isPresent() && skillOptional.isPresent()) {
EndorsementModel endorsementModel = EndorsementModel.builder()
.user(userOptional.get())
.skill(skillOptional.get())
.build();

return endorsementRepository.save(endorsementModel);
} else {
if (userOptional.isEmpty())
throw new NoEntityException("User with id:" + userId + " not found");
throw new NoEntityException("Skill with id:" + skillId + " not found");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.RDS.skilltree.EndorsementList;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class EndorsementListDRO {
private String description;
private UUID endorsementId;
private UUID endorserId;
private EndorsementType type;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.RDS.skilltree.EndorsementList;

import com.RDS.skilltree.User.UserModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.UUID;

@Builder
@Data
@AllArgsConstructor
public class EndorsementListDTO {
private UUID id;
private String description;
private UserModel endorser;
private EndorsementType type;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.RDS.skilltree.EndorsementList;

import com.RDS.skilltree.Endorsement.EndorsementModel;
import com.RDS.skilltree.Endorsement.EndorsementRepository;
import com.RDS.skilltree.Exceptions.NoEntityException;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Service
public class EndorsementListService {
@Autowired
private final EndorsementListRepository endorsementListRepository;
private final EndorsementRepository endorsementRepository;
private final UserRepository userRepository;

public EndorsementListService(EndorsementListRepository endorsementListRepository, EndorsementRepository endorsementRepository, UserRepository userRepository) {
this.endorsementListRepository = endorsementListRepository;
this.endorsementRepository = endorsementRepository;
this.userRepository = userRepository;
}


public EndorsementListModel createEndorsementListEntry(EndorsementListDRO endorsementListDRO) {
EndorsementListModel endorsementListEntry = new EndorsementListModel();

UUID endorserId = endorsementListDRO.getEndorserId();
UUID endorsementId = endorsementListDRO.getEndorsementId();
Optional<UserModel> endorserOptional = userRepository.findById(endorserId);
Optional<EndorsementModel> endorsementOptional = endorsementRepository.findById(endorsementId);
if (endorserOptional.isPresent() && endorsementOptional.isPresent()) {

endorsementListEntry.setEndorser(endorserOptional.get());
endorsementListEntry.setEndorsement(endorsementOptional.get());
endorsementListEntry.setDescription(endorsementListDRO.getDescription());
endorsementListEntry.setType(endorsementListDRO.getType());
endorsementListRepository.save(endorsementListEntry);
return endorsementListEntry;

} else {
if (endorserOptional.isEmpty())
throw new NoEntityException("User with id:" + endorserId + " not found");
throw new NoEntityException("Endorsement with id:" + endorsementId + " not found");
}

}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
package com.RDS.skilltree;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import com.RDS.skilltree.Skill.*;
import com.RDS.skilltree.User.*;
import com.RDS.skilltree.Endorsement.*;
import io.restassured.response.Response;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;

@TestInstance(value = TestInstance.Lifecycle.PER_CLASS)
public class EndorsementsIntegrationTests {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class EndorsementsIntegrationTests extends TestContainerManager {

private UserRepository userRepository;
private SkillRepository skillRepository;
private final UserService userService;
private final SkillsService skillsService;
private UserDTO user;
private SkillDTO skill;
private EndorsementRepository endorsementRepository;

@Autowired
private ObjectMapper objectMapper;
public EndorsementsIntegrationTests(UserService userService, UserRepository userRepository, SkillsService skillsService, SkillRepository skillRepository, EndorsementRepository endorsementRepository) {
this.userService = userService;
this.userRepository = userRepository;
this.skillsService = skillsService;
this.skillRepository = skillRepository;
this.endorsementRepository = endorsementRepository;
}

@BeforeEach
private void addData() throws MalformedURLException {
user = userService.createUser(UserDRO.builder()
.role(UserRole.MEMBER)
.rdsUserId("p6Bo61VEClhtVdwW0ihg")
.lastName("Doe")
.firstName("John")
.imageUrl(new URL("https://res.cloudinary.com/realdevsquad/image/upload/v1666193594/profile/p6Bo61VEClhtVdwW0iGH/lezguwdq5bgzawa3.jpg"))
.build());

@BeforeAll
public void setup() {
RestAssured.baseURI = "http://localhost:8080";
skill = skillsService.createSkill(
SkillDRO.builder()
.name("Java")
.type(SkillType.ATOMIC)
.createdBy(user.getId())
.build());
}

@AfterEach
private void cleanUp() {
endorsementRepository.deleteAll();
skillRepository.deleteAll();
userRepository.deleteAll();
}

@Test
Expand Down Expand Up @@ -113,6 +155,71 @@ public void testAPIReturns400_OnInvalidParameterPassed() {
.body("message", equalTo("Invalid endorsementId passed"));
}

@Test
@DisplayName("Return 200 on endorsements creation")
public void testAPIReturns201_OnEndorsementCreation() {
UUID userId = user.getId();
UUID skillId = skill.getId();

EndorsementDRO endorsementDRO = new EndorsementDRO();
endorsementDRO.setUserId(userId);
endorsementDRO.setSkillId(skillId);
Response response = given()
.contentType("application/json")
.body(endorsementDRO)
.post("/v1/endorsements");


response.then()
.statusCode(201)
.contentType("application/json")
.body("data.user.firstName", equalTo("John"))
.body("data.skill.name", equalTo("Java"));
}

@Test
@DisplayName("Return 400 on endorsements userid null")
public void testAPIReturns400_OnEndorsementCreationUserIdNull() {

UUID skillId = skill.getId();

EndorsementDRO endorsementDRO = new EndorsementDRO();

endorsementDRO.setSkillId(skillId);
Response response = given()
.contentType("application/json")
.body(endorsementDRO)
.post("/v1/endorsements");


response.then()
.statusCode(400)
.contentType("application/json")
.body("data", equalTo(null))
.body("message", equalTo("user id cannot be null"));
}

@Test
@DisplayName("Return 400 on endorsements skillid null")
public void testAPIReturns400_OnEndorsementCreationSkillIdNull() {
UUID userId = user.getId();

EndorsementDRO endorsementDRO = new EndorsementDRO();
endorsementDRO.setUserId(userId);

Response response = given()
.contentType("application/json")
.body(endorsementDRO)
.post("/v1/endorsements");


response.then()
.statusCode(400)
.contentType("application/json")
.body("data", equalTo(null))
.body("message", equalTo("skill id cannot be null"));
}

@Test
@Disabled
@DisplayName("Return 200 on endorsements updation")
Expand Down Expand Up @@ -182,4 +289,5 @@ public void testAPIReturn404_OnEndorsementNotFound() {
.body("code", equalTo(404))
.body("message", equalTo("Endorsement not found"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootTest
class SkillTreeApplicationTests {

@Test
void contextLoads() {
}

@Test
void contextLoads() {
}
}
Loading

0 comments on commit cfe9421

Please sign in to comment.