Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

FLPATH-313: get workflow by projectId #294

Conversation

RichardW98
Copy link
Contributor

@RichardW98 RichardW98 commented May 1, 2023

FLPATH-313: get workflow by projectId

Change type

  • New feature
  • Bug fix
  • Unit tests
  • Integration tests
  • CI
  • Documentation
  • Auto generated SDK code

Impacted services

  • Workflow Service
  • Notifivcation Service

Checklist

  • Subject and description added to both, commit and PR.
  • Relevant issues have been referenced.

@openshift-ci-robot
Copy link
Collaborator

openshift-ci-robot commented May 1, 2023

@RichardW98: This pull request references FLPATH-313 which is a valid jira issue.

In response to this:

FLPATH-313: get workflow by projectId

Change type

  • New feature
  • Bug fix
  • Unit tests
  • Integration tests
  • CI
  • Documentation
  • Auto generated SDK code

Impacted services

  • Workflow Service
  • Notifivcation Service

Checklist

  • Subject and description added to both, commit and PR.
  • Relevant issues have been referenced.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci openshift-ci bot requested review from rgolangh and ydayagi May 1, 2023 19:48
@openshift-ci-robot
Copy link
Collaborator

openshift-ci-robot commented May 1, 2023

@RichardW98: This pull request references FLPATH-313 which is a valid jira issue.

In response to this:

FLPATH-313: get workflow by projectId

Change type

  • New feature
  • Bug fix
  • Unit tests
  • Integration tests
  • CI
  • Documentation
  • Auto generated SDK code

Impacted services

  • Workflow Service
  • Notifivcation Service

Checklist

  • Subject and description added to both, commit and PR.
  • Relevant issues have been referenced.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@RichardW98 RichardW98 requested review from anludke and wKich May 1, 2023 19:50
@@ -231,6 +234,22 @@ public void TestGetStatusWithValidData() throws Exception {
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
void TestGetStatusByProjectIdWithValidData() throws Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I miss the following test:

  • where checks that the project is nill? Does it return all projects?
  • What happens is getWorkFlowStatusByProjectId return an empty list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. yes it should return all projects which the user is allowed to check
  2. if it returns empty list then UI won't show any executions in the workflow table

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add tests for them

@@ -907,6 +909,27 @@ public void testGetWorkflowParametersWithoutWorkflowOptions() {
assertNull(workflowParameters.getWorkFlowOptions().getNewOptions());
}

@Test
void getWorkFlowStatusByProjectId_when_projectIsFound_then_returnWorkFlowStatus() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to increase testing when no workflows with valid projects and what happens when JPA repo returns nil

@Override
public List<WorkFlowStatusResponseDTO> getWorkFlowStatusByProjectId(UUID projectId) {
return workFlowRepository.findAllByProjectId(projectId).stream()
.map(workFlowExecution -> getWorkFlowStatus(workFlowExecution.getId())).toList();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the reason for this map?
Could you add a test for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

@eloycoto stream().map() allows applying a function to each stream element. So for me, it makes sense to iterate over the element and call getWorkflowStatus and then collect all results into a List.
However, I agree that a unit test for this method is valuable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ApiResponse(responseCode = "403", description = "Forbidden", content = @Content) })
@GetMapping("/status")
public ResponseEntity<List<WorkFlowStatusResponseDTO>> getStatusByProjectId(
@RequestParam("projectId") String projectId) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

projectId should be marked as @NotNull

