generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 26
Add more test coverage #100
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
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
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
104 changes: 104 additions & 0 deletions
104
...arch/relevance/transformer/kendraintelligentranking/client/KendraClientSettingsTests.java
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,104 @@ | ||
| /* | ||
| * 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.search.relevance.transformer.kendraintelligentranking.client; | ||
|
|
||
| import com.amazonaws.auth.AWSCredentials; | ||
| import com.amazonaws.auth.AWSSessionCredentials; | ||
| import org.opensearch.common.settings.MockSecureSettings; | ||
| import org.opensearch.common.settings.SecureSettings; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.settings.SettingsException; | ||
| import org.opensearch.search.relevance.transformer.kendraintelligentranking.configuration.KendraIntelligentRankerSettings; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class KendraClientSettingsTests extends OpenSearchTestCase { | ||
|
|
||
| private static final String REGION = "us-west-2"; | ||
| private static final String ENDPOINT = "http://localhost"; | ||
| private static final String ACCESS_KEY = "my-access-key"; | ||
| private static final String SECRET_KEY = "my-secret-key"; | ||
| private static final String ASSUMED_ROLE = "assumed-role"; | ||
| private static final String SESSION_TOKEN = "session-token"; | ||
|
|
||
|
|
||
| private static KendraClientSettings buildClientSettings(boolean withAccessKey, boolean withSecretKey, | ||
| boolean withSessionToken) throws IOException { | ||
| try (MockSecureSettings secureSettings = new MockSecureSettings()) { | ||
| if (withAccessKey) { | ||
| secureSettings.setString(KendraIntelligentRankerSettings.ACCESS_KEY_SETTING.getKey(), ACCESS_KEY); | ||
| } | ||
| if (withSecretKey) { | ||
| secureSettings.setString(KendraIntelligentRankerSettings.SECRET_KEY_SETTING.getKey(), SECRET_KEY); | ||
| } | ||
| if (withSessionToken) { | ||
| secureSettings.setString(KendraIntelligentRankerSettings.SESSION_TOKEN_SETTING.getKey(), SESSION_TOKEN); | ||
| } | ||
| Settings settings = Settings.builder() | ||
| .put(KendraIntelligentRankerSettings.SERVICE_REGION_SETTING.getKey(), REGION) | ||
| .put(KendraIntelligentRankerSettings.SERVICE_ENDPOINT_SETTING.getKey(), ENDPOINT) | ||
| .put(KendraIntelligentRankerSettings.ASSUME_ROLE_ARN_SETTING.getKey(), ASSUMED_ROLE) | ||
| .setSecureSettings(secureSettings) | ||
| .build(); | ||
|
|
||
| return KendraClientSettings.getClientSettings(settings); | ||
| } | ||
| } | ||
|
|
||
| public void testWithBasicCredentials() throws IOException { | ||
| KendraClientSettings clientSettings = buildClientSettings(true, true, false); | ||
|
|
||
| AWSCredentials credentials = clientSettings.getCredentials(); | ||
| assertEquals(ACCESS_KEY, credentials.getAWSAccessKeyId()); | ||
| assertEquals(SECRET_KEY, credentials.getAWSSecretKey()); | ||
| assertFalse(credentials instanceof AWSSessionCredentials); | ||
| assertEquals(REGION, clientSettings.getServiceRegion()); | ||
| assertEquals(ENDPOINT, clientSettings.getServiceEndpoint()); | ||
| assertEquals(ASSUMED_ROLE, clientSettings.getAssumeRoleArn()); | ||
| } | ||
|
|
||
| public void testWithSessionCredentials() throws IOException { | ||
| KendraClientSettings clientSettings = buildClientSettings(true, true, true); | ||
|
|
||
| AWSCredentials credentials = clientSettings.getCredentials(); | ||
| assertEquals(ACCESS_KEY, credentials.getAWSAccessKeyId()); | ||
| assertEquals(SECRET_KEY, credentials.getAWSSecretKey()); | ||
| assertTrue(credentials instanceof AWSSessionCredentials); | ||
| AWSSessionCredentials sessionCredentials = (AWSSessionCredentials) credentials; | ||
| assertEquals(SESSION_TOKEN, sessionCredentials.getSessionToken()); | ||
| assertEquals(REGION, clientSettings.getServiceRegion()); | ||
| assertEquals(ENDPOINT, clientSettings.getServiceEndpoint()); | ||
| assertEquals(ASSUMED_ROLE, clientSettings.getAssumeRoleArn()); | ||
| } | ||
|
|
||
| public void testWithoutCredentials() throws IOException { | ||
| KendraClientSettings clientSettings = buildClientSettings(false, false, false); | ||
|
|
||
| assertNull(clientSettings.getCredentials()); | ||
| assertEquals(REGION, clientSettings.getServiceRegion()); | ||
| assertEquals(ENDPOINT, clientSettings.getServiceEndpoint()); | ||
| assertEquals(ASSUMED_ROLE, clientSettings.getAssumeRoleArn()); | ||
| } | ||
|
|
||
| public void testWithoutAccessKey() { | ||
| expectThrows(SettingsException.class, () -> buildClientSettings(false, true, false)); | ||
| expectThrows(SettingsException.class, () -> buildClientSettings(false, true, true)); | ||
| } | ||
|
|
||
| public void testWithoutSecretKey() { | ||
| expectThrows(SettingsException.class, () -> buildClientSettings(true, false, false)); | ||
| expectThrows(SettingsException.class, () -> buildClientSettings(true, false, true)); | ||
| } | ||
|
|
||
| public void testWithSessionTokenButNoCredentials() { | ||
| expectThrows(SettingsException.class, () -> buildClientSettings(false, false, true)); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...rch/relevance/transformer/kendraintelligentranking/client/SimpleAwsErrorHandlerTests.java
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,45 @@ | ||
| /* | ||
| * 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.search.relevance.transformer.kendraintelligentranking.client; | ||
|
|
||
| import com.amazonaws.AmazonServiceException; | ||
| import com.amazonaws.DefaultRequest; | ||
| import com.amazonaws.Request; | ||
| import com.amazonaws.http.HttpResponse; | ||
| import org.apache.http.HttpStatus; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class SimpleAwsErrorHandlerTests extends OpenSearchTestCase { | ||
|
|
||
| private static final int STATUS_CODE = HttpStatus.SC_PAYMENT_REQUIRED; | ||
| private static final String STATUS_TEXT = "Payment required"; | ||
| private static final String SERVICE_NAME = "my-service"; | ||
| private static final String ERROR_MESSAGE = "This is the error message"; | ||
|
|
||
| public void testBehavior() throws Exception { | ||
| SimpleAwsErrorHandler errorHandler = new SimpleAwsErrorHandler(); | ||
| assertFalse(errorHandler.needsConnectionLeftOpen()); | ||
| Request<?> request = new DefaultRequest<>(SERVICE_NAME); | ||
| HttpResponse httpResponse = new HttpResponse(request, null, null); | ||
| httpResponse.setContent(new ByteArrayInputStream(ERROR_MESSAGE.getBytes(StandardCharsets.UTF_8))); | ||
| httpResponse.setStatusCode(STATUS_CODE); | ||
| httpResponse.setStatusText(STATUS_TEXT); | ||
|
|
||
| AmazonServiceException ase = errorHandler.handle(httpResponse); | ||
|
|
||
| assertEquals(ERROR_MESSAGE, ase.getErrorMessage()); | ||
| assertEquals(STATUS_CODE, ase.getStatusCode()); | ||
| assertEquals(STATUS_TEXT, ase.getErrorCode()); | ||
| assertEquals(SERVICE_NAME, ase.getServiceName()); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ce/transformer/kendraintelligentranking/model/KendraIntelligentRankingExceptionTests.java
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,28 @@ | ||
| /* | ||
| * 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.search.relevance.transformer.kendraintelligentranking.model; | ||
|
|
||
| import org.opensearch.common.io.stream.BytesStreamOutput; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class KendraIntelligentRankingExceptionTests extends OpenSearchTestCase { | ||
|
|
||
| public void testSerializationRoundtrip() throws IOException { | ||
| KendraIntelligentRankingException expected = new KendraIntelligentRankingException("This is an error message"); | ||
| BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); | ||
| expected.writeTo(bytesStreamOutput); | ||
|
|
||
| KendraIntelligentRankingException deserialized = | ||
| new KendraIntelligentRankingException(bytesStreamOutput.bytes().streamInput()); | ||
| assertEquals(expected.getMessage(), deserialized.getMessage()); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...search/search/relevance/transformer/kendraintelligentranking/model/dto/DocumentTests.java
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,47 @@ | ||
| /* | ||
| * 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.search.relevance.transformer.kendraintelligentranking.model.dto; | ||
|
|
||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class DocumentTests extends OpenSearchTestCase { | ||
|
|
||
|
|
||
| public static final String JSON_DOCUMENT = "{" + | ||
| "\"Id\":\"docId\"," + | ||
| "\"GroupId\":\"groupIp\"," + | ||
| "\"TokenizedTitle\":[\"this\",\"is\",\"a\",\"title\"]," + | ||
| "\"TokenizedBody\":[\"here\",\"lies\",\"the\",\"body\"]," + | ||
| "\"OriginalScore\":2.71828" + | ||
| "}"; | ||
| public static final Document TEST_DOCUMENT = new Document("docId", | ||
| "groupIp", | ||
| List.of("this", "is", "a", "title"), | ||
| List.of("here", "lies", "the", "body"), | ||
| 2.71828f); | ||
|
|
||
| public void testSerialization() throws JsonProcessingException { | ||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
| String jsonForm = objectMapper.writeValueAsString(TEST_DOCUMENT); | ||
| assertEquals(JSON_DOCUMENT, jsonForm); | ||
| } | ||
|
|
||
| public void testDeserialization() throws JsonProcessingException { | ||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
| Document doc = objectMapper.readValue(JSON_DOCUMENT, Document.class); | ||
| assertEquals(TEST_DOCUMENT, doc); | ||
| } | ||
|
|
||
| } |
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.
if no return type, should it be void?
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.
It's a constructor.
It doesn't return anything so much as it initializes the object (in this case, it initializes all fields to
null).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 see, thanks!