-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDEXP-110 - Return list of users who run the completed jobs (#441)
* MDEXP-110 Squashed commit
- Loading branch information
1 parent
1ed5a42
commit 4435df4
Showing
8 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
src/main/java/org/folio/dataexp/controllers/RelatedUsersController.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,43 @@ | ||
package org.folio.dataexp.controllers; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.dataexp.domain.dto.JobExecution; | ||
import org.folio.dataexp.domain.dto.RelatedUser; | ||
import org.folio.dataexp.domain.dto.RelatedUserCollection; | ||
import org.folio.dataexp.domain.entity.JobExecutionEntity; | ||
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.folio.dataexp.rest.resource.RelatedUsersApi; | ||
|
||
import java.util.ArrayList; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Log4j2 | ||
@RequestMapping("/data-export") | ||
public class RelatedUsersController implements RelatedUsersApi { | ||
|
||
private final JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository; | ||
|
||
@Override | ||
public ResponseEntity<RelatedUserCollection> getRelatedUsers() { | ||
log.info("GET related users"); | ||
var relatedUsers = jobExecutionEntityCqlRepository.findAll().stream() | ||
.map(JobExecutionEntity::getJobExecution) | ||
.filter(job -> nonNull(job.getRunBy())) | ||
.map(JobExecution::getRunBy) | ||
.map(runBy -> new RelatedUser().userId(runBy.getUserId()).firstName(runBy.getFirstName()).lastName(runBy.getLastName())) | ||
.collect(Collectors.toSet()); | ||
log.info("Related users size: {}", relatedUsers.size()); | ||
var relatedUserCollection = new RelatedUserCollection().relatedUsers(new ArrayList<>(relatedUsers)) | ||
.totalRecords(relatedUsers.size()); | ||
return new ResponseEntity<>(relatedUserCollection, HttpStatus.OK); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"userId": "6b380aa8-4070-4497-a466-22345fde64c0" | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/resources/swagger.api/examples/relatedUserCollection.json
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,10 @@ | ||
{ | ||
"relatedUsers":[ | ||
{ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"userId": "6b380aa8-4070-4497-a466-22345fde64c0" | ||
} | ||
], | ||
"totalRecords": 1 | ||
} |
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 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"description": "Related user who have ever started export", | ||
"properties": { | ||
"firstName": { | ||
"description": "First name", | ||
"type": "string" | ||
}, | ||
"lastName": { | ||
"description": "Last name", | ||
"type": "string" | ||
}, | ||
"userId": { | ||
"description": "User id", | ||
"type": "string" | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/resources/swagger.api/schemas/relatedUserCollection.json
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,20 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Collection of related users", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"relatedUsers": { | ||
"description": "List of related users", | ||
"type": "array", | ||
"id": "relatedUsersList", | ||
"items": { | ||
"type": "object", | ||
"$ref": "relatedUser.json" | ||
} | ||
}, | ||
"totalRecords": { | ||
"type": "integer" | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/org/folio/dataexp/controllers/RelatedUsersControllerTest.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,68 @@ | ||
package org.folio.dataexp.controllers; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import lombok.SneakyThrows; | ||
import org.folio.dataexp.BaseDataExportInitializer; | ||
import org.folio.dataexp.domain.dto.JobExecution; | ||
import org.folio.dataexp.domain.dto.JobExecutionRunBy; | ||
import org.folio.dataexp.domain.entity.JobExecutionEntity; | ||
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository; | ||
import org.folio.spring.scope.FolioExecutionContextSetter; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
class RelatedUsersControllerTest extends BaseDataExportInitializer { | ||
|
||
@Autowired | ||
private JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository; | ||
|
||
@Test | ||
@SneakyThrows | ||
void getRelatedUsersTest() { | ||
|
||
try (var context = new FolioExecutionContextSetter(folioExecutionContext)) { | ||
|
||
var userId = UUID.randomUUID(); | ||
var userId2 = UUID.randomUUID(); | ||
|
||
var entities = List.of( | ||
new JobExecutionEntity().withJobExecution(new JobExecution().runBy( | ||
new JobExecutionRunBy().userId(userId.toString()).firstName("first name").lastName("last name"))) | ||
.withId(UUID.randomUUID()), | ||
new JobExecutionEntity().withJobExecution(new JobExecution().runBy( | ||
new JobExecutionRunBy().userId(userId2.toString()).firstName("first name 2").lastName("last name 2"))) | ||
.withId(UUID.randomUUID()), | ||
new JobExecutionEntity().withJobExecution(new JobExecution().runBy( | ||
new JobExecutionRunBy().userId(userId.toString()).firstName("first name").lastName("last name"))) | ||
.withId(UUID.randomUUID())); | ||
|
||
jobExecutionEntityCqlRepository.saveAll(entities); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders | ||
.get("/data-export/related-users") | ||
.headers(defaultHeaders()) | ||
.contentType(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json("{\n" + | ||
" \"relatedUsers\": [{\n" + | ||
" \"firstName\": \"first name\",\n" + | ||
" \"lastName\": \"last name\",\n" + | ||
" \"userId\": \"" + userId + "\"\n" + | ||
" }," + | ||
"{\n" + | ||
" \"firstName\": \"first name 2\",\n" + | ||
" \"lastName\": \"last name 2\",\n" + | ||
" \"userId\": \"" + userId2 + "\"\n" + | ||
" }],\n" + | ||
" \"totalRecords\": 2\n" + | ||
"}")); | ||
} | ||
} | ||
} |