Skip to content

Commit

Permalink
test(clouddriver): demonstrate behavior of the method getApplication(…
Browse files Browse the repository at this point in the history
…) in EphemeralServerGroupsPoller

Tests added to verify the upcoming changes when Front50Service is configured with SpinnakerRetrofitErrorHandler.

These test cases verifies the behavior of the method getApplication() when different kinds of RetrofitErrors such as httpError and conversionsError are thrown.
  • Loading branch information
Pranav-b-7 committed Jan 12, 2024
1 parent d13aa79 commit d81480a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.annotations.VisibleForTesting;
import com.netflix.spinnaker.kork.core.RetrySupport;
import com.netflix.spinnaker.kork.exceptions.SystemException;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType;
Expand Down Expand Up @@ -274,7 +275,8 @@ private Map<String, Object> buildDestroyServerGroupOperation(
return operation;
}

private Optional<Application> getApplication(String applicationName) {
@VisibleForTesting
Optional<Application> getApplication(String applicationName) {
try {
return Optional.of(front50Service.get(applicationName));
} catch (RetrofitError e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2024 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.pollers;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.core.RetrySupport;
import com.netflix.spinnaker.kork.exceptions.SystemException;
import com.netflix.spinnaker.orca.clouddriver.CloudDriverService;
import com.netflix.spinnaker.orca.clouddriver.config.PollerConfigurationProperties;
import com.netflix.spinnaker.orca.front50.Front50Service;
import com.netflix.spinnaker.orca.notifications.NotificationClusterLock;
import com.netflix.spinnaker.orca.pipeline.ExecutionLauncher;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;

public class EphemeralServerGroupsPollerTest {

private Front50Service front50Service;

private EphemeralServerGroupsPoller ephemeralServerGroupsPoller;

private NotificationClusterLock notificationClusterLock;
private ObjectMapper objectMapper;
private CloudDriverService cloudDriverService;
private RetrySupport retrySupport;
private Registry registry;
private ExecutionLauncher executionLauncher;
private PollerConfigurationProperties pollerConfigurationProperties;

@BeforeEach
public void setup() {

front50Service = mock(Front50Service.class);
notificationClusterLock = mock(NotificationClusterLock.class);
objectMapper = new ObjectMapper();
cloudDriverService = mock(CloudDriverService.class);
retrySupport = mock(RetrySupport.class);
registry = mock(Registry.class);
executionLauncher = mock(ExecutionLauncher.class);
pollerConfigurationProperties = mock(PollerConfigurationProperties.class);
ephemeralServerGroupsPoller =
new EphemeralServerGroupsPoller(
notificationClusterLock,
objectMapper,
cloudDriverService,
retrySupport,
registry,
executionLauncher,
front50Service,
pollerConfigurationProperties);
}

@Test
public void testSystemExceptionStackTraceWhenRetrofitHttpErrorOccurs() {
var application = "testapp";

var url = "https://front50service.com/v2/applications/" + application;
Response mockResponse =
new Response(
url,
HttpStatus.NOT_ACCEPTABLE.value(),
HttpStatus.NOT_ACCEPTABLE.name(),
Collections.emptyList(),
new TypedByteArray(
"application/json", "{ \"error\": \"application testapp not found\"}".getBytes()));

RetrofitError httpError = RetrofitError.httpError(url, mockResponse, null, null);

when(front50Service.get(application)).thenThrow(httpError);

SystemException systemException = new SystemException(httpError.getMessage(), httpError);

System.out.println("cause : " + systemException.getCause());
assertThatThrownBy(() -> ephemeralServerGroupsPoller.getApplication(application))
.hasCause(systemException.getCause())
.hasCauseExactlyInstanceOf(RetrofitError.class);
}
}

0 comments on commit d81480a

Please sign in to comment.