-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
168 additions
and
23 deletions.
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
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,38 @@ | ||
package com.flowci.common.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Constraint(validatedBy = ValidId.IdValidator.class) | ||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface ValidId { | ||
|
||
String message() default "invalid id"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
class IdValidator implements ConstraintValidator<ValidId, String> { | ||
|
||
@Override | ||
public void initialize(ValidId constraintAnnotation) { | ||
ConstraintValidator.super.initialize(constraintAnnotation); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
try { | ||
return Long.parseLong(value) > 0L; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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
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
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
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,50 @@ | ||
package com.flowci.flow.business; | ||
|
||
import com.flowci.SpringTest; | ||
import com.flowci.common.RequestContextHolder; | ||
import com.flowci.flow.model.Flow; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class ListFlowsTest extends SpringTest { | ||
|
||
@Autowired | ||
private MockRepositoriesConfig mockRepositoriesConfig; | ||
|
||
@Autowired | ||
private ListFlows listFlows; | ||
|
||
@MockBean | ||
private RequestContextHolder requestContextHolder; | ||
|
||
@Test | ||
void givenParentId_whenFetching_thenReturnAllFlowsUnderTheParent() { | ||
var flowRepoMock = mockRepositoriesConfig.getFlowRepo(); | ||
var parentIdCaptor = ArgumentCaptor.forClass(Long.class); | ||
var userIdCaptor = ArgumentCaptor.forClass(Long.class); | ||
when(flowRepoMock.findAllByParentIdAndUserIdOrderByCreatedAt( | ||
parentIdCaptor.capture(), | ||
userIdCaptor.capture(), | ||
any(Pageable.class)) | ||
).thenReturn(Collections.emptyList()); | ||
|
||
var userIdMock = 1L; | ||
when(requestContextHolder.getUserId()).thenReturn(userIdMock); | ||
|
||
listFlows.invoke(null, PageRequest.of(0, 1)); | ||
|
||
assertEquals(Flow.ROOT_ID, parentIdCaptor.getValue()); | ||
verify(flowRepoMock, times(1)).findAllByParentIdAndUserIdOrderByCreatedAt( | ||
parentIdCaptor.capture(), userIdCaptor.capture(), any(Pageable.class)); | ||
} | ||
} |