Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -21,12 +21,4 @@ public String toString() {
return type;
}

public static TransformerType fromString(String type) {
for (TransformerType transformerType : values()) {
if (transformerType.type.equalsIgnoreCase(type)) {
return transformerType;
}
}
throw new IllegalArgumentException("Unrecognized Transformer type [" + type + "]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,4 @@ public KendraIntelligentRankingException(String message) {
super(message);
}

public KendraIntelligentRankingException(String message, Throwable cause) {
super(message, cause);
}

public KendraIntelligentRankingException(String message, Throwable cause, Object... args) {
super(message, cause, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import java.util.List;
import java.util.Objects;

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class Document {
Expand All @@ -30,6 +31,12 @@ public Document(String id, String groupId, List<String> tokenizedTitle, List<Str
this.originalScore = originalScore;
}

/**
* No-args constructor used to deserialize.
*/
public Document() {
}

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Collaborator Author

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).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks!

public String getId() {
return id;
}
Expand Down Expand Up @@ -69,4 +76,17 @@ public Float getOriginalScore() {
public void setOriginalScore(Float originalScore) {
this.originalScore = originalScore;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Document document = (Document) o;
return Objects.equals(id, document.id) && Objects.equals(groupId, document.groupId) && Objects.equals(tokenizedTitle, document.tokenizedTitle) && Objects.equals(tokenizedBody, document.tokenizedBody) && Objects.equals(originalScore, document.originalScore);
}

@Override
public int hashCode() {
return Objects.hash(id, groupId, tokenizedTitle, tokenizedBody, originalScore);
}
}
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));
}
}
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());
}
}
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());
}
}
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);
}

}