-
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
8 changed files
with
138 additions
and
4 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
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 @@ | ||
package com.flowci.flow.business; | ||
|
||
public interface InitRootGroup { | ||
void invoke(); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/flowci/flow/business/impl/InitRootGroupImpl.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,35 @@ | ||
package com.flowci.flow.business.impl; | ||
|
||
import com.flowci.common.model.Variables; | ||
import com.flowci.flow.business.InitRootGroup; | ||
import com.flowci.flow.model.Group; | ||
import com.flowci.flow.repo.GroupRepo; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor | ||
public class InitRootGroupImpl implements InitRootGroup { | ||
|
||
private final GroupRepo groupRepo; | ||
|
||
@Override | ||
public void invoke() { | ||
var optional = groupRepo.findById(Group.ROOT_ID); | ||
if (optional.isPresent()) { | ||
log.info("Root group '{}' already exists", Group.ROOT_NAME); | ||
return; | ||
} | ||
|
||
var root = new Group(); | ||
root.setName(Group.ROOT_NAME); | ||
root.setParentId(Group.ROOT_ID); | ||
root.setVariables(Variables.EMPTY); | ||
root.setCreatedBy("system"); | ||
root.setUpdatedBy("system"); | ||
groupRepo.save(root); | ||
log.info("Root group '{}' is created", root.getName()); | ||
} | ||
} |
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,9 @@ | ||
package com.flowci.flow.repo; | ||
|
||
import com.flowci.flow.model.Group; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface GroupRepo extends JpaRepository<Group, Long> { | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/test/java/com/flowci/flow/business/InitRootGroupTest.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,46 @@ | ||
package com.flowci.flow.business; | ||
|
||
import com.flowci.SpringTest; | ||
import com.flowci.flow.model.Group; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class InitRootGroupTest extends SpringTest { | ||
|
||
@Autowired | ||
private MockRepositoriesConfig repositoriesConfig; | ||
|
||
@Autowired | ||
private InitRootGroup initRootGroup; | ||
|
||
@Test | ||
void whenEmptyDatabase_thenDefaultRootGroupShouldBeCreated() { | ||
var groupRepoMock = repositoriesConfig.getGroupRepo(); | ||
when(groupRepoMock.findById(any())).thenReturn(Optional.empty()); | ||
|
||
var argCaptor = ArgumentCaptor.forClass(Group.class); | ||
when(groupRepoMock.save(argCaptor.capture())).thenReturn(mock(Group.class)); | ||
|
||
initRootGroup.invoke(); | ||
verify(groupRepoMock, times(1)).save(argCaptor.capture()); | ||
|
||
var groupParam = argCaptor.getValue(); | ||
assertEquals(Group.ROOT_NAME, groupParam.getName()); | ||
} | ||
|
||
@Test | ||
void whenRootGroupExisted_thenSkipToCreateIt() { | ||
var groupRepoMock = repositoriesConfig.getGroupRepo(); | ||
when(groupRepoMock.findById(any())).thenReturn(Optional.of(mock(Group.class))); | ||
|
||
initRootGroup.invoke(); | ||
verify(groupRepoMock, times(0)).save(any(Group.class)); | ||
} | ||
} |