@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content),
@ApiResponse(responseCode = "403", description = "Forbidden", content = @Content) })
@GetMapping("/status")
public ResponseEntity<List<WorkFlowStatusResponseDTO>> getStatusByProjectId(
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure what is the reason for it, but the generated openapi.json returns a string as a result while I'd expect the type of the return value to be a list.

however, if the project is mandatory in this context, I wonder why not expose the project-status endpoint instead.

if the purpose is to evaluate the project status, then it seems more natural to query for the project resource instead of the workflow, as project status might contain other information relevant to the project's overall status.

What will be the use of the new endpoint?

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this method return the list of the statuses of all the workflows available in a project (if I understood correctly), maybe renaming the method into getStatusesByProjectId can ease the understanding.

Copy link
Contributor Author

@RichardW98 RichardW98 May 2, 2023

Choose a reason for hiding this comment

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

@gciavarrini @masayag new UI design has replaced the project Overview with workflows Overview (https://www.figma.com/file/iu0HAOSoLOESA2GHyxuHLg/parados-mui?node-id=3640-3490). Therefore project-status will not be needed any more, and instead we will need to show all workflow executions' info by project. I will update this pr to include data other than status in the return (e.g. startTime, endTime ...). @wKich has a pr for this in UI: rhdhorchestrator/backstage-parodos#124. please review

projectId.toString()))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.jsonPath("$", hasSize(1))).andExpect(MockMvcResultMatchers
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if a formatter issue: can you move the last andExpect to a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea. it's formatter 😅

@Override
public List<WorkFlowStatusResponseDTO> getWorkFlowStatusByProjectId(UUID projectId) {
return workFlowRepository.findAllByProjectId(projectId).stream()
.map(workFlowExecution -> getWorkFlowStatus(workFlowExecution.getId())).toList();
Copy link
Contributor

Choose a reason for hiding this comment

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

@eloycoto stream().map() allows applying a function to each stream element. So for me, it makes sense to iterate over the element and call getWorkflowStatus and then collect all results into a List.
However, I agree that a unit test for this method is valuable.

@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content),
@ApiResponse(responseCode = "403", description = "Forbidden", content = @Content) })
@GetMapping("/status")
public ResponseEntity<List<WorkFlowStatusResponseDTO>> getStatusByProjectId(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this method return the list of the statuses of all the workflows available in a project (if I understood correctly), maybe renaming the method into getStatusesByProjectId can ease the understanding.

@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch 2 times, most recently from 7b81873 to 5ff2dcf Compare May 3, 2023 13:29
@@ -26,4 +29,8 @@
*/
public interface ProjectRepository extends JpaRepository<Project, UUID> {

List<Project> findAllByUserUsername(String username);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes Thank you for the example ~

@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch 2 times, most recently from ebc3aa0 to 7f3e4b0 Compare May 3, 2023 13:51
@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch from 7f3e4b0 to 7c4d6ef Compare May 3, 2023 15:13
@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch from 7c4d6ef to e7a540b Compare May 3, 2023 15:29
@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch from e7a540b to f17ccf1 Compare May 3, 2023 18:02
@RichardW98
Copy link
Contributor Author

I renamed 'user' table to 'users' temporarily, will fix it in another pr to standandize table names

@@ -33,4 +35,8 @@ public interface ProjectService {

List<ProjectResponseDTO> getProjects();

List<ProjectResponseDTO> findProjectsByUserName(String userName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

for consistency, let's keep userName in lowercase

ldapDetails = (LdapUserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return ldapDetails.getUsername();
}
catch (NullPointerException e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is expected to generate the NPE?
Could we replace this with if instead of try-catch?

Copy link
Collaborator

Choose a reason for hiding this comment

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

is NPE related to ldapDetails? If so we could check instead of handling this exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SecurityContextHolder.getContext().getAuthentication() might be null. I will change it to if/else

@@ -172,6 +185,26 @@ public synchronized WorkFlowExecution updateWorkFlow(WorkFlowExecution workFlowE
return workFlowRepository.save(workFlowExecution);
}

@Override
public List<WorkFlowResponseDTO> getWorkFlowsByProjectId(UUID projectId) {
List<ProjectResponseDTO> projects = projectId == null
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't it less predictable behavior? why not define projectId as @NotNull?
I'm not sure how users are created in the system, but what if the user has workflow in several projects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes then this controller will return all the workflows for this user, not limited to certain projects

Copy link
Collaborator

Choose a reason for hiding this comment

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

so I think we should separate it into two service call, since the controller makes the decision on which service call to use, based on the input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I will separate them

? projectService.findProjectsByUserName(SecurityUtils.getUsername())
: List.of(projectService.getProjectByIdAndUserName(projectId, SecurityUtils.getUsername()));
return projects.stream().flatMap(project -> workFlowRepository.findAllByProjectId(project.getId()).stream()
.filter(workFlowExecution -> workFlowExecution.getMainWorkFlowExecution() == null)
Copy link
Collaborator

Choose a reason for hiding this comment

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

a lot is going on in this method, but in terms of readability, I find its readability being somewhat complex.
do you think it can be refactored a bit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I will move out the WorkFlowResponseDTO builder

public class ProjectRepositoryTest extends RepositoryTestBase {

@Autowired
private TestEntityManager entityManager;
Copy link
Collaborator

Choose a reason for hiding this comment

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

entityManager is already defined in RepositoryTestBase as protected and being used by this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will clean this

@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch from f17ccf1 to ff031ca Compare May 4, 2023 17:46
@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch 2 times, most recently from c147156 to 9010b07 Compare May 4, 2023 18:03
@RichardW98 RichardW98 requested review from masayag and pkliczewski May 4, 2023 18:03
}

@Override
public List<WorkFlowResponseDTO> getWorkFlows() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps rename to getWorkFlowsForCurrentUser?
or to change the signature to accept username as a parameter and pass it from the caller (controller).

public class WorkFlowResponseDTO {

private UUID workFlowExecutionId;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String projectId;
Copy link
Collaborator

Choose a reason for hiding this comment

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

pls change to UUID.

@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch 2 times, most recently from 6d6d51e to f859af3 Compare May 4, 2023 19:09
@RichardW98 RichardW98 force-pushed the filter-workflow-by-projectid branch from f859af3 to 6a74b24 Compare May 4, 2023 19:26

// then
assertEquals(exception.getMessage(), String.format("Project with id: %s not found", project.getId()));
assertNotNull(res);
assertEquals(res.getId().toString(), project.getId().toString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

toString() can be removed from both sides

@openshift-ci
Copy link

openshift-ci bot commented May 4, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: masayag, wKich

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved label May 4, 2023
@masayag
Copy link
Collaborator

masayag commented May 4, 2023

/lgtm

@openshift-ci openshift-ci bot added the lgtm label May 4, 2023
@openshift-merge-robot openshift-merge-robot merged commit 717ba82 into rhdhorchestrator:main May 4, 2023
@RichardW98 RichardW98 deleted the filter-workflow-by-projectid branch May 8, 2023 15:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants