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 24 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<?> 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<>(endorsementModel, HttpStatus.CREATED);
return new ResponseEntity<>("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,13 +1,9 @@
package com.RDS.skilltree;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SkillTreeApplicationTests {

@Test
void contextLoads() {
}

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

import com.RDS.skilltree.Endorsement.EndorsementModel;
import com.RDS.skilltree.Endorsement.EndorsementRepository;
import com.RDS.skilltree.EndorsementList.*;
import com.RDS.skilltree.Exceptions.NoEntityException;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class EndorsementListServiceTest {

@Mock
private EndorsementListRepository endorsementListRepository;

@Mock
private EndorsementRepository endorsementRepository;

@Mock
private UserRepository userRepository;

@InjectMocks
private EndorsementListService endorsementListService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testCreateEndorsementListEntry() {
// Mock data
UUID endorserId = UUID.randomUUID();
UUID endorsementId = UUID.randomUUID();
EndorsementListDRO endorsementListDRO = new EndorsementListDRO();
endorsementListDRO.setEndorserId(endorserId);
endorsementListDRO.setEndorsementId(endorsementId);
endorsementListDRO.setDescription("Test Description");
endorsementListDRO.setType(EndorsementType.POSITIVE);

UserModel mockUser = new UserModel();
mockUser.setId(endorserId);

EndorsementModel mockEndorsement = new EndorsementModel();
mockEndorsement.setId(endorsementId);

// Mock the repository behavior
when(userRepository.findById(endorserId)).thenReturn(Optional.of(mockUser));
when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(mockEndorsement));

// Call the service method
EndorsementListModel result = endorsementListService.createEndorsementListEntry(endorsementListDRO);

// Verify the interactions
verify(endorsementListRepository, times(1)).save(any(EndorsementListModel.class));

// Assertions
assertNotNull(result);
assertEquals(endorserId, result.getEndorser().getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also print some message, to demonstrate what failed in the assert statements.

assertEquals(endorsementId, result.getEndorsement().getId());
assertEquals("Test Description", result.getDescription());
assertEquals(EndorsementType.POSITIVE, result.getType());
}

@Test
void testCreateEndorsementListEntryWithInvalidUser() {
UUID endorserId = UUID.randomUUID();
UUID endorsementId = UUID.randomUUID();
EndorsementListDRO endorsementListDRO = new EndorsementListDRO();
endorsementListDRO.setEndorserId(endorserId);
endorsementListDRO.setEndorsementId(endorsementId);

// Mock the repository behavior for an invalid user
when(userRepository.findById(endorserId)).thenReturn(Optional.empty());

// Assert that a NoEntityException is thrown
NoEntityException exception = assertThrows(NoEntityException.class,
() -> endorsementListService.createEndorsementListEntry(endorsementListDRO));
assertEquals("User with id:" + endorserId + " not found", exception.getMessage());

// Verify that save method is not called
verify(endorsementListRepository, never()).save(any(EndorsementListModel.class));
}

@Test
void testCreateEndorsementListEntryWithInvalidEndorsement() {
UUID endorserId = UUID.randomUUID();
UUID endorsementId = UUID.randomUUID();
EndorsementListDRO endorsementListDRO = new EndorsementListDRO();
endorsementListDRO.setEndorserId(endorserId);
endorsementListDRO.setEndorsementId(endorsementId);

UserModel mockUser = new UserModel();
mockUser.setId(endorserId);

// Mock the repository behavior for an invalid endorsement
when(userRepository.findById(endorserId)).thenReturn(Optional.of(mockUser));
when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.empty());

// Assert that a NoEntityException is thrown
NoEntityException exception = assertThrows(NoEntityException.class,
() -> endorsementListService.createEndorsementListEntry(endorsementListDRO));
assertEquals("Endorsement with id:" + endorsementId + " not found", exception.getMessage());

// Verify that save method is not called
verify(endorsementListRepository, never()).save(any(EndorsementListModel.class));
}
}
Loading
Loading