Skip to content

Commit

Permalink
#94 | Implement the process of getting Assignment by ID (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 authored Aug 28, 2023
1 parent 729a674 commit e157d7d
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.ays.assignment.controller;

import com.ays.assignment.model.Assignment;
import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.model.dto.response.AssignmentResponse;
import com.ays.assignment.model.mapper.AssignmentToAssignmentResponseMapper;
import com.ays.assignment.service.AssignmentSaveService;
import com.ays.assignment.service.AssignmentService;
import com.ays.common.model.dto.response.AysResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* REST controller class for managing assignment-related operations via HTTP requests.
Expand All @@ -25,6 +27,9 @@ class AssignmentController {

private final AssignmentSaveService assignmentSaveService;

private final AssignmentService assignmentService;

private static final AssignmentToAssignmentResponseMapper assignmentToAssignmentResponseMapper = AssignmentToAssignmentResponseMapper.initialize();

/**
* Saves a new assignment to the system.
Expand All @@ -39,4 +44,23 @@ public AysResponse<Void> saveAssignment(@RequestBody @Valid AssignmentSaveReques
assignmentSaveService.saveAssignment(saveRequest);
return AysResponse.SUCCESS;
}


/**
* Gets a user by ID.
* Requires ADMIN authority.
*
* @param id The ID of the user to retrieve.
* @return A response object containing the retrieved user data.
*/
@GetMapping("/assignment/{id}")
@PreAuthorize("hasAnyAuthority('ADMIN')")
public AysResponse<AssignmentResponse> getAssignmentById(@PathVariable @UUID String id) {

final Assignment assignment = assignmentService.getAssignmentById(id);
final AssignmentResponse assignmentResponse = assignmentToAssignmentResponseMapper.map(assignment);
return AysResponse.successOf(assignmentResponse);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.util.Optional;

/**
* Repository interface for performing CRUD operations on UserAssignmentEntity objects.
*/
Expand All @@ -18,4 +20,14 @@ public interface AssignmentRepository extends JpaRepository<AssignmentEntity, St
*/
boolean existsByUserIdAndStatus(String userId, AssignmentStatus status);


/**
* Retrieves an optional AssignmentEntity based on the provided user ID and institution ID.
*
* @param id The ID of the assignment to retrieve.
* @param institutionId The ID of the institution associated with the user.
* @return An optional AssignmentEntity that matches the specified ID and institution ID, or an empty optional if not found.
*/
Optional<AssignmentEntity> findByIdAndInstitutionId(String id, String institutionId);

}
18 changes: 18 additions & 0 deletions src/main/java/com/ays/assignment/service/AssignmentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ays.assignment.service;


import com.ays.assignment.model.Assignment;

/**
* Assignment Save Service to perform assignment related business operations.
*/
public interface AssignmentService {

/**
* Get Assignment by Assignment ID
*
* @param id the given Assignment ID
* @return Assignment
*/
Assignment getAssignmentById(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.ays.assignment.service.impl;

import com.ays.assignment.model.Assignment;
import com.ays.assignment.model.entity.AssignmentEntity;
import com.ays.assignment.model.mapper.AssignmentEntityToAssignmentMapper;
import com.ays.assignment.repository.AssignmentRepository;
import com.ays.assignment.service.AssignmentService;
import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException;
import com.ays.auth.model.AysIdentity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
class AssignmentServiceImpl implements AssignmentService {

private final AssignmentRepository assignmentRepository;

private final AysIdentity identity;

private static final AssignmentEntityToAssignmentMapper assignmentEntityToAssignmentMapper = AssignmentEntityToAssignmentMapper.initialize();

/**
* Retrieves an assignment by their ID.
*
* @param id the ID of the assignment to retrieve
* @return the Assignment object representing the retrieved user
* @throws AysAssignmentNotExistByIdException if the assignment with the specified ID does not exist
*/
@Override
public Assignment getAssignmentById(String id) {

final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId())
.orElseThrow(() -> new AysAssignmentNotExistByIdException(id));

return assignmentEntityToAssignmentMapper.map(assignmentEntity);
}
}
1 change: 0 additions & 1 deletion src/main/resources/db/changelog/changes/5-ays-dml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
VALUES ('a9ec051e-3c4f-4cb5-ab08-e9dcee5e1f03', '91df7ae9-d5b9-44ae-b54f-d5d55359c4a4',
'Kıyafet İhtiyacı', 'Cem', 'Orkun', '90', '1234567890',
ST_GeomFromText('POINT(40.981210 29.000000)', 4326), 'AVAILABLE', 'AYS', CURRENT_TIMESTAMP);

INSERT INTO AYS_ASSIGNMENT (ID, USER_ID, INSTITUTION_ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE,
LINE_NUMBER, POINT, STATUS, CREATED_USER, CREATED_AT)
VALUES ('823835f4-85f0-4052-a09c-b9b6e5284afc', 'edb36891-b898-4c12-bcec-d9aaa5d45190',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.ays.assignment.controller;

import com.ays.AbstractRestControllerTest;
import com.ays.assignment.model.Assignment;
import com.ays.assignment.model.AssignmentBuilder;
import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder;
import com.ays.assignment.model.dto.response.AssignmentResponse;
import com.ays.assignment.model.mapper.AssignmentToAssignmentResponseMapper;
import com.ays.assignment.service.AssignmentSaveService;
import com.ays.assignment.service.AssignmentService;
import com.ays.common.model.AysPhoneNumberBuilder;
import com.ays.common.model.dto.response.AysResponse;
import com.ays.common.model.dto.response.AysResponseBuilder;
import com.ays.common.util.AysRandomUtil;
import com.ays.common.util.exception.model.AysError;
import com.ays.util.AysMockMvcRequestBuilders;
import com.ays.util.AysMockResultMatchersBuilders;
Expand All @@ -22,8 +28,14 @@ class AssignmentControllerTest extends AbstractRestControllerTest {
@MockBean
private AssignmentSaveService assignmentSaveService;

@MockBean
private AssignmentService assignmentService;

private static final String BASE_PATH = "/api/v1";

private static final AssignmentToAssignmentResponseMapper ASSIGNMENT_TO_ASSIGNMENT_RESPONSE_MAPPER = AssignmentToAssignmentResponseMapper.initialize();


@Test
void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSavedResponse() throws Exception {
// Given
Expand Down Expand Up @@ -85,4 +97,65 @@ void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAcc

}

@Test
void givenValidAssignmentId_whenAssignmentFound_thenReturnAssignmentResponse() throws Exception {

// Given
String mockAssignmentId = AysRandomUtil.generateUUID();

// When
Assignment mockAssignment = new AssignmentBuilder().build();
Mockito.when(assignmentService.getAssignmentById(mockAssignmentId))
.thenReturn(mockAssignment);


// Then
String endpoint = BASE_PATH.concat("/assignment/").concat(mockAssignmentId);
AssignmentResponse mockAssignmentResponse = ASSIGNMENT_TO_ASSIGNMENT_RESPONSE_MAPPER.map(mockAssignment);
AysResponse<AssignmentResponse> mockAysResponse = AysResponse.successOf(mockAssignmentResponse);
mockMvc.perform(AysMockMvcRequestBuilders
.get(endpoint, mockAdminUserToken.getAccessToken()))
.andDo(MockMvcResultHandlers.print())
.andExpect(AysMockResultMatchersBuilders.status().isOk())
.andExpect(AysMockResultMatchersBuilders.time()
.isNotEmpty())
.andExpect(AysMockResultMatchersBuilders.httpStatus()
.value(mockAysResponse.getHttpStatus().getReasonPhrase()))
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(mockAysResponse.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.isNotEmpty());

Mockito.verify(assignmentService, Mockito.times(1))
.getAssignmentById(mockAssignmentId);

}


@Test
void givenValidAssignmentId_whenUnauthorizedForGettingAssignmentById_thenReturnAccessDeniedException() throws Exception {

// Given
String mockAssignmentId = AysRandomUtil.generateUUID();

// Then
String endpoint = BASE_PATH.concat("/assignment/".concat(mockAssignmentId));
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
.get(endpoint, mockUserToken.getAccessToken());

AysResponse<AysError> mockResponse = AysResponseBuilder.FORBIDDEN;
mockMvc.perform(mockHttpServletRequestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(AysMockResultMatchersBuilders.status().isForbidden())
.andExpect(AysMockResultMatchersBuilders.time()
.isNotEmpty())
.andExpect(AysMockResultMatchersBuilders.httpStatus()
.value(mockResponse.getHttpStatus().name()))
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(mockResponse.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.doesNotExist());
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.ays.assignment.controller;

import com.ays.AbstractSystemTest;
import com.ays.assignment.model.Assignment;
import com.ays.assignment.model.AssignmentBuilder;
import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder;
import com.ays.assignment.model.dto.response.AssignmentResponse;
import com.ays.assignment.model.mapper.AssignmentToAssignmentResponseMapper;
import com.ays.common.model.AysPhoneNumberBuilder;
import com.ays.common.model.dto.response.AysResponse;
import com.ays.common.model.dto.response.AysResponseBuilder;
import com.ays.common.util.AysRandomUtil;
import com.ays.common.util.exception.model.AysError;
import com.ays.util.AysMockMvcRequestBuilders;
import com.ays.util.AysMockResultMatchersBuilders;
import com.ays.util.AysTestData;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
Expand All @@ -17,6 +23,9 @@ class AssignmentSystemTest extends AbstractSystemTest {

private static final String BASE_PATH = "/api/v1";

private static final AssignmentToAssignmentResponseMapper ASSIGNMENT_TO_ASSIGNMENT_RESPONSE_MAPPER = AssignmentToAssignmentResponseMapper.initialize();


@Test
void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSavedResponse() throws Exception {
// Given
Expand Down Expand Up @@ -73,4 +82,62 @@ void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAcc

}

@Test
void givenValidAssignmentId_whenAssignmentFound_thenReturnAssignmentResponse() throws Exception {

// Given
String mockAssignmentId = AysTestData.Assignment.VALID_ID_ONE;

// When
Assignment mockAssignment = new AssignmentBuilder()
.withId(mockAssignmentId)
.build();

// Then
String endpoint = BASE_PATH.concat("/assignment/").concat(mockAssignmentId);
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
.get(endpoint, adminUserTokenOne.getAccessToken());

AssignmentResponse mockAssignmentResponse = ASSIGNMENT_TO_ASSIGNMENT_RESPONSE_MAPPER.map(mockAssignment);
AysResponse<AssignmentResponse> mockAysResponse = AysResponse.successOf(mockAssignmentResponse);
mockMvc.perform(mockHttpServletRequestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(AysMockResultMatchersBuilders.status().isOk())
.andExpect(AysMockResultMatchersBuilders.time()
.isNotEmpty())
.andExpect(AysMockResultMatchersBuilders.httpStatus()
.value(mockAysResponse.getHttpStatus().getReasonPhrase()))
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(mockAysResponse.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.exists());

}


@Test
void givenValidAssignmentId_whenUnauthorizedForGettingAssignmentById_thenReturnAccessDeniedException() throws Exception {

// Given
String mockAssignmentId = AysRandomUtil.generateUUID();

// Then
String endpoint = BASE_PATH.concat("/assignment/".concat(mockAssignmentId));
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
.get(endpoint, userTokenOne.getAccessToken());

AysResponse<AysError> mockResponse = AysResponseBuilder.FORBIDDEN;
mockMvc.perform(mockHttpServletRequestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(AysMockResultMatchersBuilders.status().isForbidden())
.andExpect(AysMockResultMatchersBuilders.time()
.isNotEmpty())
.andExpect(AysMockResultMatchersBuilders.httpStatus()
.value(mockResponse.getHttpStatus().name()))
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(mockResponse.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.doesNotExist());
}

}
5 changes: 5 additions & 0 deletions src/test/java/com/ays/assignment/model/AssignmentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public AssignmentBuilder() {
super(Assignment.class);
}

public AssignmentBuilder withId(String id) {
data.setId(id);
return this;
}

public AssignmentBuilder withDescription(String description) {
data.setDescription(description);
return this;
Expand Down
Loading

0 comments on commit e157d7d

Please sign in to comment.