Search in sources :

Example 6 with NotFoundException

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

the class CloudEventStoreCloudant method removeAllEvents.

@Override
public void removeAllEvents() throws Exception {
    try {
        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();
            DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(this.dbName).docId(document.getId()).rev(document.getRev()).build();
            DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
            if (!deleteDocumentResponse.isOk()) {
                logger.info("Could not delete a document.");
            }
        }
    } catch (NotFoundException e) {
        String errMsg = "Unable to retrieve all documents from Cloudant";
        logger.error(errMsg, e);
        throw new Exception(errMsg, e);
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GsonBuilder(com.google.gson.GsonBuilder) 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) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 7 with NotFoundException

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

the class AssistantServiceIT method testListCounterexamples.

/**
 * Test listCounterexamples.
 */
@Test
public void testListCounterexamples() {
    String counterExampleText = // gotta be unique
    "Make me a " + UUID.randomUUID().toString() + " sandwich";
    try {
        ListCounterexamplesOptions listOptions = new ListCounterexamplesOptions.Builder(workspaceId).build();
        CounterexampleCollection ccResponse = service.listCounterexamples(listOptions).execute().getResult();
        assertNotNull(ccResponse);
        assertNotNull(ccResponse.getCounterexamples());
        assertNotNull(ccResponse.getPagination());
        assertNotNull(ccResponse.getPagination().getRefreshUrl());
        // nextUrl may be null
        Date start = new Date();
        // Now add a counterexample and make sure we get it back
        CreateCounterexampleOptions createOptions = new CreateCounterexampleOptions.Builder(workspaceId, counterExampleText).build();
        service.createCounterexample(createOptions).execute().getResult();
        long count = ccResponse.getCounterexamples().size();
        CounterexampleCollection ccResponse2 = service.listCounterexamples(listOptions.newBuilder().pageLimit(count + 1).includeAudit(true).build()).execute().getResult();
        assertNotNull(ccResponse2);
        assertNotNull(ccResponse2.getCounterexamples());
        List<Counterexample> counterexamples = ccResponse2.getCounterexamples();
        assertTrue(counterexamples.size() > count);
        Counterexample exResponse = null;
        for (Counterexample resp : counterexamples) {
            if (resp.text().equals(counterExampleText)) {
                exResponse = resp;
                break;
            }
        }
        assertNotNull(exResponse);
        Date now = new Date();
        assertTrue(fuzzyBefore(exResponse.created(), now));
        assertTrue(fuzzyAfter(exResponse.created(), start));
        assertTrue(fuzzyBefore(exResponse.updated(), now));
        assertTrue(fuzzyAfter(exResponse.updated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        try {
            DeleteCounterexampleOptions deleteOptions = new DeleteCounterexampleOptions.Builder(workspaceId, counterExampleText).build();
            service.deleteCounterexample(deleteOptions).execute();
        } catch (NotFoundException ex) {
        // Okay
        }
    }
}
Also used : NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Date(java.util.Date) UnauthorizedException(com.ibm.cloud.sdk.core.service.exception.UnauthorizedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Test(org.junit.Test)

Example 8 with NotFoundException

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

the class EntitiesIT method testDeleteEntity.

/**
 * Test deleteEntity.
 */
@Test
public void testDeleteEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).build();
    Entity response = service.createEntity(options).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), entity);
        assertNull(response.getDescription());
        assertNull(response.getMetadata());
        assertTrue(response.isFuzzyMatch() == null || response.isFuzzyMatch().equals(Boolean.FALSE));
    } catch (Exception ex) {
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute().getResult();
        fail(ex.getMessage());
    }
    DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
    service.deleteEntity(deleteOptions).execute().getResult();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
        service.getEntity(getOptions).execute().getResult();
        fail("deleteEntity failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) GetEntityOptions(com.ibm.watson.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 9 with NotFoundException

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

the class ValuesIT method testDeleteValue.

/**
 * Test deleteValue.
 */
@Test
public void testDeleteValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    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"));
    }
    CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
    Value response = service.createValue(createOptions).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.value());
        assertEquals(response.value(), entityValue);
        assertNull(response.metadata());
    } catch (Exception ex) {
        // Clean up
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute().getResult();
        fail(ex.getMessage());
    }
    DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
    service.deleteValue(deleteOptions).execute().getResult();
    try {
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.getValue(getOptions).execute().getResult();
        fail("deleteValue failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : GetValueOptions(com.ibm.watson.assistant.v1.model.GetValueOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.assistant.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) Value(com.ibm.watson.assistant.v1.model.Value) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Test(org.junit.Test)

Example 10 with NotFoundException

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

the class DeleteDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Delete the document =============================================
    // Set the options to get the document out of the database if it exists
    String exampleDbName = "orders";
    String exampleDocId = "example";
    GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId(exampleDocId).build();
    try {
        // Try to get the document if it previously existed in the database
        Document document = client.getDocument(documentInfoOptions).execute().getResult();
        // Delete the document from the database
        DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(exampleDbName).docId(// docId is required for DELETE
        exampleDocId).rev(// rev is required for DELETE
        document.getRev()).build();
        DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
        if (deleteDocumentResponse.isOk()) {
            System.out.println("You have deleted the document.");
        }
    } catch (NotFoundException nfe) {
        System.out.println("Cannot delete 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) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)

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