Skip to content

Commit

Permalink
Error on exception should be in form of Map
Browse files Browse the repository at this point in the history
  • Loading branch information
kritika-singh3 committed Jun 10, 2019
1 parent 61919d6 commit 669ce3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ protected GoPluginApiResponse execute(LookupSecretRequest lookupSecretsRequest)
Map<String, String> response = Collections.singletonMap("message", String.format("Secrets with keys %s not found.", unresolvedKeys));
return new DefaultGoPluginApiResponse(NOT_FOUND_ERROR_CODE, GSON.toJson(response));
} catch (IOException | GeneralSecurityException | BadSecretException e) {
return DefaultGoPluginApiResponse.error("Error while looking up secrets: " + e.getMessage());
Map<String, String> errorMessage = Collections.singletonMap("message", "Error while looking up secrets: " + e.getMessage());
return DefaultGoPluginApiResponse.error(GSON.toJson(errorMessage));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.skyscreamer.jsonassert.JSONAssert;

import java.io.File;
import java.io.IOException;
Expand All @@ -39,6 +38,7 @@
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;

class LookupSecretsRequestExecutorTest {

private File databaseFile;

@BeforeEach
Expand Down Expand Up @@ -87,4 +87,15 @@ void shouldReturnEmptyResponseWhenSecretsAreNotPresent() throws JSONException {
assertEquals("{\"message\":\"Secrets with keys [randomKey1, randomKey2] not found.\"}", response.responseBody(), true);
}

@Test
void shouldErrorAsAMapWhenAnyExceptionOccurs() throws JSONException {
GoPluginApiRequest goPluginApiRequest = mock(GoPluginApiRequest.class);
when(goPluginApiRequest.requestBody()).thenReturn(
new LookupSecretRequest("some-non-existent-file.db", Arrays.asList("randomKey1", "randomKey2")).toJSON());

GoPluginApiResponse response = new LookupSecretsRequestExecutor().execute(goPluginApiRequest);

assertThat(response.responseCode()).isEqualTo(500);
assertEquals("{\"message\":\"Error while looking up secrets: File \\u0027some-non-existent-file.db\\u0027 does not exist\"}", response.responseBody(), true);
}
}

0 comments on commit 669ce3a

Please sign in to comment.