Java Code Examples for com.couchbase.client.core.endpoint.kv.AuthenticationException

The following examples show how to use com.couchbase.client.core.endpoint.kv.AuthenticationException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void shouldFailObservableIfErrorOnConnect() {
    InstrumentedService service = new InstrumentedService(host, bucket, password, port, ctx, factory);

    Endpoint endpoint = mock(Endpoint.class);
    final EndpointStates endpointStates = new EndpointStates(LifecycleState.DISCONNECTED);
    when(endpoint.states()).thenReturn(endpointStates.states());
    when(endpoint.connect()).thenReturn(Observable.<LifecycleState>error(new AuthenticationException()));
    when(factory.create(host, bucket, bucket, password, port, ctx)).thenReturn(endpoint);

    CouchbaseRequest req = mock(CouchbaseRequest.class);
    AsyncSubject<CouchbaseResponse> reqObservable = AsyncSubject.create();
    when(req.observable()).thenReturn(reqObservable);

    try {
        service.send(req);
        reqObservable.toBlocking().single();
        assertTrue("Should've failed but did not", false);
    } catch(AuthenticationException ex) {
        assertTrue(true);
        assertEquals(LifecycleState.IDLE, service.state());
    } catch(Throwable tr) {
        assertTrue(tr.getMessage(), false);
    }
}
 
Example 2
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCouchbaseConfigurationError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(new AuthenticationException());
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    try {
        testRunner.run();
        fail("ProcessException should be thrown.");
    } catch (AssertionError e) {
        Assert.assertTrue(e.getCause().getClass().equals(ProcessException.class));
        Assert.assertTrue(e.getCause().getCause().getClass().equals(AuthenticationException.class));
    }

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
 
Example 3
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCouchbaseConfigurationError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(new AuthenticationException());
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    try {
        testRunner.run();
        fail("ProcessException should be thrown.");
    } catch (AssertionError e) {
        Assert.assertTrue(e.getCause().getClass().equals(ProcessException.class));
        Assert.assertTrue(e.getCause().getCause().getClass().equals(AuthenticationException.class));
    }

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
 
Example 4
Source Project: couchbase-jvm-core   Source File: ObserveViaCAS.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Build an {@link ObserveItem} state from a {@link ObserveResponse}.
 *
 * @param id the observed key.
 * @param response the {@link ObserveResponse} received for that key.
 * @param cas the cas that we expect.
 * @param remove true if this is a remove operation, false otherwise.
 * @param persistIdentifier the {@link ObserveResponse.ObserveStatus} to watch for in persistence.
 * @param replicaIdentifier the {@link ObserveResponse.ObserveStatus} to watch for in replication.
 * @throws DocumentConcurrentlyModifiedException if the cas observed on master copy isn't the expected one.
 */
public ObserveItem(String id, ObserveResponse response, long cas, boolean remove,
                   ObserveResponse.ObserveStatus persistIdentifier,
                   ObserveResponse.ObserveStatus replicaIdentifier) {
    int replicated = 0;
    int persisted = 0;
    boolean persistedMaster = false;


    if (response.content() != null && response.content().refCnt() > 0) {
        response.content().release();
    }
    ObserveResponse.ObserveStatus status = response.observeStatus();

    if (response.status() == ResponseStatus.ACCESS_ERROR) {
        String details = ResponseStatusDetails.stringify(response.status(), response.statusDetails());
        throw new AuthenticationException("The application is not authorized to perform the \"observe\" "
            + "operation, make sure you have read privileges on this bucket: " + details);
    }

    // the CAS values always need to match up to make sure we are still observing the right
    // document. The only exclusion from that rule is when a real delete is returned, because
    // then the cas value is 0.
    boolean validCas = cas == response.cas()
            || (remove && response.cas() == 0 && status == persistIdentifier);

    if (response.master()) {
        if (!validCas) {
            throw new DocumentConcurrentlyModifiedException("The CAS on the active node "
                    + "changed for ID \"" + id + "\", indicating it has been modified in the "
                    + "meantime.", cas);
        }

        if (status == persistIdentifier) {
            persisted++;
            persistedMaster = true;
        }
    } else if (validCas) {
        if (status == persistIdentifier) {
            persisted++;
            replicated++;
        } else if (status == replicaIdentifier) {
            replicated++;
        }
    }

    this.replicated = replicated;
    this.persisted = persisted;
    this.persistedMaster = persistedMaster;
}