Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openchallenges): added unit tests to the image service #2291

Merged
merged 21 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.mockito.Mock;
import org.sagebionetworks.openchallenges.image.service.api.ApiUtil;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.context.request.NativeWebRequest;

public class ApiUtilTest {

@Mock private NativeWebRequest nativeWebRequest;

@Test
public void SetExampleResponse_ShouldReturnExpectedResponse() throws IOException {
// Create a mock NativeWebRequest
nativeWebRequest = new MockNativeWebRequest();

// Set up mock behavior
HttpServletResponse mockResponse = new MockHttpServletResponse();
when(nativeWebRequest.getNativeResponse(HttpServletResponse.class)).thenReturn(mockResponse);

// Define example content
String contentType = "application/json";
String example = "{\"key\": \"value\"}";

// Call the SetExampleResponse_ShouldReturnExpectedResponse method
ApiUtil.SetExampleResponse_ShouldReturnExpectedResponse(nativeWebRequest, contentType, example);

// Verify the response
assertEquals(contentType, mockResponse.getHeader("Content-Type"));
assertEquals(example, mockResponse.getContentAsString());
}

private static class MockNativeWebRequest implements NativeWebRequest {
@Override
public <T> T getNativeRequest(Class<T> requiredType) {
return null;
}

@Override
public <T> T getNativeResponse(Class<T> requiredType) {
return null;
}

@Override
public String getHeader(String name) {
return null;
}

@Override
public String[] getHeaderValues(String name) {
return new String[0];
}
}
}
mdsage1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.sagebionetworks.openchallenges.image.service.ImageService;

@RunWith(MockitoJUnitRunner.class)
public class ImageApiDelegateTest {

@Mock private ImageService imageService;

@Test
void ImageApiDelegate_ShouldHaveInjectedImageServiceDependency() {
// Create an instance of ImageApiDelegateImpl
ImageApiDelegateImpl delegate = new ImageApiDelegateImpl(imageService);

// Verify that the ImageService dependency is injected
assertNotNull(delegate.getImageService());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.sagebionetworks.openchallenges.image.service.api.ImageApiDelegate;
import org.sagebionetworks.openchallenges.image.service.model.dto.ImageDto;
import org.sagebionetworks.openchallenges.image.service.model.dto.ImageQueryDto;
import org.springframework.http.ResponseEntity;

@RunWith(MockitoJUnitRunner.class)
public class ImageApiTest {

@Mock private ImageApiDelegate delegate;

@Test
public void testGetImage() {
// Create an instance of ImageApi
ImageApi imageApi =
new ImageApi() {
@Override
public ImageApiDelegate getDelegate() {
return delegate;
}
};

// Create mock response
ImageDto mockImageDto = new ImageDto();
ResponseEntity<ImageDto> mockResponse = ResponseEntity.ok(mockImageDto);

// Create a mock ImageQueryDto
ImageQueryDto mockImageQuery = new ImageQueryDto();

// Set up mock behavior
when(delegate.getImage(mockImageQuery)).thenReturn(mockResponse);

// Call the getImage method
ResponseEntity<ImageDto> response = imageApi.getImage(mockImageQuery);

// Verify the response
assertEquals(mockResponse, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;
import org.sagebionetworks.openchallenges.image.service.exception.SimpleChallengeGlobalException;
import org.springframework.http.HttpStatus;

public class SimpleChallengeGlobalExceptionTest {

@Test
public void testConstructorWithDetails() {
// Create an instance of SimpleChallengeGlobalException using the constructor with details
String details = "Something went wrong";
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(details);

// Verify the exception details
assertEquals(details, exception.getMessage());
assertNull(exception.getType());
assertNull(exception.getTitle());
assertNull(exception.getStatus());
assertNull(exception.getDetail());
}

@Test
public void SimpleChallenge_ShouldReturnSpecifiedArgs() {
// Define the exception details
String type = "ExceptionType";
String title = "Exception Title";
HttpStatus status = HttpStatus.BAD_REQUEST;
String detail = "Exception detail message";

// Create an instance of SimpleChallengeGlobalException using the all-args constructor
SimpleChallengeGlobalException exception =
new SimpleChallengeGlobalException(type, title, status, detail);

// Verify the exception details
assertNull(exception.getMessage());
assertEquals(type, exception.getType());
assertEquals(title, exception.getTitle());
assertEquals(status, exception.getStatus());
assertEquals(detail, exception.getDetail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// import static org.junit.Assert.assertEquals;

// import org.junit.Test;
// import org.sagebionetworks.openchallenges.image.service.model.dto.ImageAspectRatioDto;

// public class ImageAspectRatioDtoTest {

// @Test
// public void GetValueShouldGetSpecifiedNumericCombination() {
// // Test the getValue() method for each enum value
// assertEquals("original", ImageAspectRatioDto.ORIGINAL.getValue());
// assertEquals("16_9", ImageAspectRatioDto._16_9.getValue());
// assertEquals("1_1", ImageAspectRatioDto._1_1.getValue());
// assertEquals("3_2", ImageAspectRatioDto._3_2.getValue());
// assertEquals("2_3", ImageAspectRatioDto._2_3.getValue());
// }

// @Test
// public void FromValueShouldEqualExpectedValue() {
// // Test the fromValue() method for each enum value
// assertEquals(ImageAspectRatioDto.ORIGINAL, ImageAspectRatioDto.fromValue("original"));
// assertEquals(ImageAspectRatioDto._16_9, ImageAspectRatioDto.fromValue("16_9"));
// assertEquals(ImageAspectRatioDto._1_1, ImageAspectRatioDto.fromValue("1_1"));
// assertEquals(ImageAspectRatioDto._3_2, ImageAspectRatioDto.fromValue("3_2"));
// assertEquals(ImageAspectRatioDto._2_3, ImageAspectRatioDto.fromValue("2_3"));
// }

// @Test(expected = IllegalArgumentException.class)
// public void testFromShouldReturnInvalidValue() {
// // Test the fromValue() method with an invalid value
// ImageAspectRatioDto.fromValue("invalid_value");
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void getImage_ShouldReturnImage_WhenObjectKeyIsPassed() {
query.setObjectKey("image.png");

// when an image is requested from the image service
ThumborUrlBuilder builder = mock(ThumborUrlBuilder.class);
ImageQueryDto builder = mock(ThumborUrlBuilder.class);

when(thumbor.buildImage(query.getObjectKey())).thenReturn(builder); // why is it needed?
when(builder.toUrl()).thenReturn(expectedUrl);
Expand Down