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.");
}
}
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());
}
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());
}
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);
}
}
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();
}
}
Aggregations