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

Adds Get Endorsement End Point #40

Merged
merged 18 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0d9e2d8
Adds Get Endorsement End Point Controller Model changes
heyrandhir Nov 23, 2023
969c496
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Nov 27, 2023
232d062
Use JSON API Specs format to convert data
heyrandhir Dec 1, 2023
5680e23
remove JsonInclude annotation that wasn't being used
heyrandhir Dec 2, 2023
11717e2
Refactor: Update map structure in convertToHashMap method
heyrandhir Dec 2, 2023
4e0a258
Refactor: Resolve comments by Ajey Krishna
heyrandhir Dec 2, 2023
8341960
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 5, 2023
c4d1242
Refactor: Resolve comments by AjeyaK Bhavika and Debanjan
heyrandhir Dec 5, 2023
e08f967
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 5, 2023
512d7da
Merge branch 'feat/get-endorsement-endpoint' of https://github.com/Re…
heyrandhir Dec 5, 2023
73716ab
Refactor: Remove empty file
heyrandhir Dec 5, 2023
c577994
Added required args constructor and removed redundant lines from tests
heyrandhir Dec 14, 2023
21424bf
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 14, 2023
aea1097
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 14, 2023
8291aae
convert the models to respective DTO's
heyrandhir Dec 14, 2023
7c1a2f6
Defined types to maintain a consistent format for the Api response
heyrandhir Dec 14, 2023
80e33aa
Resolve comments by vikhyat
heyrandhir Dec 14, 2023
0c295cd
Adds @JsonInclude(JsonInclude.Include.NON_NULL) to improve response c…
heyrandhir Dec 15, 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
@@ -0,0 +1,41 @@
package com.RDS.skilltree.Endorsement;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;

