-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(MonitoredDeployBaseTask): Add tests to verify log message for di…
…fferent types of RetrofitError (#4608) This commit introduces a set of test cases aimed at showcasing the current behavior of the getRetrofitLogMessage() method when different types of RetrofitErrors, such as HTTP Error, Network Error, Unexpected Error, and Conversion Error, are thrown. The purpose of these test cases is to establish a baseline understanding of the method's behavior in handling various RetrofitError scenarios before the upcoming modifications. These tests will serve as a reference point to compare and validate the behavior of the code after enhancements are made, specifically focusing on eliminating the usage of RetrofitError. These test cases provide valuable insight into the existing behavior of the getRetrofitLogMessage() method and will aid in assessing the effectiveness of subsequent modifications.
- Loading branch information
1 parent
276114b
commit adc81ac
Showing
2 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...netflix/spinnaker/orca/clouddriver/tasks/monitoreddeploy/MonitoredDeployBaseTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Copyright 2023 OpsMx, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.clouddriver.tasks.monitoreddeploy; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.spectator.api.NoopRegistry; | ||
import com.netflix.spinnaker.config.DeploymentMonitorDefinition; | ||
import com.netflix.spinnaker.orca.deploymentmonitor.DeploymentMonitorServiceProvider; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import retrofit.RequestInterceptor; | ||
import retrofit.RestAdapter; | ||
import retrofit.RetrofitError; | ||
import retrofit.client.Header; | ||
import retrofit.client.OkClient; | ||
import retrofit.client.Response; | ||
import retrofit.converter.ConversionException; | ||
import retrofit.converter.Converter; | ||
import retrofit.converter.JacksonConverter; | ||
import retrofit.mime.TypedInput; | ||
|
||
public class MonitoredDeployBaseTaskTest { | ||
|
||
private MonitoredDeployBaseTask monitoredDeployBaseTask; | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@BeforeEach | ||
void setup() { | ||
OkClient okClient = new OkClient(); | ||
RestAdapter.LogLevel retrofitLogLevel = RestAdapter.LogLevel.NONE; | ||
|
||
RequestInterceptor requestInterceptor = request -> {}; | ||
DeploymentMonitorDefinition deploymentMonitorDefinition = new DeploymentMonitorDefinition(); | ||
deploymentMonitorDefinition.setId("LogMonitorId"); | ||
deploymentMonitorDefinition.setName("LogMonitor"); | ||
deploymentMonitorDefinition.setFailOnError(true); | ||
var deploymentMonitorDefinitions = new ArrayList<DeploymentMonitorDefinition>(); | ||
deploymentMonitorDefinitions.add(deploymentMonitorDefinition); | ||
|
||
DeploymentMonitorServiceProvider deploymentMonitorServiceProvider = | ||
new DeploymentMonitorServiceProvider( | ||
okClient, retrofitLogLevel, requestInterceptor, deploymentMonitorDefinitions); | ||
monitoredDeployBaseTask = | ||
new MonitoredDeployBaseTask(deploymentMonitorServiceProvider, new NoopRegistry()); | ||
} | ||
|
||
@Test | ||
public void shouldParseHttpErrorResponseDetailsWhenHttpErrorHasOccurred() { | ||
|
||
var converter = new JacksonConverter(objectMapper); | ||
var responseBody = new HashMap<String, String>(); | ||
var headers = new ArrayList<Header>(); | ||
var header = new Header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
|
||
headers.add(header); | ||
responseBody.put("error", "400 - Bad request, application name cannot be empty"); | ||
|
||
Response response = | ||
new Response( | ||
"/deployment/evaluateHealth", | ||
HttpStatus.BAD_REQUEST.value(), | ||
HttpStatus.BAD_REQUEST.name(), | ||
headers, | ||
new MockTypedInput(converter, responseBody)); | ||
|
||
RetrofitError httpError = | ||
RetrofitError.httpError( | ||
"https://foo.com/deployment/evaluateHealth", response, new JacksonConverter(), null); | ||
|
||
String logMessageOnHttpError = | ||
monitoredDeployBaseTask.getRetrofitLogMessage(httpError.getResponse()); | ||
String status = HttpStatus.BAD_REQUEST.value() + " (" + HttpStatus.BAD_REQUEST.name() + ")"; | ||
String body = "{\"error\":\"400 - Bad request, application name cannot be empty\"}"; | ||
|
||
assertThat(logMessageOnHttpError) | ||
.isEqualTo( | ||
String.format("status: %s\nheaders: %s\nresponse body: %s", status, header, body)); | ||
} | ||
|
||
@Test | ||
public void shouldParseHttpErrorResponseDetailsWhenConversionErrorHasOccurred() { | ||
|
||
var converter = new JacksonConverter(objectMapper); | ||
var responseBody = new HashMap<String, String>(); | ||
var headers = new ArrayList<Header>(); | ||
var header = new Header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
|
||
headers.add(header); | ||
responseBody.put("error", "400 - Bad request, application name cannot be empty"); | ||
|
||
Response response = | ||
new Response( | ||
"/deployment/evaluateHealth", | ||
HttpStatus.BAD_REQUEST.value(), | ||
HttpStatus.BAD_REQUEST.name(), | ||
headers, | ||
new MockTypedInput(converter, responseBody)); | ||
|
||
RetrofitError conversionError = | ||
RetrofitError.conversionError( | ||
"https://foo.com/deployment/evaluateHealth", | ||
response, | ||
new JacksonConverter(), | ||
null, | ||
new ConversionException("Failed to parse response")); | ||
|
||
String status = HttpStatus.BAD_REQUEST.value() + " (" + HttpStatus.BAD_REQUEST.name() + ")"; | ||
String body = "{\"error\":\"400 - Bad request, application name cannot be empty\"}"; | ||
String logMessageOnConversionError = | ||
monitoredDeployBaseTask.getRetrofitLogMessage(conversionError.getResponse()); | ||
|
||
assertThat(logMessageOnConversionError) | ||
.isEqualTo( | ||
String.format("status: %s\nheaders: %s\nresponse body: %s", status, header, body)); | ||
} | ||
|
||
@Test | ||
void shouldReturnDefaultLogMsgWhenNetworkErrorHasOccurred() { | ||
|
||
RetrofitError networkError = | ||
RetrofitError.networkError( | ||
"https://foo.com/deployment/evaluateHealth", | ||
new IOException("Failed to connect to the host : foo.com")); | ||
|
||
String logMessageOnNetworkError = | ||
monitoredDeployBaseTask.getRetrofitLogMessage(networkError.getResponse()); | ||
|
||
assertThat(logMessageOnNetworkError).isEqualTo("<NO RESPONSE>"); | ||
} | ||
|
||
@Test | ||
void shouldReturnDefaultLogMsgWhenUnexpectedErrorHasOccurred() { | ||
|
||
RetrofitError unexpectedError = | ||
RetrofitError.unexpectedError( | ||
"https://foo.com/deployment/evaluateHealth", | ||
new IOException("Failed to connect to the host : foo.com")); | ||
|
||
String logMessageOnUnexpectedError = | ||
monitoredDeployBaseTask.getRetrofitLogMessage(unexpectedError.getResponse()); | ||
|
||
assertThat(logMessageOnUnexpectedError).isEqualTo("<NO RESPONSE>"); | ||
} | ||
|
||
static class MockTypedInput implements TypedInput { | ||
private final Converter converter; | ||
private final Object body; | ||
|
||
private byte[] bytes; | ||
|
||
MockTypedInput(Converter converter, Object body) { | ||
this.converter = converter; | ||
this.body = body; | ||
} | ||
|
||
@Override | ||
public String mimeType() { | ||
return "application/unknown"; | ||
} | ||
|
||
@Override | ||
public long length() { | ||
try { | ||
initBytes(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return bytes.length; | ||
} | ||
|
||
@Override | ||
public InputStream in() throws IOException { | ||
initBytes(); | ||
return new ByteArrayInputStream(bytes); | ||
} | ||
|
||
private synchronized void initBytes() throws IOException { | ||
if (bytes == null) { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
converter.toBody(body).writeTo(out); | ||
bytes = out.toByteArray(); | ||
} | ||
} | ||
} | ||
} |