Skip to content

Commit

Permalink
#230 | Implement Admin Registration Application Summary Endpoint (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetAksunger authored Dec 10, 2023
1 parent 160c390 commit 62fe3cc
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationResponse;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationSummaryResponse;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationsResponse;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper;
import com.ays.admin_user.service.AdminUserRegisterApplicationService;
import com.ays.common.model.AysPage;
import com.ays.common.model.dto.response.AysPageResponse;
import com.ays.common.model.dto.response.AysResponse;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -26,14 +30,15 @@
@RestController
@RequestMapping("/api/v1/admin")
@RequiredArgsConstructor
@Validated
class AdminUserRegisterApplicationController {

private final AdminUserRegisterApplicationService adminUserRegisterApplicationService;


private final AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.initialize();
private final AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper.initialize();

private final AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.initialize();

/**
* Gets a list of admin user register applications in the system.
Expand Down Expand Up @@ -74,4 +79,21 @@ public AysResponse<AdminUserRegisterApplicationResponse> getRegistrationApplicat
);
}

/**
* Gets an admin user register application summary in the system.
* Requires no authority.
*
* @param id The id of the register application.
* @return A response with the register application summary.
*/
@GetMapping("/registration-application/{id}/summary")
public AysResponse<AdminUserRegisterApplicationSummaryResponse> getRegistrationApplicationSummaryById(@PathVariable @UUID String id) {
final AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService
.getRegistrationApplicationSummaryById(id);

return AysResponse.successOf(
adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.map(registerApplication)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ays.admin_user.model.dto.response;

import lombok.Getter;
import lombok.Setter;

/**
* A DTO (Data Transfer Object) representing an admin user register application summary
*/
@Getter
@Setter
public class AdminUserRegisterApplicationSummaryResponse {

private String id;
private Institution institution;

/**
* A DTO (Data Transfer Object) representing an institution in an admin user register application
*/
@Getter
@Setter
public static class Institution {
private String id;
private String name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ays.admin_user.model.mapper;

import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationSummaryResponse;
import com.ays.common.model.mapper.BaseMapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper is an interface that defines the mapping between an {@link AdminUserRegisterApplication} and an {@link AdminUserRegisterApplicationSummaryResponse}.
* This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time.
* <p>The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation.
* <p>The interface extends the MapStruct interface {@link BaseMapper}, which defines basic mapping methods.
* The interface adds no additional mapping methods, but simply defines the types to be used in the mapping process.
*/
@Mapper
public interface AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper extends BaseMapper<AdminUserRegisterApplication, AdminUserRegisterApplicationSummaryResponse> {

/**
* Initializes the mapper.
*
* @return the initialized mapper object.
*/
static AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper initialize() {
return Mappers.getMapper(AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public interface AdminUserRegisterApplicationService {
*/
AdminUserRegisterApplication getRegistrationApplicationById(String id);

/**
* Get registration application summary by id.
*
* @param id registration application id
* @return Admin User Registration Application
*/
AdminUserRegisterApplication getRegistrationApplicationSummaryById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntity;
import com.ays.admin_user.model.enums.AdminUserRegisterApplicationStatus;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper;
import com.ays.admin_user.repository.AdminUserRegisterApplicationRepository;
import com.ays.admin_user.service.AdminUserRegisterApplicationService;
import com.ays.admin_user.util.exception.AysAdminUserRegisterApplicationNotExistByIdAndStatusException;
import com.ays.admin_user.util.exception.AysAdminUserRegisterApplicationNotExistByIdException;
import com.ays.common.model.AysPage;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -68,4 +70,21 @@ public AdminUserRegisterApplication getRegistrationApplicationById(String id) {
return adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.map(registerApplicationEntity);
}

/**
* Retrieves an admin user register application by id and checks if it is waiting.
* If it is not waiting, throws an exception.
*
* @param id registration application id
* @return An admin user register application.
*/
@Override
public AdminUserRegisterApplication getRegistrationApplicationSummaryById(String id) {
final AdminUserRegisterApplicationEntity registerApplicationEntity = adminUserRegisterApplicationRepository
.findById(id)
.filter(AdminUserRegisterApplicationEntity::isWaiting)
.orElseThrow(() -> new AysAdminUserRegisterApplicationNotExistByIdAndStatusException(id, AdminUserRegisterApplicationStatus.WAITING));

return adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.map(registerApplicationEntity);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ays.admin_user.util.exception;

import com.ays.admin_user.model.enums.AdminUserRegisterApplicationStatus;
import com.ays.common.util.exception.AysNotExistException;

import java.io.Serial;

/**
* Exception indicating that an admin user register application does not exist with the specified ID.
* This exception is a subclass of AysNotExistException, which is typically used to indicate that an entity or
* resource does not exist.
* Typically, this exception is thrown when an operation or query is performed on an admin user register application
* entity using an ID that does not correspond to an existing admin user register application.
*/
public class AysAdminUserRegisterApplicationNotExistByIdAndStatusException extends AysNotExistException {

/**
* Unique identifier for serialization.
*/
@Serial
private static final long serialVersionUID = 8056706849244878245L;

/**
* Constructs a new AysAdminUserRegisterApplicationNotExistByIdAndStatusException with the specified applicationId and applicationStatus.
*
* @param applicationId the applicationId of admin user register application.
* @param applicationStatus the applicationStatus of admin user register application.
*/
public AysAdminUserRegisterApplicationNotExistByIdAndStatusException(String applicationId, AdminUserRegisterApplicationStatus applicationStatus) {
super("ADMIN USER REGISTER APPLICATION NOT EXIST! id:" + applicationId + ", status:" + applicationStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity,
.authorizeHttpRequests(customizer -> customizer
.requestMatchers(HttpMethod.GET, "/public/**").permitAll()
.requestMatchers(HttpMethod.POST, "/api/v1/authentication/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/admin/registration-application/*/summary").permitAll()
.anyRequest().authenticated()
)
.sessionManagement(customizer -> customizer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequestBuilder;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationResponse;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationSummaryResponse;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationsResponse;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntity;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntityBuilder;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper;
import com.ays.admin_user.service.AdminUserRegisterApplicationService;
import com.ays.common.model.AysPage;
Expand Down Expand Up @@ -40,7 +42,7 @@ class AdminUserRegisterApplicationControllerTest extends AbstractRestControllerT
private final AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper = AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.initialize();
private final AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.initialize();
private final AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper.initialize();

private final AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.initialize();

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

Expand Down Expand Up @@ -164,4 +166,35 @@ void givenValidAdminUserRegisterApplicationId_whenUnauthorizedForGettingAdminUse
.doesNotExist());
}

@Test
void givenValidAdminUserRegisterApplicationId_whenAdminUserApplicationFound_thenReturnAdminUserApplicationSummaryResponse() throws Exception {

// Given
String mockId = AysRandomUtil.generateUUID();
AdminUserRegisterApplication mockAdminUserRegisterApplication = new AdminUserRegisterApplicationBuilder()
.withId(mockId)
.build();

// When
Mockito.when(adminUserRegisterApplicationService.getRegistrationApplicationSummaryById(mockId))
.thenReturn(mockAdminUserRegisterApplication);

// Then
String endpoint = BASE_PATH.concat("/registration-application/".concat(mockId).concat("/summary"));
AdminUserRegisterApplicationSummaryResponse mockResponse = adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.map(mockAdminUserRegisterApplication);
AysResponse<AdminUserRegisterApplicationSummaryResponse> mockAysResponse = AysResponse.successOf(mockResponse);
mockMvc.perform(AysMockMvcRequestBuilders
.get(endpoint))
.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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequestBuilder;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationResponse;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationSummaryResponse;
import com.ays.admin_user.model.entity.AdminUserEntity;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntity;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntityBuilder;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper;
import com.ays.common.model.AysPage;
import com.ays.common.model.dto.response.AysPageResponse;
import com.ays.common.model.dto.response.AysResponse;
Expand All @@ -32,7 +34,7 @@ class AdminUserRegisterApplicationSystemTest extends AbstractSystemTest {

private final AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper = AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.initialize();
private final AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationResponseMapper.initialize();

private final AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.initialize();

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

Expand Down Expand Up @@ -168,4 +170,41 @@ void givenValidAdminUserRegisterApplicationId_whenUnauthorizedForGettingAdminUse
.doesNotExist());
}

@Test
void givenValidAdminUserRegisterApplicationId_whenAdminUserApplicationFound_thenReturnAdminUserApplicationSummaryResponse() throws Exception {

// Initialize
AdminUserEntity mockAdminUserEntity = adminUserRepository.findById(AysValidTestData.AdminUser.ID)
.orElseThrow();
AdminUserRegisterApplicationEntity mockEntity = new AdminUserRegisterApplicationEntityBuilder()
.withValidFields()
.withAdminUserId(mockAdminUserEntity.getId())
.withAdminUser(mockAdminUserEntity)
.withInstitutionId(mockAdminUserEntity.getInstitutionId())
.withInstitution(mockAdminUserEntity.getInstitution())
.build();
this.initialize(mockEntity);

// Given
String mockId = mockEntity.getId();

// Then
String endpoint = BASE_PATH.concat("/registration-application/".concat(mockId).concat("/summary"));
AdminUserRegisterApplication mockAdminUserRegisterApplication = adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.map(mockEntity);
AdminUserRegisterApplicationSummaryResponse mockResponse = adminUserRegisterApplicationToAdminUserRegisterApplicationSummaryResponseMapper.map(mockAdminUserRegisterApplication);
AysResponse<AdminUserRegisterApplicationSummaryResponse> mockAysResponse = AysResponse.successOf(mockResponse);
mockMvc.perform(AysMockMvcRequestBuilders
.get(endpoint))
.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());
}

}
Loading

0 comments on commit 62fe3cc

Please sign in to comment.