-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#235 | Institution Manager Application Management | Institution Summa…
…ry Service (#251) Co-authored-by: AhmetAksunger <[email protected]> Co-authored-by: Emre Yılmaz <[email protected]>
- Loading branch information
1 parent
c4dd8d8
commit 9746e7a
Showing
16 changed files
with
542 additions
and
1 deletion.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/com/ays/institution/controller/InstitutionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.ays.institution.controller; | ||
|
||
import com.ays.common.model.dto.response.AysResponse; | ||
import com.ays.institution.model.Institution; | ||
import com.ays.institution.model.dto.response.InstitutionResponse; | ||
import com.ays.institution.model.dto.response.InstitutionsSummaryResponse; | ||
import com.ays.institution.model.mapper.InstitutionToInstitutionsSummaryResponseMapper; | ||
import com.ays.institution.service.InstitutionService; | ||
import lombok.RequiredArgsConstructor; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* REST controller class for managing institution-related operations via HTTP requests. | ||
* This controller handles the business operations for institutions in the system. | ||
* The mapping path for this controller is "/api/v1/institutions". | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
@Validated | ||
class InstitutionController { | ||
|
||
private final InstitutionService institutionService; | ||
|
||
private final InstitutionToInstitutionsSummaryResponseMapper institutionToInstitutionsSummaryResponseMapper = InstitutionToInstitutionsSummaryResponseMapper.initialize(); | ||
|
||
/** | ||
* Retrieves a summary of all institutions. | ||
* Requires the user to have the 'SUPER_ADMIN' authority. | ||
* | ||
* @return An {@link AysResponse} containing a list of {@link InstitutionResponse} representing the summary of institutions. | ||
*/ | ||
@GetMapping("/institutions/summary") | ||
@PreAuthorize("hasAnyAuthority('SUPER_ADMIN')") | ||
public AysResponse<List<InstitutionsSummaryResponse>> getSummaryOfActiveInstitutions() { | ||
|
||
final List<Institution> institutionsSummary = institutionService.getSummaryOfActiveInstitutions(); | ||
final List<InstitutionsSummaryResponse> institutionsSummaryRespons = institutionToInstitutionsSummaryResponseMapper.map(institutionsSummary); | ||
return AysResponse.successOf(institutionsSummaryRespons); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ays/institution/model/dto/response/InstitutionsSummaryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ays.institution.model.dto.response; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* This class represents the response for a single institution. | ||
* It includes information such as the institution's id and name. | ||
*/ | ||
@Getter | ||
@Setter | ||
public class InstitutionsSummaryResponse { | ||
|
||
private String id; | ||
private String name; | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/ays/institution/model/mapper/InstitutionEntityToInstitutionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ays.institution.model.mapper; | ||
|
||
import com.ays.common.model.mapper.BaseMapper; | ||
import com.ays.institution.model.Institution; | ||
import com.ays.institution.model.entity.InstitutionEntity; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* InstitutionEntityToInstitutionMapper is an interface that defines the mapping between an {@link InstitutionEntity} and an {@link Institution}. | ||
* 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 InstitutionEntityToInstitutionMapper extends BaseMapper<InstitutionEntity, Institution> { | ||
|
||
/** | ||
* Initializes the mapper. | ||
* | ||
* @return the initialized mapper object. | ||
*/ | ||
static InstitutionEntityToInstitutionMapper initialize() { | ||
return Mappers.getMapper(InstitutionEntityToInstitutionMapper.class); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...java/com/ays/institution/model/mapper/InstitutionToInstitutionsSummaryResponseMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ays.institution.model.mapper; | ||
|
||
import com.ays.common.model.mapper.BaseMapper; | ||
import com.ays.institution.model.Institution; | ||
import com.ays.institution.model.dto.response.InstitutionsSummaryResponse; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* InstitutionToInstitutionSummaryResponseMapper is an interface that defines the mapping between an {@link Institution} and an {@link InstitutionsSummaryResponse}. | ||
* 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 InstitutionToInstitutionsSummaryResponseMapper extends BaseMapper<Institution, InstitutionsSummaryResponse> { | ||
|
||
/** | ||
* Initializes the mapper. | ||
* | ||
* @return the initialized mapper object. | ||
*/ | ||
static InstitutionToInstitutionsSummaryResponseMapper initialize() { | ||
return Mappers.getMapper(InstitutionToInstitutionsSummaryResponseMapper.class); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/ays/institution/repository/InstitutionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
package com.ays.institution.repository; | ||
|
||
import com.ays.institution.model.entity.InstitutionEntity; | ||
import com.ays.institution.model.enums.InstitutionStatus; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An interface for accessing and managing institutions in a data source with CRUD operations. | ||
* It extends the JpaRepository interface with InstitutionEntity as the entity type and String as the ID type. | ||
* The default behavior of the repository can be extended by adding custom methods to this interface. | ||
*/ | ||
public interface InstitutionRepository extends JpaRepository<InstitutionEntity, String> { | ||
|
||
/** | ||
* Find all institutions by status | ||
* | ||
* @param status the status of the institutions to retrieve | ||
* @return the list of institutions | ||
*/ | ||
List<InstitutionEntity> findAllByStatusOrderByNameAsc(InstitutionStatus status); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/ays/institution/service/InstitutionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ays.institution.service; | ||
|
||
import com.ays.institution.model.Institution; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Institution Service to perform institution related business operations. | ||
*/ | ||
public interface InstitutionService { | ||
|
||
/** | ||
* Get all active institutions summary | ||
* | ||
* @return list of active institutions | ||
*/ | ||
List<Institution> getSummaryOfActiveInstitutions(); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ays/institution/service/impl/InstitutionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.ays.institution.service.impl; | ||
|
||
import com.ays.institution.model.Institution; | ||
import com.ays.institution.model.entity.InstitutionEntity; | ||
import com.ays.institution.model.enums.InstitutionStatus; | ||
import com.ays.institution.model.mapper.InstitutionEntityToInstitutionMapper; | ||
import com.ays.institution.repository.InstitutionRepository; | ||
import com.ays.institution.service.InstitutionService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Implementation of the {@link InstitutionService} interface for performing institution-related business operations. | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
class InstitutionServiceImpl implements InstitutionService { | ||
|
||
private final InstitutionRepository institutionRepository; | ||
|
||
private final InstitutionEntityToInstitutionMapper institutionEntityToInstitutionMapper = InstitutionEntityToInstitutionMapper.initialize(); | ||
|
||
/** | ||
* Retrieves a summary of all active institutions. | ||
* | ||
* @return a list of {@link Institution} representing the summary of institutions | ||
*/ | ||
@Override | ||
public List<Institution> getSummaryOfActiveInstitutions() { | ||
final List<InstitutionEntity> activeInstitutions = institutionRepository.findAllByStatusOrderByNameAsc(InstitutionStatus.ACTIVE); | ||
return institutionEntityToInstitutionMapper.map(activeInstitutions); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/com/ays/institution/controller/InstitutionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.ays.institution.controller; | ||
|
||
import com.ays.AbstractRestControllerTest; | ||
import com.ays.common.model.dto.response.AysResponse; | ||
import com.ays.common.model.dto.response.AysResponseBuilder; | ||
import com.ays.common.util.exception.model.AysError; | ||
import com.ays.institution.model.Institution; | ||
import com.ays.institution.model.dto.response.InstitutionsSummaryResponse; | ||
import com.ays.institution.model.entity.InstitutionBuilder; | ||
import com.ays.institution.model.mapper.InstitutionToInstitutionsSummaryResponseMapper; | ||
import com.ays.institution.service.InstitutionService; | ||
import com.ays.util.AysMockMvcRequestBuilders; | ||
import com.ays.util.AysMockResultMatchersBuilders; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
|
||
import java.util.List; | ||
|
||
class InstitutionControllerTest extends AbstractRestControllerTest { | ||
|
||
@MockBean | ||
private InstitutionService institutionService; | ||
|
||
|
||
private final InstitutionToInstitutionsSummaryResponseMapper institutionToInstitutionsSummaryResponseMapper = InstitutionToInstitutionsSummaryResponseMapper.initialize(); | ||
|
||
|
||
private static final String BASE_PATH = "/api/v1/institutions"; | ||
|
||
@Test | ||
void whenInstitutionStatusActive_thenReturnListInstitutionResponse() throws Exception { | ||
|
||
// When | ||
List<Institution> mockActiveInstitutions = List.of( | ||
new InstitutionBuilder().withValidFields().build(), | ||
new InstitutionBuilder().withValidFields().build() | ||
); | ||
|
||
Mockito.when(institutionService.getSummaryOfActiveInstitutions()).thenReturn(mockActiveInstitutions); | ||
|
||
// Then | ||
List<InstitutionsSummaryResponse> mockActiveInstitutionsSummaryResponses = institutionToInstitutionsSummaryResponseMapper.map(mockActiveInstitutions); | ||
AysResponse<List<InstitutionsSummaryResponse>> mockAysResponse = AysResponse.successOf(mockActiveInstitutionsSummaryResponses); | ||
|
||
mockMvc.perform(AysMockMvcRequestBuilders | ||
.get(BASE_PATH.concat("/summary"), mockSuperAdminToken.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()); | ||
|
||
} | ||
|
||
@Test | ||
void whenUserUnauthorizedForSummary_thenReturnAccessDeniedException() throws Exception { | ||
|
||
// When | ||
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders | ||
.get(BASE_PATH.concat("/summary"), mockUserToken.getAccessToken()); | ||
|
||
// Then | ||
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.