Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create EndorsementController #41

Merged
merged 35 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0fdea86
create EndorsementController
bhtibrewal Nov 23, 2023
9a89d07
add env to ignore
bhtibrewal Nov 23, 2023
0d0aa10
add DTO and DROs
bhtibrewal Nov 24, 2023
3175cc6
de.env
bhtibrewal Nov 25, 2023
c9af284
merge branch update_model
bhtibrewal Nov 25, 2023
e90b5f2
create EndorsementListController
bhtibrewal Nov 25, 2023
d1faad5
remove EndorsementListController
bhtibrewal Nov 26, 2023
c0e211e
add EndorsementServiceTest.java
bhtibrewal Nov 26, 2023
39cf6f9
Merge branch 'develop' into postEndorsementAPI
bhtibrewal Nov 26, 2023
1f6c2d4
add JsonManagedReference
bhtibrewal Dec 1, 2023
ca1b171
Merge remote-tracking branch 'origin/postEndorsementAPI' into postEnd…
bhtibrewal Dec 1, 2023
0fde179
address some review comments
bhtibrewal Dec 4, 2023
a8a2e5e
added validation checks
bhtibrewal Dec 5, 2023
e867d0f
remove extra entries in EndorsementDRO.java
bhtibrewal Dec 6, 2023
be11ae5
handle NoEntityException
bhtibrewal Dec 6, 2023
2b17be2
add DB_NAME variable in application.properties
bhtibrewal Dec 6, 2023
3cacabe
handle exceptions in one catch block
bhtibrewal Dec 6, 2023
2e8bbe0
made ApiResponse class
bhtibrewal Dec 6, 2023
2084c09
handle NoEntityException exception globally
bhtibrewal Dec 6, 2023
d89effd
remove default false
bhtibrewal Dec 7, 2023
ba72dd8
Merge branch 'develop' of https://github.com/Real-Dev-Squad/skill-tre…
bhtibrewal Dec 29, 2023
08bf445
merge the tests
bhtibrewal Dec 29, 2023
77f7110
delete usused files
bhtibrewal Dec 29, 2023
5742947
fix build, add missing imports
bhtibrewal Dec 29, 2023
c5a09ca
fix EndorsementListServiceTest
bhtibrewal Dec 30, 2023
f4cbbbd
fix testCreateEndorsement test
bhtibrewal Dec 30, 2023
05237e6
revert change
bhtibrewal Dec 30, 2023
717f24d
add integration test
bhtibrewal Dec 30, 2023
eae68d5
update integration test
bhtibrewal Dec 30, 2023
9ed6eb3
update integration test
bhtibrewal Dec 30, 2023
f3f9894
revert integration test
bhtibrewal Dec 30, 2023
bb7ae49
fix integration test
bhtibrewal Dec 31, 2023
2fab76d
use GenericResponse
bhtibrewal Dec 31, 2023
3acd3d5
fix return type
bhtibrewal Dec 31, 2023
d90443f
change endorsementModel to EndorsementDTO.toDto
bhtibrewal Dec 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<EndorsementModel>> postEndorsement(@RequestBody @Valid EndorsementDRO endorsementDRO) {
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved

EndorsementModel endorsementModel = endorsementService.createEndorsement(endorsementDRO);
if (endorsementModel != null)
return new ResponseEntity<>(new GenericResponse<EndorsementModel>(endorsementModel, ""), HttpStatus.CREATED);
return new ResponseEntity<>(new GenericResponse<EndorsementModel>(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