Skip to content

Commit

Permalink
πŸ› : correct NPE when stack has no credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Aug 24, 2020
1 parent 3bfcfd4 commit e1c28a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ public Map<String, String> launchJob(@PathVariable String id, @PathVariable JobT
// create a new job
var job = new Job(jobType, id, user);
job.setTerraformImage(stack.getModule().getTerraformImage());
this.credentialsRepository.findById(stack.getCredentialsId())
.ifPresent(job::setCredentials);
if(stack.getCredentialsId() != null){
this.credentialsRepository.findById(stack.getCredentialsId())
.ifPresent(job::setCredentials);
}
jobRepository.save(job);

return Map.of("jobId", job.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gaia_app.stacks.controller;

import io.gaia_app.credentials.AWSCredentials;
import io.gaia_app.credentials.CredentialsRepository;
import io.gaia_app.modules.bo.TerraformImage;
import io.gaia_app.modules.bo.TerraformModule;
Expand Down Expand Up @@ -201,4 +202,37 @@ void launchJob_shouldConfigureAndSaveTheJob() {
assertEquals(module.getTerraformImage(), job.getTerraformImage());
}

@Test
void launchJob_shouldInjectCredentialsIntoJob() {
// given
var stack = new Stack();
stack.setCredentialsId("123456");
var module = new TerraformModule();
module.setTerraformImage(TerraformImage.Companion.defaultInstance());
stack.setModule(module);
var user = new User("test_user", null);

AWSCredentials awsCredentials = new AWSCredentials("a", "s");
when(credentialsRepository.findById("123456")).thenReturn(Optional.of(awsCredentials));

// when
when(stackRepository.findById(anyString())).thenReturn(Optional.of(stack));
var result = stackRestController.launchJob("test_stack", JobType.RUN, user);

// then
assertThat(result)
.isNotNull()
.containsKeys("jobId");

var captor = forClass(Job.class);
verify(jobRepository).save(captor.capture());
var job = captor.getValue();
assertNotNull(job);
assertEquals(JobType.RUN, job.getType());
assertEquals("test_stack", job.getStackId());
assertEquals(user, job.getUser());
assertEquals(module.getTerraformImage(), job.getTerraformImage());
assertEquals(job.getCredentials(), awsCredentials);
}

}

0 comments on commit e1c28a9

Please sign in to comment.