Skip to content

Commit

Permalink
🐛 : fix README loading
Browse files Browse the repository at this point in the history
return a String instead of an Optional

resolves #365
  • Loading branch information
juwit committed Jul 22, 2020
1 parent 2710a97 commit 049320c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
Expand Down Expand Up @@ -76,9 +76,9 @@ public TerraformModule saveModule(@PathVariable String id, @RequestBody @Valid T
}

@GetMapping(value = "/{id}/readme", produces = MediaType.TEXT_PLAIN_VALUE)
public Optional<String> readme(@PathVariable String id) {
public String readme(@PathVariable String id) {
var module = moduleRepository.findById(id).orElseThrow();
return moduleGitRepository.getReadme(module);
return moduleGitRepository.getReadme(module).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND) );
}

}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/gaia_app/registries/RegistryRawContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.web.client.RestClientException
import org.springframework.web.client.RestTemplate
import java.util.*

Expand All @@ -19,20 +20,24 @@ abstract class RegistryRawContent(private val registryType: RegistryType, privat
val token = module.moduleMetadata.createdBy?.oAuth2User?.token;

val headers = HttpHeaders()
if(token != null) {
if (token != null) {
headers.add("Authorization", "Bearer $token")
}

val requestEntity = HttpEntity<Any>(headers)

val response = restTemplate.exchange(
try {
val response = restTemplate.exchange(
this.registryType.readmeUrl.replace("{id}", module.registryDetails.projectId),
HttpMethod.GET,
requestEntity,
RegistryFile::class.java)

if(response.statusCode == HttpStatus.OK) {
return Optional.of(String(Base64.getDecoder().decode(response.body?.content?.replace("\n",""))))
if (response.statusCode == HttpStatus.OK) {
return Optional.of(String(Base64.getDecoder().decode(response.body?.content?.replace("\n", ""))))
}
} catch (e: RestClientException) {
return Optional.empty()
}
return Optional.empty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void readme_shouldReturnReadmeContent() {
var result = moduleRestController.readme("TEST");

// then
assertThat(result).isPresent().get().isEqualTo(readme);
assertThat(result).isEqualTo(readme);
verify(moduleRepository).findById("TEST");
verify(moduleGitRepository).getReadme(module);
}
Expand Down

0 comments on commit 049320c

Please # to comment.