Skip to content

Commit

Permalink
♻️ : extract ModuleMetadata class
Browse files Browse the repository at this point in the history
in order to avoid duplication
  • Loading branch information
juwit committed Jan 25, 2020
1 parent 9474f90 commit 563ed5a
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public String index(Model model, User user, Team userTeam){
toUpdateStackCount = this.stackRepository.countStacksByState(StackState.TO_UPDATE);
}
else if(userTeam != null){
moduleCount = this.moduleRepository.countByAuthorizedTeamsContainingOrCreatedBy(userTeam, user);
moduleCount = this.moduleRepository.countByAuthorizedTeamsContainingOrModuleMetadataCreatedBy(userTeam, user);
runningStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.RUNNING, userTeam);
toUpdateStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.TO_UPDATE, userTeam);
}
else {
moduleCount = this.moduleRepository.countByCreatedBy(user);
moduleCount = this.moduleRepository.countByModuleMetadataCreatedBy(user);
runningStackCount = stackRepository.countStacksByStateAndCreatedBy(StackState.RUNNING, user);
toUpdateStackCount = stackRepository.countStacksByStateAndCreatedBy(StackState.TO_UPDATE, user);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/codeka/gaia/modules/bo/ModuleMetadata.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.codeka.gaia.modules.bo

import io.codeka.gaia.teams.User
import org.springframework.data.mongodb.core.mapping.DBRef
import java.time.LocalDateTime

data class ModuleMetadata @JvmOverloads constructor(
val createdAt: LocalDateTime = LocalDateTime.now(),
@field:DBRef var createdBy: User? = null,
var updatedAt: LocalDateTime? = null,
@field:DBRef var updatedBy: User? = null
)
45 changes: 6 additions & 39 deletions src/main/java/io/codeka/gaia/modules/bo/TerraformModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -41,15 +40,7 @@ public class TerraformModule {

private String estimatedMonthlyCostDescription;

@DBRef
private User createdBy;

private LocalDateTime createdAt = LocalDateTime.now();

@DBRef
private User updatedBy;

private LocalDateTime updatedAt;
private ModuleMetadata moduleMetadata = new ModuleMetadata();

private RegistryDetails registryDetails;

Expand Down Expand Up @@ -126,7 +117,7 @@ public void setAuthorizedTeams(List<Team> authorizedTeams) {
}

public boolean isAuthorizedFor(User user) {
return user.isAdmin() || this.authorizedTeams.contains(user.getTeam()) || user.equals(this.createdBy);
return user.isAdmin() || this.authorizedTeams.contains(user.getTeam()) || user.equals(this.moduleMetadata.getCreatedBy());
}

public BigDecimal getEstimatedMonthlyCost() {
Expand All @@ -145,14 +136,6 @@ public void setEstimatedMonthlyCostDescription(String estimatedMonthlyCostDescri
this.estimatedMonthlyCostDescription = estimatedMonthlyCostDescription;
}

public User getCreatedBy() {
return createdBy;
}

public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}

public RegistryDetails getRegistryDetails() {
return registryDetails;
}
Expand All @@ -161,28 +144,12 @@ public void setRegistryDetails(RegistryDetails registryDetails) {
this.registryDetails = registryDetails;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public User getUpdatedBy() {
return updatedBy;
}

public void setUpdatedBy(User updatedBy) {
this.updatedBy = updatedBy;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
public ModuleMetadata getModuleMetadata() {
return moduleMetadata;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
public void setModuleMetadata(ModuleMetadata moduleMetadata) {
this.moduleMetadata = moduleMetadata;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public List<TerraformModule> findAllModules(User user){
return moduleRepository.findAll();
}
if(user.getTeam() != null){
return moduleRepository.findAllByCreatedByOrAuthorizedTeamsContaining(user, user.getTeam());
return moduleRepository.findAllByModuleMetadataCreatedByOrAuthorizedTeamsContaining(user, user.getTeam());
}
return moduleRepository.findAllByCreatedBy(user);
return moduleRepository.findAllByModuleMetadataCreatedBy(user);
}

@GetMapping("/{id}")
Expand All @@ -52,7 +52,7 @@ public TerraformModule findModule(@PathVariable String id, User user){
@ResponseStatus(HttpStatus.CREATED)
public TerraformModule createModule(@RequestBody TerraformModule module, User user){
module.setId(UUID.randomUUID().toString());
module.setCreatedBy(user);
module.getModuleMetadata().setCreatedBy(user);
return moduleRepository.save(module);
}

Expand All @@ -63,8 +63,8 @@ public TerraformModule saveModule(@PathVariable String id, @RequestBody @Valid T
throw new ModuleForbiddenException();
}

module.setUpdatedBy(user);
module.setUpdatedAt(LocalDateTime.now());
module.getModuleMetadata().setUpdatedBy(user);
module.getModuleMetadata().setUpdatedAt(LocalDateTime.now());

return moduleRepository.save(module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
@Repository
public interface TerraformModuleRepository extends MongoRepository<TerraformModule, String> {

int countByCreatedBy(User user);
int countByModuleMetadataCreatedBy(User user);

int countByAuthorizedTeamsContaining(Team team);

int countByAuthorizedTeamsContainingOrCreatedBy(Team team, User user);
int countByAuthorizedTeamsContainingOrModuleMetadataCreatedBy(Team team, User user);

List<TerraformModule> findAllByAuthorizedTeamsContainingOrCreatedBy(Team team, User user);
List<TerraformModule> findAllByAuthorizedTeamsContainingOrModuleMetadata_CreatedBy(Team team, User user);

Optional<TerraformModule> findByIdAndAuthorizedTeamsContaining(String id, Team team);

List<TerraformModule> findAllByCreatedByOrAuthorizedTeamsContaining(User user, Team team);
List<TerraformModule> findAllByModuleMetadataCreatedByOrAuthorizedTeamsContaining(User user, Team team);

List<TerraformModule> findAllByCreatedBy(User user);
List<TerraformModule> findAllByModuleMetadataCreatedBy(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class RegistryRawContent(private val registryType: RegistryType, privat
// no project details, impossible to load a readme, so returning empty
module.registryDetails ?: return Optional.empty()

val token = module.createdBy?.oAuth2User?.token;
val token = module.moduleMetadata.createdBy?.oAuth2User?.token;

val headers = HttpHeaders()
if(token != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.codeka.gaia.registries.controller

import io.codeka.gaia.hcl.HclParser
import io.codeka.gaia.modules.bo.ModuleMetadata
import io.codeka.gaia.modules.bo.TerraformModule
import io.codeka.gaia.modules.repository.TerraformCLIRepository
import io.codeka.gaia.modules.repository.TerraformModuleRepository
Expand Down Expand Up @@ -42,7 +43,7 @@ class GithubRegistryController(
module.cliVersion = cliRepository.listCLIVersion().first()

module.registryDetails = RegistryDetails(RegistryType.GITHUB, githubRepository.fullName)
module.createdBy = user
module.moduleMetadata = ModuleMetadata(createdBy = user)

// get variables
val variablesFile = githubRegistryApi.getFileContent(user, "$owner/$repo", "variables.tf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GitlabRegistryController(
module.cliVersion = cliRepository.listCLIVersion().first()

module.registryDetails = RegistryDetails(RegistryType.GITLAB, gitlabRepository.id)
module.createdBy = user
module.moduleMetadata.createdBy = user

// get variables
val variablesFile = gitlabRegistryApi.getFileContent(user, projectId, "variables.tf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class StackCommandBuilder {
*/
private String evalGitRepositoryUrl(TerraformModule module) {
var url = module.getGitRepositoryUrl();
var data = module.getCreatedBy().getOAuth2User();
var data = module.getModuleMetadata().getCreatedBy().getOAuth2User();
if (data == null) {
return url;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/module_description.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ <h1>
<div class="desc">{{description}}</div>
<hr>
<div class="metadata">
<p>Published <b>{{createdAt | dateTime}}</b> by <a href="#">{{createdBy.username}}</a></p>
<p v-if="updatedAt">Last modified <b>{{updatedAt | dateTime}}</b> by <a href="#">{{updatedBy.username}}</a></p>
<p>Published <b>{{moduleMetadata.createdAt | dateTime}}</b> by <a href="#">{{moduleMetadata.createdBy.username}}</a></p>
<p v-if="moduleMetadata.updatedAt">Last modified <b>{{moduleMetadata.updatedAt | dateTime}}</b> by <a href="#">{{moduleMetadata.updatedBy.username}}</a></p>
<p>Source: <a :href="gitRepositoryUrl">{{gitRepositoryUrl}}</a></p>
<p v-if="estimatedMonthlyCost">Estimated monthly cost: ${{estimatedMonthlyCost}}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
template: "#modules-template",
methods: {
isAuthorized(module){
return module.createdBy.username === this.$user.username
return module.moduleMetadata.createdBy.username === this.$user.username
|| this.$user.username === 'admin'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class IndexControllerTest {
@Test
void index_shouldShowModuleCount(){
// given
when(terraformModuleRepository.countByAuthorizedTeamsContainingOrCreatedBy(team, user)).thenReturn(12);
when(terraformModuleRepository.countByAuthorizedTeamsContainingOrModuleMetadataCreatedBy(team, user)).thenReturn(12);

// when
var result = indexController.index(model, user, team);

// then
assertEquals("index", result);
verify(terraformModuleRepository).countByAuthorizedTeamsContainingOrCreatedBy(team, user);
verify(terraformModuleRepository).countByAuthorizedTeamsContainingOrModuleMetadataCreatedBy(team, user);
verify(model).addAttribute("moduleCount", 12L);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ void index_shouldShowStacksCount_forAdmin(){
@Test
void usersWIthNoTeam_shouldSeeTheirCreatedModuleOrStacks(){
// given
doReturn(3).when(terraformModuleRepository).countByCreatedBy(user);
doReturn(3).when(terraformModuleRepository).countByModuleMetadataCreatedBy(user);
doReturn(1).when(stackRepository).countStacksByStateAndCreatedBy(StackState.RUNNING, user);
doReturn(2).when(stackRepository).countStacksByStateAndCreatedBy(StackState.TO_UPDATE, user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void module_shouldBeAuthorized_forTheModuleCreator(){
var daniel = new User("Daniel Jackson", sg1);

var module = new TerraformModule();
module.setCreatedBy(daniel);
module.getModuleMetadata().setCreatedBy(daniel);

// when
var authorized = module.isAuthorizedFor(daniel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ void findAll_shouldReturnAllModules_forAdmin(){
@Test
void findAll_shouldReturnAuthorizedModules_forUserTeam_andOwnedModules(){
// given
when(moduleRepository.findAllByCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam)).thenReturn(List.of(new TerraformModule()));
when(moduleRepository.findAllByModuleMetadataCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam)).thenReturn(List.of(new TerraformModule()));

// when
var modules = moduleRestController.findAllModules(bob);

// then
assertThat(modules).hasSize(1);
verify(moduleRepository).findAllByCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam);
verify(moduleRepository).findAllByModuleMetadataCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam);
}

@Test
Expand All @@ -74,7 +74,7 @@ void findAll_shouldReturnOwnedModules_forUserWithNoTeam(){
var modules = moduleRestController.findAllModules(john);

// then
verify(moduleRepository).findAllByCreatedBy(john);
verify(moduleRepository).findAllByModuleMetadataCreatedBy(john);
}

@Test
Expand All @@ -93,7 +93,7 @@ void findById_shouldReturnModule_forAdmin(){
void findById_shouldReturnOwnedModule_forUserWithNoTeam(){
// given
var ownedModule = new TerraformModule();
ownedModule.setCreatedBy(john);
ownedModule.getModuleMetadata().setCreatedBy(john);
when(moduleRepository.findById("12")).thenReturn(Optional.of(ownedModule));

// when
Expand Down Expand Up @@ -143,8 +143,8 @@ void findById_shouldThrowForbiddenModule_forUnauthorizedUsers(){
@Test
void save_shouldSaveTheModule_whenTheUserIsAuthorized(){
// given
var module = mock(TerraformModule.class);
when(module.isAuthorizedFor(bob)).thenReturn(true);
var module = new TerraformModule();
module.getModuleMetadata().setCreatedBy(bob);
when(moduleRepository.findById("12")).thenReturn(Optional.of(module));

// when
Expand All @@ -158,8 +158,8 @@ void save_shouldSaveTheModule_whenTheUserIsAuthorized(){
@Test
void save_shouldSaveTheModule_forTheAdminUser(){
// given
var module = mock(TerraformModule.class);
when(module.isAuthorizedFor(admin)).thenReturn(true);
var module = new TerraformModule();
module.getModuleMetadata().setCreatedBy(admin);
when(moduleRepository.findById("12")).thenReturn(Optional.of(module));

// when
Expand Down Expand Up @@ -191,23 +191,23 @@ void createModule_shouldSaveTheModule_forTheGivenUser(){

// then
verify(moduleRepository).save(module);
assertThat(module.getCreatedBy()).isEqualTo(bob);
assertThat(module.getModuleMetadata().getCreatedBy()).isEqualTo(bob);
assertThat(module.getId()).isNotBlank();
}

@Test
void updateModule_shouldSetUpdatedMetadata(){
// given
var module = mock(TerraformModule.class);
when(module.isAuthorizedFor(bob)).thenReturn(true);
var module = new TerraformModule();
module.getModuleMetadata().setCreatedBy(bob);
when(moduleRepository.findById("12")).thenReturn(Optional.of(module));

// when
moduleRestController.saveModule("12", module, bob);

// then
verify(module).setUpdatedAt(any(LocalDateTime.class));
verify(module).setUpdatedBy(bob);
assertThat(module.getModuleMetadata().getUpdatedAt()).isEqualToIgnoringMinutes(LocalDateTime.now());
assertThat(module.getModuleMetadata().getUpdatedBy()).isEqualTo(bob);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,36 @@ void setUp() {
module1 = new TerraformModule();
module1.setId("Module 1");
module1.setAuthorizedTeams(List.of(team1));
module1.setCreatedBy(bob);
module1.getModuleMetadata().setCreatedBy(bob);

module2 = new TerraformModule();
module2.setId("Module 2");
module2.setAuthorizedTeams(List.of(team1, team2));
module2.setCreatedBy(bob);
module2.getModuleMetadata().setCreatedBy(bob);

terraformModuleRepository.deleteAll();
terraformModuleRepository.saveAll(List.of(module1, module2));
}

@Test
void team1Users_shouldHaveAccessToModule1And2(){
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrCreatedBy(team1, null);
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrModuleMetadata_CreatedBy(team1, null);

assertThat(modules).hasSize(2);
}

@Test
void team2Users_shouldHaveAccessToModule2(){
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrCreatedBy(team2, null);
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrModuleMetadata_CreatedBy(team2, null);

assertThat(modules).hasSize(1);
}

@Test
void bob_shouldSeeItsModules(){
var modules = terraformModuleRepository.findAllByModuleMetadataCreatedBy(bob);

assertThat(modules).hasSize(2);
}

}
Loading

0 comments on commit 563ed5a

Please # to comment.