-
Notifications
You must be signed in to change notification settings - Fork 15
Add GetObjectApiTest, PutObjectsApiTest, ListKeyVersionsApiTest #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,10 @@ | ||
package org.vss; | ||
|
||
import jakarta.ws.rs.ApplicationPath; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
|
||
@ApplicationPath("/") | ||
public class VSSApplication extends ResourceConfig { | ||
public VSSApplication() { | ||
} | ||
} |
This file contains hidden or 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,48 @@ | ||
package org.vss.api; | ||
|
||
import com.google.protobuf.GeneratedMessageV3; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.core.Response; | ||
import org.vss.ErrorCode; | ||
import org.vss.ErrorResponse; | ||
import org.vss.KVStore; | ||
import org.vss.exception.ConflictException; | ||
|
||
public abstract class AbstractVssApi { | ||
final KVStore kvStore; | ||
|
||
@Inject | ||
public AbstractVssApi(KVStore kvStore) { | ||
this.kvStore = kvStore; | ||
} | ||
|
||
Response toResponse(GeneratedMessageV3 protoResponse) { | ||
|
||
return Response | ||
.status(Response.Status.OK) | ||
.entity(protoResponse.toByteArray()) | ||
.build(); | ||
} | ||
|
||
Response toErrorResponse(Exception e) { | ||
ErrorCode errorCode; | ||
if (e instanceof ConflictException) { | ||
errorCode = ErrorCode.CONFLICT_EXCEPTION; | ||
} else if (e instanceof IllegalArgumentException | ||
|| e instanceof InvalidProtocolBufferException) { | ||
errorCode = ErrorCode.INVALID_REQUEST_EXCEPTION; | ||
} else { | ||
errorCode = ErrorCode.INTERNAL_SERVER_EXCEPTION; | ||
} | ||
|
||
ErrorResponse errorResponse = ErrorResponse.newBuilder() | ||
.setErrorCode(errorCode) | ||
.setMessage(e.getMessage()) | ||
.build(); | ||
|
||
return Response.status(errorCode.getNumber()) | ||
.entity(errorResponse.toByteArray()) | ||
.build(); | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
package org.vss.api; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.vss.GetObjectRequest; | ||
import org.vss.GetObjectResponse; | ||
import org.vss.KVStore; | ||
|
||
@Path(VssApiEndpoint.GET_OBJECT) | ||
@Slf4j | ||
public class GetObjectApi extends AbstractVssApi { | ||
|
||
@Inject | ||
public GetObjectApi(KVStore kvstore) { | ||
super(kvstore); | ||
} | ||
|
||
@POST | ||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | ||
public Response execute(byte[] payload) { | ||
try { | ||
GetObjectRequest request = GetObjectRequest.parseFrom(payload); | ||
GetObjectResponse response = kvStore.get(request); | ||
return toResponse(response); | ||
} catch (Exception e) { | ||
log.error("Exception in GetObjectApi: ", e); | ||
return toErrorResponse(e); | ||
} | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
package org.vss.api; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.vss.KVStore; | ||
import org.vss.ListKeyVersionsRequest; | ||
import org.vss.ListKeyVersionsResponse; | ||
|
||
@Path(VssApiEndpoint.LIST_KEY_VERSIONS) | ||
@Slf4j | ||
public class ListKeyVersionsApi extends AbstractVssApi { | ||
|
||
@Inject | ||
public ListKeyVersionsApi(KVStore kvStore) { | ||
super(kvStore); | ||
} | ||
|
||
@POST | ||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | ||
public Response execute(byte[] payload) { | ||
try { | ||
ListKeyVersionsRequest request = ListKeyVersionsRequest.parseFrom(payload); | ||
ListKeyVersionsResponse response = kvStore.listKeyVersions(request); | ||
return toResponse(response); | ||
} catch (Exception e) { | ||
log.error("Exception in ListKeyVersionsApi: ", e); | ||
return toErrorResponse(e); | ||
} | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
package org.vss.api; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.vss.KVStore; | ||
import org.vss.PutObjectRequest; | ||
import org.vss.PutObjectResponse; | ||
|
||
@Path(VssApiEndpoint.PUT_OBJECTS) | ||
@Slf4j | ||
public class PutObjectsApi extends AbstractVssApi { | ||
|
||
@Inject | ||
public PutObjectsApi(KVStore kvStore) { | ||
super(kvStore); | ||
} | ||
|
||
@POST | ||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | ||
public Response execute(byte[] payload) { | ||
try { | ||
PutObjectRequest putObjectRequest = PutObjectRequest.parseFrom(payload); | ||
PutObjectResponse response = kvStore.put(putObjectRequest); | ||
return toResponse(response); | ||
} catch (Exception e) { | ||
log.error("Exception in PutObjectsApi: ", e); | ||
return toErrorResponse(e); | ||
} | ||
} | ||
} |
This file contains hidden or 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,7 @@ | ||
package org.vss.api; | ||
|
||
public class VssApiEndpoint { | ||
public static final String GET_OBJECT = "/getObject"; | ||
public static final String PUT_OBJECTS = "/putObjects"; | ||
public static final String LIST_KEY_VERSIONS = "/listKeyVersions"; | ||
} |
This file contains hidden or 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,86 @@ | ||
package org.vss.api; | ||
|
||
import com.google.protobuf.ByteString; | ||
import jakarta.ws.rs.core.Response; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.vss.ErrorCode; | ||
import org.vss.ErrorResponse; | ||
import org.vss.GetObjectRequest; | ||
import org.vss.GetObjectResponse; | ||
import org.vss.KVStore; | ||
import org.vss.KeyValue; | ||
import org.vss.exception.ConflictException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GetObjectApiTest { | ||
private GetObjectApi getObjectApi; | ||
private KVStore mockKVStore; | ||
|
||
private static String TEST_STORE_ID = "storeId"; | ||
private static String TEST_KEY = "key"; | ||
private static KeyValue TEST_KV = KeyValue.newBuilder().setKey(TEST_KEY).setValue( | ||
ByteString.copyFrom("test_value", StandardCharsets.UTF_8)).build(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockKVStore = mock(KVStore.class); | ||
getObjectApi = new GetObjectApi(mockKVStore); | ||
} | ||
|
||
@Test | ||
void execute_ValidPayload_ReturnsResponse() { | ||
GetObjectRequest expectedRequest = | ||
GetObjectRequest.newBuilder().setStoreId(TEST_STORE_ID).setKey(TEST_KEY).build(); | ||
byte[] payload = expectedRequest.toByteArray(); | ||
GetObjectResponse mockResponse = GetObjectResponse.newBuilder().setValue(TEST_KV).build(); | ||
when(mockKVStore.get(expectedRequest)).thenReturn(mockResponse); | ||
|
||
Response actualResponse = getObjectApi.execute(payload); | ||
|
||
assertThat(actualResponse.getStatus(), is(Response.Status.OK.getStatusCode())); | ||
assertThat(actualResponse.getEntity(), is(mockResponse.toByteArray())); | ||
verify(mockKVStore).get(expectedRequest); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideErrorTestCases") | ||
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception, | ||
ErrorCode errorCode) { | ||
GetObjectRequest expectedRequest = GetObjectRequest.newBuilder() | ||
.setStoreId(TEST_STORE_ID) | ||
.setKey(TEST_KEY) | ||
.build(); | ||
byte[] payload = expectedRequest.toByteArray(); | ||
when(mockKVStore.get(any())).thenThrow(exception); | ||
|
||
Response response = getObjectApi.execute(payload); | ||
|
||
ErrorResponse expectedErrorResponse = ErrorResponse.newBuilder() | ||
.setErrorCode(errorCode) | ||
.setMessage("") | ||
.build(); | ||
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray())); | ||
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber())); | ||
verify(mockKVStore).get(expectedRequest); | ||
} | ||
|
||
private static Stream<Arguments> provideErrorTestCases() { | ||
return Stream.of( | ||
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION), | ||
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION), | ||
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION) | ||
); | ||
} | ||
} |
This file contains hidden or 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,92 @@ | ||
package org.vss.api; | ||
|
||
import com.google.protobuf.ByteString; | ||
import jakarta.ws.rs.core.Response; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.vss.ErrorCode; | ||
import org.vss.ErrorResponse; | ||
import org.vss.KVStore; | ||
import org.vss.KeyValue; | ||
import org.vss.ListKeyVersionsRequest; | ||
import org.vss.ListKeyVersionsResponse; | ||
import org.vss.exception.ConflictException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ListKeyVersionsApiTest { | ||
private ListKeyVersionsApi listKeyVersionsApi; | ||
private KVStore mockKVStore; | ||
|
||
private static String TEST_STORE_ID = "storeId"; | ||
private static String TEST_KEY = "key"; | ||
private static KeyValue TEST_KV = KeyValue.newBuilder().setKey(TEST_KEY).setValue( | ||
ByteString.copyFrom("test_value", StandardCharsets.UTF_8)).build(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockKVStore = mock(KVStore.class); | ||
listKeyVersionsApi = new ListKeyVersionsApi(mockKVStore); | ||
} | ||
|
||
@Test | ||
void execute_ValidPayload_ReturnsResponse() { | ||
ListKeyVersionsRequest expectedRequest = | ||
ListKeyVersionsRequest.newBuilder() | ||
.setStoreId(TEST_STORE_ID) | ||
.setKeyPrefix(TEST_KEY) | ||
.build(); | ||
byte[] payload = expectedRequest.toByteArray(); | ||
ListKeyVersionsResponse mockResponse = ListKeyVersionsResponse.newBuilder().addAllKeyVersions( | ||
List.of(TEST_KV)).build(); | ||
when(mockKVStore.listKeyVersions(expectedRequest)).thenReturn(mockResponse); | ||
|
||
Response actualResponse = listKeyVersionsApi.execute(payload); | ||
|
||
assertThat(actualResponse.getStatus(), is(Response.Status.OK.getStatusCode())); | ||
assertThat(actualResponse.getEntity(), is(mockResponse.toByteArray())); | ||
verify(mockKVStore).listKeyVersions(expectedRequest); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideErrorTestCases") | ||
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception, | ||
ErrorCode errorCode) { | ||
ListKeyVersionsRequest expectedRequest = | ||
ListKeyVersionsRequest.newBuilder() | ||
.setStoreId(TEST_STORE_ID) | ||
.setKeyPrefix(TEST_KEY) | ||
.build(); | ||
byte[] payload = expectedRequest.toByteArray(); | ||
when(mockKVStore.listKeyVersions(any())).thenThrow(exception); | ||
|
||
Response response = listKeyVersionsApi.execute(payload); | ||
|
||
ErrorResponse expectedErrorResponse = ErrorResponse.newBuilder() | ||
.setErrorCode(errorCode) | ||
.setMessage("") | ||
.build(); | ||
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray())); | ||
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber())); | ||
verify(mockKVStore).listKeyVersions(expectedRequest); | ||
} | ||
|
||
private static Stream<Arguments> provideErrorTestCases() { | ||
return Stream.of( | ||
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION), | ||
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION), | ||
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i tried creating a common base testcase but it was more complex and less readable since many api specific calls are interleaved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are the abstract methods a sub-test would have had to implement in that case, and it seemed difficult to understand test-case.