diff --git a/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponse.java b/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponse.java index 1aecf7c55d9..d31ed6b8ac9 100644 --- a/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponse.java +++ b/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponse.java @@ -37,6 +37,8 @@ public class QueryResponse extends GenericModel { @SerializedName("table_results") protected List tableResults; + protected List passages; + /** * Gets the matchingResults. * @@ -113,4 +115,15 @@ public List getSuggestedRefinements() { public List getTableResults() { return tableResults; } + + /** + * Gets the passages. + * + *

Passages returned by Discovery. + * + * @return the passages + */ + public List getPassages() { + return passages; + } } diff --git a/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponsePassage.java b/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponsePassage.java new file mode 100644 index 00000000000..610724a73ae --- /dev/null +++ b/discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponsePassage.java @@ -0,0 +1,118 @@ +/* + * (C) Copyright IBM Corp. 2020. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package com.ibm.watson.discovery.v2.model; + +import com.google.gson.annotations.SerializedName; +import com.ibm.cloud.sdk.core.service.model.GenericModel; + +/** A passage query response. */ +public class QueryResponsePassage extends GenericModel { + + @SerializedName("passage_text") + protected String passageText; + + @SerializedName("passage_score") + protected Double passageScore; + + @SerializedName("document_id") + protected String documentId; + + @SerializedName("collection_id") + protected String collectionId; + + @SerializedName("start_offset") + protected Long startOffset; + + @SerializedName("end_offset") + protected Long endOffset; + + protected String field; + + /** + * Gets the passageText. + * + *

The content of the extracted passage. + * + * @return the passageText + */ + public String getPassageText() { + return passageText; + } + + /** + * Gets the passageScore. + * + *

The confidence score of the passages's analysis. A higher score indicates greater + * confidence. + * + * @return the passageScore + */ + public Double getPassageScore() { + return passageScore; + } + + /** + * Gets the documentId. + * + *

The unique identifier of the ingested document. + * + * @return the documentId + */ + public String getDocumentId() { + return documentId; + } + + /** + * Gets the collectionId. + * + *

The unique identifier of the collection. + * + * @return the collectionId + */ + public String getCollectionId() { + return collectionId; + } + + /** + * Gets the startOffset. + * + *

The position of the first character of the extracted passage in the originating field. + * + * @return the startOffset + */ + public Long getStartOffset() { + return startOffset; + } + + /** + * Gets the endOffset. + * + *

The position of the last character of the extracted passage in the originating field. + * + * @return the endOffset + */ + public Long getEndOffset() { + return endOffset; + } + + /** + * Gets the field. + * + *

The label of the field from which the passage has been extracted. + * + * @return the field + */ + public String getField() { + return field; + } +} diff --git a/discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java b/discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java index 90136b2d78d..f86ab6f90df 100644 --- a/discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java +++ b/discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java @@ -24,14 +24,12 @@ import com.google.gson.internal.LazilyParsedNumber; import com.ibm.cloud.sdk.core.http.HttpConfigOptions; import com.ibm.cloud.sdk.core.http.HttpMediaType; +import com.ibm.cloud.sdk.core.http.Response; import com.ibm.cloud.sdk.core.security.Authenticator; import com.ibm.cloud.sdk.core.security.BasicAuthenticator; import com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator; import com.ibm.cloud.sdk.core.security.IamAuthenticator; -import com.ibm.cloud.sdk.core.service.exception.BadRequestException; -import com.ibm.cloud.sdk.core.service.exception.ForbiddenException; -import com.ibm.cloud.sdk.core.service.exception.InternalServerErrorException; -import com.ibm.cloud.sdk.core.service.exception.NotFoundException; +import com.ibm.cloud.sdk.core.service.exception.*; import com.ibm.cloud.sdk.core.util.GsonSingleton; import com.ibm.watson.common.RetryRunner; import com.ibm.watson.common.WaitFor; @@ -40,6 +38,8 @@ import com.ibm.watson.discovery.query.Operator; import com.ibm.watson.discovery.v1.model.*; import com.ibm.watson.discovery.v1.model.NormalizationOperation.Operation; + +import java.awt.*; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -354,7 +354,7 @@ public void pingIsSuccessful() { } /** Bad credentials throws exception. */ - @Test(expected = ForbiddenException.class) + @Test(expected = UnauthorizedException.class) public void badCredentialsThrowsException() { Discovery badService = new Discovery("2019-04-30", new BasicAuthenticator("foo", "bar")); badService.listEnvironments(null).execute().getResult(); @@ -2304,7 +2304,8 @@ private class WaitForCollectionAvailable implements WaitFor.Condition { public boolean isSatisfied() { GetCollectionOptions getOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build(); - String status = discovery.getCollection(getOptions).execute().getResult().getStatus(); + Collection collectionResult = discovery.getCollection(getOptions).execute().getResult(); + String status = collectionResult.getStatus(); return status.equals(Collection.Status.ACTIVE); } diff --git a/discovery/src/test/java/com/ibm/watson/discovery/v2/DiscoveryIT.java b/discovery/src/test/java/com/ibm/watson/discovery/v2/DiscoveryIT.java index 1279bf50e54..9e83e198333 100644 --- a/discovery/src/test/java/com/ibm/watson/discovery/v2/DiscoveryIT.java +++ b/discovery/src/test/java/com/ibm/watson/discovery/v2/DiscoveryIT.java @@ -21,7 +21,7 @@ import com.ibm.cloud.sdk.core.http.HttpMediaType; import com.ibm.cloud.sdk.core.http.Response; import com.ibm.cloud.sdk.core.security.Authenticator; -import com.ibm.cloud.sdk.core.security.BasicAuthenticator; +import com.ibm.cloud.sdk.core.security.IamAuthenticator; import com.ibm.watson.common.RetryRunner; import com.ibm.watson.common.WatsonServiceTest; import com.ibm.watson.discovery.query.AggregationType; @@ -32,21 +32,21 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.List; import java.util.UUID; -import org.junit.Assume; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; /** The Class DiscoveryIT. */ -@Ignore @RunWith(RetryRunner.class) +@Ignore public class DiscoveryIT extends WatsonServiceTest { private static final String VERSION = "2019-11-22"; private static final String RESOURCE = "src/test/resources/discovery/v2/"; - private static final String PROJECT_ID = "9558dc01-8554-4d18-b0a5-70196f9f2fe6"; - private static final String COLLECTION_ID = "161d1e47-9651-e657-0000-016e8e939caf"; + private static final String PROJECT_ID = "571ec4f7-c413-4928-b7f9-3c69045ed27a"; + private static final String COLLECTION_ID = "d02b9aa7-903e-2ea1-0000-017410e684bd"; private Discovery service; @@ -64,12 +64,11 @@ public class DiscoveryIT extends WatsonServiceTest { @Before public void setUp() throws Exception { super.setUp(); - String bearerToken = getProperty("discovery_v2.bearer_token"); - Assume.assumeFalse("config.properties doesn't have valid credentials.", (bearerToken == null)); - // Authenticator authenticator = new BearerTokenAuthenticator(bearerToken); String apiKey = getProperty("discovery.apikey"); - Authenticator authenticator = new BasicAuthenticator("apikey", apiKey); + Authenticator authenticator = + new IamAuthenticator( + apiKey, "https://iam.test.cloud.ibm.com/identity/token", null, null, false, null); service = new Discovery(VERSION, authenticator); service.setDefaultHeaders(getDefaultHeaders()); service.setServiceUrl(getProperty("discovery_v2.url")); @@ -1103,4 +1102,47 @@ public void testDeleteUserData() { assertNotNull(deleteResponse); assertTrue(deleteResponse.getStatusCode() == 204); } + + /** Test query passages per document true. */ + @Test + public void TestQueryPassagesPerDocumentTrue() { + List Ids = new ArrayList<>(); + Ids.add(COLLECTION_ID); + QueryLargePassages queryLargePassages = + new QueryLargePassages.Builder().perDocument(true).build(); + + QueryOptions queryOptions = + new QueryOptions.Builder() + .projectId(PROJECT_ID) + .collectionIds(Ids) + .passages(queryLargePassages) + .query("text:IBM") + .count(2) + .build(); + QueryResponse queryResult = service.query(queryOptions).execute().getResult(); + assertNotNull(queryResult.getResults().get(0).getDocumentPassages().get(0).getPassageText()); + } + + /** Test query passages per document false. */ + @Test + public void TestQueryPassagesPerDocumentFalse() { + List Ids = new ArrayList<>(); + Ids.add(COLLECTION_ID); + QueryLargePassages queryLargePassages = + new QueryLargePassages.Builder().perDocument(false).build(); + + QueryOptions queryOptions = + new QueryOptions.Builder() + .projectId(PROJECT_ID) + .collectionIds(Ids) + .passages(queryLargePassages) + .query("text:IBM") + .count(2) + .build(); + QueryResponse queryResult = service.query(queryOptions).execute().getResult(); + assertNotNull(queryResult); + assertNotNull(queryResult.getPassages().get(0).getCollectionId()); + assertNotNull(queryResult.getPassages().get(0).getPassageText()); + assertNotNull(queryResult.getPassages().get(0).getDocumentId()); + } } diff --git a/language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java b/language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java index 032ea3a24fa..c4a190f8f0f 100644 --- a/language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java +++ b/language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java @@ -230,7 +230,6 @@ public void testDocumentTranslation() throws FileNotFoundException, InterruptedE new TranslateDocumentOptions.Builder() .file(new File("src/test/resources/language_translation/document_to_translate.txt")) .fileContentType(HttpMediaType.TEXT_PLAIN) - .filename("java-integration-test-file") .source("en") .target("es") .build();