Skip to content

Commit

Permalink
additional code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Feb 4, 2025
1 parent 6bd3451 commit f8e8d45
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

package org.opensearch.dataprepper.plugins.source.atlassian.rest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.plugins.source.atlassian.rest.auth.AtlassianAuthConfig;
import org.opensearch.dataprepper.plugins.source.source_crawler.exception.UnAuthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AtlassianRestClientTest {

@Mock
private RestTemplate restTemplate;

@Mock
private AtlassianAuthConfig authConfig;

private static Stream<Arguments> provideHttpStatusCodesWithExceptionClass() {
return Stream.of(
Arguments.of(HttpStatus.FORBIDDEN, UnAuthorizedException.class),
Arguments.of(HttpStatus.UNAUTHORIZED, RuntimeException.class),
Arguments.of(HttpStatus.TOO_MANY_REQUESTS, RuntimeException.class),
Arguments.of(HttpStatus.INSUFFICIENT_STORAGE, RuntimeException.class)
);
}

@ParameterizedTest
@MethodSource("provideHttpStatusCodesWithExceptionClass")
void testInvokeRestApiTokenExpired(HttpStatus statusCode, Class expectedExceptionType) {
AtlassianRestClient atlassianRestClient = new AtlassianRestClient(restTemplate, authConfig);
atlassianRestClient.setSleepTimeMultiplier(1);
when(restTemplate.getForEntity(any(URI.class), any(Class.class))).thenThrow(new HttpClientErrorException(statusCode));
URI uri = UriComponentsBuilder.fromHttpUrl("https://example.com/rest/api/2/issue/key").buildAndExpand().toUri();
assertThrows(expectedExceptionType, () -> atlassianRestClient.invokeRestApi(uri, Object.class));
}

@Test
void testInvokeRestApiSuccessFullResponse() {
AtlassianRestClient atlassianRestClient = new AtlassianRestClient(restTemplate, authConfig);
atlassianRestClient.setSleepTimeMultiplier(1);
String apiReponse = "{\"api-response\":\"ok\"}";
ResponseEntity responseEntity = new ResponseEntity(apiReponse, HttpStatus.OK);
when(restTemplate.getForEntity(any(URI.class), any(Class.class))).thenReturn(responseEntity);
URI uri = UriComponentsBuilder.fromHttpUrl("https://example.com/rest/api/2/issue/key").buildAndExpand().toUri();
assertEquals(apiReponse, atlassianRestClient.invokeRestApi(uri, String.class).getBody());
}

}

0 comments on commit f8e8d45

Please sign in to comment.