### Question:
AuthRestApi { @NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } } AuthRestApi(@NotNull NetworkClient client); void springAuth(@NotNull final String username,
                           @NotNull final String password,
                           final String organization,
                           final Map<String, String> params); @NotNull EncryptionKey requestEncryptionMetadata();  }### Answer:
@Test public void shouldReturnEncryptionKeyIfApiAvailable() throws Exception { MockResponse encryptionKey = MockResponseFactory.create200() .setBody(mKey.asString()); mWebMockRule.enqueue(encryptionKey); EncryptionKey keyResponse = apiUnderTest.requestEncryptionMetadata(); assertThat(keyResponse, Is.is(notNullValue())); }
@Test public void shouldReturnEmptyEncryptionKeyIfApiNotAvailable() throws Exception { String malformedJson = "{Error: Key generation is off}"; MockResponse encryptionKey = MockResponseFactory.create200() .setBody(malformedJson); mWebMockRule.enqueue(encryptionKey); EncryptionKey keyResponse = apiUnderTest.requestEncryptionMetadata(); assertThat(keyResponse.isAvailable(), Is.is(false)); }
@Test public void shouldReturnEmptyEncryptionKeyIfApiReturns401Error() throws Exception { MockResponse anonymousCookie = MockResponseFactory.create200() .setBody("6.1") .addHeader("Set-Cookie", "cookie1=12"); String malformedJson = "{Error: Key generation is off}"; MockResponse encryptionKey = MockResponseFactory.create401() .setBody(malformedJson); mWebMockRule.enqueue(anonymousCookie); mWebMockRule.enqueue(encryptionKey); EncryptionKey keyResponse = apiUnderTest.requestEncryptionMetadata(); assertThat(keyResponse.isAvailable(), is(false)); }