@RestController
@RequestMapping("/v1/endorsements")
@Slf4j
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
@RequiredArgsConstructor
public class EndorsementController {
private final EndorsementService endorsementService;

@GetMapping("/{id}")
public ResponseEntity<EndorsementResponse<EndorsementDTO>> getEndorsementById(@PathVariable(value = "id", required = true) String id){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
try {
UUID uuid = UUID.fromString(id);
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
EndorsementDTO response = endorsementService.getEndorsementById(uuid);
return ResponseEntity.ok().headers(headers).body(new EndorsementResponse<EndorsementDTO>(response, "Data retrieved successfully"));
} catch (IllegalArgumentException e) {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
String message = "Invalid UUID: " + id;
return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers(headers).body(new EndorsementResponse<EndorsementDTO>(null, message));
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers).body(new EndorsementResponse<EndorsementDTO>(null, e.getMessage()));
} catch (Exception e) {
String message = "Something went wrong. Please contact admin.";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers(headers).body(new EndorsementResponse<EndorsementDTO>(null, message));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.RDS.skilltree.Endorsement;

import com.RDS.skilltree.EndorsementList.EndorsementListModel;
import com.RDS.skilltree.Skill.SkillDTO;
import com.RDS.skilltree.User.UserDTO;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*;
import java.util.List;
import java.util.UUID;

@Getter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
public class EndorsementDTO extends TrackedProperties {
private UUID id;
private UserDTO user;
private SkillDTO skill;
private EndorsementStatus status;
private List<EndorsementListModel> endorsersList;
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

public static EndorsementDTO toDto(EndorsementModel endorsementModel) {
EndorsementDTO endorsementDTO = EndorsementDTO.builder()
.id(endorsementModel.getId())
.user(UserDTO.toDTO(endorsementModel.getUser()))
.skill(SkillDTO.toDto(endorsementModel.getSkill()))
.status(endorsementModel.getStatus())
.endorsersList(endorsementModel.getEndorsersList())
.build();
endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt());
endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt());
endorsementDTO.setCreatedBy(endorsementModel.getCreatedBy());
endorsementDTO.setUpdatedBy(endorsementModel.getUpdatedBy());
return endorsementDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Builder
@Getter
@Table(name = "endorsements")
public class EndorsementModel extends TrackedProperties {
@Id
Expand All @@ -33,15 +33,10 @@ public class EndorsementModel extends TrackedProperties {
private SkillModel skill;

@OneToMany(mappedBy = "endorsement")
@JsonManagedReference
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
private List<EndorsementListModel> endorsersList = new ArrayList<>();

@Column(name = "endorsement_status")
@Enumerated(value = EnumType.STRING)
private EndorsementStatus status = EndorsementStatus.PENDING;

public EndorsementModel(UserModel user, SkillModel skill) {
this.status = EndorsementStatus.PENDING;
this.user = user;
this.skill = skill;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.RDS.skilltree.Endorsement;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class EndorsementResponse<T> {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
private T data;
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.RDS.skilltree.Endorsement;
import java.util.UUID;

public interface EndorsementService {
EndorsementDTO getEndorsementById(UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.RDS.skilltree.Endorsement;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class EndorsementServiceImpl implements EndorsementService {
private final EndorsementRepository endorsementRepository;

@Override
public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException {
Optional<EndorsementModel> endorsementModel = endorsementRepository.findById(id);
return EndorsementDTO.toDto(endorsementModel.orElseThrow(() -> new EntityNotFoundException("No endorsement with the id " + id + " found")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.RDS.skilltree.Endorsement.EndorsementModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -23,6 +24,7 @@ public class EndorsementListModel extends TrackedProperties {

@JoinColumn(name = "endorsement_id", referencedColumnName = "id")
@ManyToOne(targetEntity = EndorsementModel.class, cascade = CascadeType.ALL)
@JsonBackReference
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
private EndorsementModel endorsement;

@JoinColumn(name = "user_id", referencedColumnName = "id")
Expand All @@ -40,11 +42,11 @@ public class EndorsementListModel extends TrackedProperties {
private EndorsementType type;

public EndorsementListModel(EndorsementModel endorsement, UserModel endorser, String description,
EndorsementType type) {
EndorsementType type) {
this.endorsement = endorsement;
this.endorser = endorser;
this.description = description;
this.type = type;
this.deleted = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.RDS.skilltree.Endorsement;

import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.User.UserModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.assertThatThrownBy;


import java.time.Instant;
import java.util.Optional;
import java.util.UUID;

@RunWith(MockitoJUnitRunner.class)
public class EndorsementServiceTest {
@Mock
private EndorsementRepository endorsementRepository;

@InjectMocks
@Autowired
private EndorsementServiceImpl underTest;

@Test
public void itShouldGetEndorsementsById() {
UUID endorsementId = UUID.randomUUID();
UUID userId = UUID.randomUUID();
UUID skillId = UUID.randomUUID();

UserModel userModel = UserModel.builder().id(userId).build();
SkillModel skillModel = SkillModel.builder().id(skillId).build();
EndorsementModel endorsementModel = EndorsementModel.builder()
.id(endorsementId)
.user(userModel)
.skill(skillModel)
.build();
endorsementModel.setCreatedAt(Instant.now());
endorsementModel.setUpdatedAt(Instant.now());
endorsementModel.setCreatedBy(userModel);
endorsementModel.setUpdatedBy(userModel);

when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(endorsementModel));

EndorsementDTO result = underTest.getEndorsementById(endorsementId);

assertNotNull(result);
assertEquals("The Endorsement Id doesn't matches the expected endorsement Id", endorsementId, result.getId());
}

@Test
public void itShouldHandleEndorsementNotFound() {
UUID nonExistentEndorsementId = UUID.randomUUID();
when(endorsementRepository.findById(nonExistentEndorsementId)).thenReturn(Optional.empty());

assertThatThrownBy(() -> underTest.getEndorsementById(nonExistentEndorsementId))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("No endorsement with the id " + nonExistentEndorsementId + " found");
}

}