Search in sources :

Example 1 with NotFoundException

use of com.ibm.cloud.sdk.core.service.exception.NotFoundException in project cloudant-java-sdk by IBM.

the class UpdateDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Update the document =============================================
    // Set the options to get the document out of the database if it exists
    String exampleDbName = "orders";
    GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId("example").build();
    try {
        // Try to get the document if it previously existed in the database
        Document document = client.getDocument(documentInfoOptions).execute().getResult();
        // Note: for response byte stream use:
        /*
            InputStream documentAsByteStream =
                client.getDocumentAsStream(documentInfoOptions)
                    .execute()
                    .getResult();
            */
        // Add Bob Smith's address to the document
        document.put("address", "19 Front Street, Darlington, DL5 1TY");
        // Remove the joined property from document object
        document.removeProperty("joined");
        // Update the document in the database
        PostDocumentOptions updateDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(document).build();
        // ================================================================
        // Note: for request byte stream use:
        /*
            PostDocumentOptions updateDocumentOptions =
                new PostDocumentOptions.Builder()
                    .db(exampleDbName)
                    .contentType("application/json")
                    .body(documentAsByteStream)
                    .build();
            */
        // ================================================================
        DocumentResult updateDocumentResponse = client.postDocument(updateDocumentOptions).execute().getResult();
        // ====================================================================
        // Note: updating the document can also be done with the "putDocument"
        // method. docId and rev are required for an UPDATE operation,
        // but rev can be provided in the document object too:
        /*
            PutDocumentOptions updateDocumentOptions =
                new PutDocumentOptions.Builder()
                    .db(exampleDbName)
                    .docId(document.getId()) // docId is a required parameter
                    .rev(document.getRev())
                    .document(document) // rev in the document object CAN replace above `rev` parameter
                    .build();
            DocumentResult updateDocumentResponse = client
                .putDocument(updateDocumentOptions)
                .execute()
                .getResult();
            */
        // ====================================================================
        // Keeping track of the latest revision number of the document object
        // is necessary for further UPDATE/DELETE operations:
        document.setRev(updateDocumentResponse.getRev());
        System.out.println("You have updated the document:\n" + document);
    } catch (NotFoundException nfe) {
        System.out.println("Cannot update document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
    }
}
Also used : DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Document(com.ibm.cloud.cloudant.v1.model.Document) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)

Example 2 with NotFoundException

use of com.ibm.cloud.sdk.core.service.exception.NotFoundException in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierIT method dClassify.

/**
 * Test classify. Use the pre created classifier to avoid waiting for availability
 */
@Test
public void dClassify() {
    Classification classification = null;
    try {
        ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(preCreatedClassifierId).text("is it hot outside?").build();
        classification = service.classify(classifyOptions).execute().getResult();
    } catch (NotFoundException e) {
        // The build should not fail here, because this is out of our control.
        throw new AssumptionViolatedException(e.getMessage(), e);
    }
    assertNotNull(classification);
    assertEquals("temperature", classification.getTopClass());
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 3 with NotFoundException

use of com.ibm.cloud.sdk.core.service.exception.NotFoundException in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierIT method bGetClassifier.

/**
 * Test get classifier.
 */
@Test
public void bGetClassifier() {
    final Classifier classifier;
    try {
        GetClassifierOptions getOptions = new GetClassifierOptions.Builder().classifierId(classifierId).build();
        classifier = service.getClassifier(getOptions).execute().getResult();
    } catch (NotFoundException e) {
        // The build should not fail here, because this is out of our control.
        throw new AssumptionViolatedException(e.getMessage(), e);
    }
    assertNotNull(classifier);
    assertEquals(classifierId, classifier.getClassifierId());
    assertEquals(Classifier.Status.TRAINING, classifier.getStatus());
}
Also used : GetClassifierOptions(com.ibm.watson.natural_language_classifier.v1.model.GetClassifierOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 4 with NotFoundException

use of com.ibm.cloud.sdk.core.service.exception.NotFoundException in project java-sdk by watson-developer-cloud.

the class SynonymsIT method testDeleteSynonym.

/**
 * Test deleteSynonym.
 */
@Test
public void testDeleteSynonym() {
    String entity = "beverage";
    String entityValue = "orange juice";
    String synonym = "OJ";
    try {
        CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
        service.createEntity(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.createValue(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
    Synonym response = service.createSynonym(createOptions).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.synonym());
        assertEquals(response.synonym(), synonym);
    } catch (Exception ex) {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
        fail(ex.getMessage());
    }
    DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym).build();
    service.deleteSynonym(deleteOptions).execute().getResult();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.getSynonym(getOptions).execute().getResult();
        fail("deleteSynonym failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) GetSynonymOptions(com.ibm.watson.assistant.v1.model.GetSynonymOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) Test(org.junit.Test)

Example 5 with NotFoundException

use of com.ibm.cloud.sdk.core.service.exception.NotFoundException in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getEvents.

@Override
public List<CloudEvent<?, ?>> getEvents() {
    try {
        List<CloudEvent<?, ?>> events = new ArrayList<>();
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            @SuppressWarnings("rawtypes") CloudEventImpl evt = this.gson.fromJson(document.toString(), CloudEventImpl.class);
            events.add(evt);
        }
        return events;
    } catch (NotFoundException e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return Collections.emptyList();
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) CloudEvent(io.cloudevents.CloudEvent) CloudEventImpl(io.cloudevents.v02.CloudEventImpl)

Aggregations

NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)11 Test (org.junit.Test)7 Document (com.ibm.cloud.cloudant.v1.model.Document)4 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)3 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)3 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 AssumptionViolatedException (org.junit.AssumptionViolatedException)3 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)2 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)2 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)2 DocsResultRow (com.ibm.cloud.cloudant.v1.model.DocsResultRow)2 GetDocumentOptions (com.ibm.cloud.cloudant.v1.model.GetDocumentOptions)2 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)2 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)2 GsonBuilder (com.google.gson.GsonBuilder)1 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)1 UnauthorizedException (com.ibm.cloud.sdk.core.service.exception.UnauthorizedException)1 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)1 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)1 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)1