From ec6f1c0fa005e9e2afc45891a4ee0d27839c71e1 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Fri, 17 Jul 2020 16:23:50 +0200 Subject: [PATCH] :recycle: : add CredentialsService --- .../CredentialsRestController.java | 20 ++++----- .../credentials/CredentialsService.kt | 19 +++++++++ .../java/io/gaia_app/runner/StackRunner.java | 14 +++---- .../credentials/CredentialsServiceTest.kt | 41 +++++++++++++++++++ .../io/gaia_app/runner/StackRunnerTest.java | 17 ++++---- 5 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 src/main/java/io/gaia_app/credentials/CredentialsService.kt create mode 100644 src/test/java/io/gaia_app/credentials/CredentialsServiceTest.kt diff --git a/src/main/java/io/gaia_app/credentials/CredentialsRestController.java b/src/main/java/io/gaia_app/credentials/CredentialsRestController.java index 98c0cd151..a0741abce 100644 --- a/src/main/java/io/gaia_app/credentials/CredentialsRestController.java +++ b/src/main/java/io/gaia_app/credentials/CredentialsRestController.java @@ -13,43 +13,43 @@ @Secured({"ROLE_USER", "ROLE_ADMIN"}) public class CredentialsRestController { - private CredentialsRepository credentialsRepository; + private CredentialsService credentialsService; - public CredentialsRestController(CredentialsRepository credentialsRepository) { - this.credentialsRepository = credentialsRepository; + public CredentialsRestController(CredentialsService credentialsService) { + this.credentialsService = credentialsService; } @GetMapping public List getAllCredentials(User user) { - return this.credentialsRepository.findAllByCreatedBy(user); + return this.credentialsService.findAllByCreatedBy(user); } @GetMapping("/{id}") public Credentials getCredentials(@PathVariable String id, User user) { - return this.credentialsRepository.findByIdAndCreatedBy(id, user) + return this.credentialsService.findByIdAndCreatedBy(id, user) .orElseThrow(() -> new ResponseStatusException(HttpStatus.FORBIDDEN)); } @PostMapping public Credentials createCredentials(@RequestBody Credentials credentials) { - return this.credentialsRepository.save(credentials); + return this.credentialsService.save(credentials); } @PutMapping("/{id}") public Credentials updateCredentials(@RequestBody Credentials credentials, @PathVariable String id, User user) { // checking if we have the rights on this credentials - if (this.credentialsRepository.findByIdAndCreatedBy(id, user).isEmpty()) { + if (this.credentialsService.findByIdAndCreatedBy(id, user).isEmpty()) { throw new ResponseStatusException(HttpStatus.FORBIDDEN); } - return this.credentialsRepository.save(credentials); + return this.credentialsService.save(credentials); } @DeleteMapping("/{id}") public void deleteCredentials(@PathVariable String id, User user) { // checking if we have the rights on this credentials - if (this.credentialsRepository.findByIdAndCreatedBy(id, user).isEmpty()) { + if (this.credentialsService.findByIdAndCreatedBy(id, user).isEmpty()) { throw new ResponseStatusException(HttpStatus.FORBIDDEN); } - this.credentialsRepository.deleteById(id); + this.credentialsService.deleteById(id); } } diff --git a/src/main/java/io/gaia_app/credentials/CredentialsService.kt b/src/main/java/io/gaia_app/credentials/CredentialsService.kt new file mode 100644 index 000000000..3838d5b7d --- /dev/null +++ b/src/main/java/io/gaia_app/credentials/CredentialsService.kt @@ -0,0 +1,19 @@ +package io.gaia_app.credentials + +import io.gaia_app.teams.User +import org.springframework.stereotype.Service +import java.util.* + +@Service +class CredentialsService(val credentialsRepository: CredentialsRepository){ + + fun findById(id: String): Optional = this.credentialsRepository.findById(id) + + fun findByIdAndCreatedBy(id: String, createdBy: User) = credentialsRepository.findByIdAndCreatedBy(id, createdBy) + + fun findAllByCreatedBy(createdBy: User) = credentialsRepository.findAllByCreatedBy(createdBy) + + fun save(credentials: Credentials): Credentials = credentialsRepository.save(credentials) + + fun deleteById(id:String) = credentialsRepository.deleteById(id) +} diff --git a/src/main/java/io/gaia_app/runner/StackRunner.java b/src/main/java/io/gaia_app/runner/StackRunner.java index 1aaabf3cb..472b4003a 100644 --- a/src/main/java/io/gaia_app/runner/StackRunner.java +++ b/src/main/java/io/gaia_app/runner/StackRunner.java @@ -1,6 +1,6 @@ package io.gaia_app.runner; -import io.gaia_app.credentials.CredentialsRepository; +import io.gaia_app.credentials.CredentialsService; import io.gaia_app.modules.bo.TerraformModule; import io.gaia_app.stacks.bo.Job; import io.gaia_app.stacks.bo.JobType; @@ -32,19 +32,19 @@ public class StackRunner { private StackRepository stackRepository; private JobRepository jobRepository; private StepRepository stepRepository; - private CredentialsRepository credentialsRepository; + private CredentialsService credentialsService; private Map jobs = new HashMap<>(); @Autowired public StackRunner(DockerRunner dockerRunner, StackCommandBuilder stackCommandBuilder, - StackRepository stackRepository, JobRepository jobRepository, StepRepository stepRepository, CredentialsRepository credentialsRepository) { + StackRepository stackRepository, JobRepository jobRepository, StepRepository stepRepository, CredentialsService credentialsService) { this.dockerRunner = dockerRunner; this.stackCommandBuilder = stackCommandBuilder; this.stackRepository = stackRepository; this.jobRepository = jobRepository; this.stepRepository = stepRepository; - this.credentialsRepository = credentialsRepository; + this.credentialsService = credentialsService; } private String managePlanScript(Job job, Stack stack, TerraformModule module) { @@ -123,7 +123,7 @@ private void treatJob(JobWorkflow jobWorkflow, Consumer jobActionFn @Async public void plan(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) { if(stack.getCredentialsId() != null){ - jobWorkflow.getJob().setCredentials(this.credentialsRepository.findById(stack.getCredentialsId()).orElseThrow()); + jobWorkflow.getJob().setCredentials(this.credentialsService.findById(stack.getCredentialsId()).orElseThrow()); } treatJob( jobWorkflow, @@ -136,7 +136,7 @@ public void plan(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) { @Async public void apply(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) { if(stack.getCredentialsId() != null){ - jobWorkflow.getJob().setCredentials(this.credentialsRepository.findById(stack.getCredentialsId()).orElseThrow()); + jobWorkflow.getJob().setCredentials(this.credentialsService.findById(stack.getCredentialsId()).orElseThrow()); } treatJob( jobWorkflow, @@ -149,7 +149,7 @@ public void apply(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) @Async public void retry(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) { if(stack.getCredentialsId() != null){ - jobWorkflow.getJob().setCredentials(this.credentialsRepository.findById(stack.getCredentialsId()).orElseThrow()); + jobWorkflow.getJob().setCredentials(this.credentialsService.findById(stack.getCredentialsId()).orElseThrow()); } stepRepository.deleteByJobId(jobWorkflow.getJob().getId()); treatJob( diff --git a/src/test/java/io/gaia_app/credentials/CredentialsServiceTest.kt b/src/test/java/io/gaia_app/credentials/CredentialsServiceTest.kt new file mode 100644 index 000000000..a37105278 --- /dev/null +++ b/src/test/java/io/gaia_app/credentials/CredentialsServiceTest.kt @@ -0,0 +1,41 @@ +package io.gaia_app.credentials + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.Mockito.verify +import org.mockito.junit.jupiter.MockitoExtension +import java.util.* + +@ExtendWith(MockitoExtension::class) +class CredentialsServiceTest { + + @Mock + lateinit var credentialsRepository: CredentialsRepository + + @InjectMocks + lateinit var credentialsService: CredentialsService + + @Test + fun loadCredentials() { + val awsCredentials = AWSCredentials("access", "secret") + `when`(credentialsRepository.findById("aws")).thenReturn(Optional.of(awsCredentials)) + + val credentials = credentialsService.findById("aws") + assertThat(credentials).isEqualTo(Optional.of(awsCredentials)) + } + + @Test + fun saveCredentials() { + val awsCredentials = AWSCredentials("access", "secret") + `when`(credentialsRepository.save(awsCredentials)).thenReturn(awsCredentials) + + credentialsService.save(awsCredentials) + + verify(credentialsRepository).save(awsCredentials) + } + +} diff --git a/src/test/java/io/gaia_app/runner/StackRunnerTest.java b/src/test/java/io/gaia_app/runner/StackRunnerTest.java index c3d8431dc..290a87f93 100644 --- a/src/test/java/io/gaia_app/runner/StackRunnerTest.java +++ b/src/test/java/io/gaia_app/runner/StackRunnerTest.java @@ -1,8 +1,7 @@ package io.gaia_app.runner; import io.gaia_app.credentials.AWSCredentials; -import io.gaia_app.credentials.CredentialsRepository; -import io.gaia_app.credentials.EmptyCredentials; +import io.gaia_app.credentials.CredentialsService; import io.gaia_app.modules.bo.TerraformModule; import io.gaia_app.stacks.bo.Job; import io.gaia_app.stacks.bo.JobType; @@ -44,7 +43,7 @@ class StackRunnerTest { private StepRepository stepRepository; @Mock - private CredentialsRepository credentialsRepository; + private CredentialsService credentialsService; @Mock private JobWorkflow jobWorkflow; @@ -406,13 +405,13 @@ void credentialsShouldBeInjected_whenRunningAPlan(){ stack.setCredentialsId("dummy"); var credentials = new AWSCredentials("dummy","dummy"); - when(credentialsRepository.findById("dummy")).thenReturn(Optional.of(credentials)); + when(credentialsService.findById("dummy")).thenReturn(Optional.of(credentials)); // when stackRunner.plan(jobWorkflow, module, stack); // then - verify(credentialsRepository).findById("dummy"); + verify(credentialsService).findById("dummy"); assertThat(job.getCredentials()).isEqualTo(credentials); } @@ -422,13 +421,13 @@ void credentialsShouldBeInjected_whenRunningAnApply(){ stack.setCredentialsId("dummy"); var credentials = new AWSCredentials("dummy","dummy"); - when(credentialsRepository.findById("dummy")).thenReturn(Optional.of(credentials)); + when(credentialsService.findById("dummy")).thenReturn(Optional.of(credentials)); // when stackRunner.apply(jobWorkflow, module, stack); // then - verify(credentialsRepository).findById("dummy"); + verify(credentialsService).findById("dummy"); assertThat(job.getCredentials()).isEqualTo(credentials); } @@ -438,13 +437,13 @@ void credentialsShouldBeInjected_whenRunningARetry(){ stack.setCredentialsId("dummy"); var credentials = new AWSCredentials("dummy","dummy"); - when(credentialsRepository.findById("dummy")).thenReturn(Optional.of(credentials)); + when(credentialsService.findById("dummy")).thenReturn(Optional.of(credentials)); // when stackRunner.retry(jobWorkflow, module, stack); // then - verify(credentialsRepository).findById("dummy"); + verify(credentialsService).findById("dummy"); assertThat(job.getCredentials()).isEqualTo(credentials); }