Skip to content

Commit

Permalink
♻️ : add CredentialsService
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jul 30, 2020
1 parent e3d2273 commit ec6f1c0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<EmptyCredentials> 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);
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/gaia_app/credentials/CredentialsService.kt
Original file line number Diff line number Diff line change
@@ -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<Credentials> = 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)
}
14 changes: 7 additions & 7 deletions src/main/java/io/gaia_app/runner/StackRunner.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,19 +32,19 @@ public class StackRunner {
private StackRepository stackRepository;
private JobRepository jobRepository;
private StepRepository stepRepository;
private CredentialsRepository credentialsRepository;
private CredentialsService credentialsService;

private Map<String, Job> 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) {
Expand Down Expand Up @@ -123,7 +123,7 @@ private void treatJob(JobWorkflow jobWorkflow, Consumer<JobWorkflow> 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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/io/gaia_app/credentials/CredentialsServiceTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}

}
17 changes: 8 additions & 9 deletions src/test/java/io/gaia_app/runner/StackRunnerTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,7 +43,7 @@ class StackRunnerTest {
private StepRepository stepRepository;

@Mock
private CredentialsRepository credentialsRepository;
private CredentialsService credentialsService;

@Mock
private JobWorkflow jobWorkflow;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit ec6f1c0

Please sign in to comment.