Search in sources :

Example 1 with BucketConfiguration

use of org.couchbase.mock.BucketConfiguration in project apex-malhar by apache.

the class CouchBaseOutputOperatorTest method createMock.

protected CouchbaseMock createMock(String name, String password, BucketConfiguration bucketConfiguration) throws Exception {
    bucketConfiguration.numNodes = numNodes;
    bucketConfiguration.numReplicas = numReplicas;
    bucketConfiguration.name = name;
    bucketConfiguration.type = BucketType.COUCHBASE;
    bucketConfiguration.password = password;
    bucketConfiguration.hostname = "localhost";
    ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
    configList.add(bucketConfiguration);
    CouchbaseMock mockCouchbase = new CouchbaseMock(0, configList);
    return mockCouchbase;
}
Also used : CouchbaseMock(org.couchbase.mock.CouchbaseMock) ArrayList(java.util.ArrayList) BucketConfiguration(org.couchbase.mock.BucketConfiguration)

Example 2 with BucketConfiguration

use of org.couchbase.mock.BucketConfiguration in project apex-malhar by apache.

the class CouchBaseOutputOperatorTest method TestCouchBaseOutputOperator.

@Test
public void TestCouchBaseOutputOperator() throws InterruptedException, Exception {
    BucketConfiguration bucketConfiguration = new BucketConfiguration();
    CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
    CouchbaseMock mockCouchbase1 = createMock("default", "", bucketConfiguration);
    mockCouchbase1.start();
    mockCouchbase1.waitForStartup();
    List<URI> uriList = new ArrayList<URI>();
    int port1 = mockCouchbase1.getHttpPort();
    logger.debug("port is {}", port1);
    uriList.add(new URI("http", null, "localhost", port1, "/pools", "", ""));
    cfb.buildCouchbaseConnection(uriList, bucketConfiguration.name, bucketConfiguration.password);
    CouchBaseWindowStore store = new CouchBaseWindowStore();
    store.setBucket(bucketConfiguration.name);
    store.setPasswordConfig(password);
    store.setPassword(bucketConfiguration.password);
    store.setUriString("localhost:" + port1 + "," + "localhost:" + port1);
    try {
        store.connect();
    } catch (IOException ex) {
        DTThrowable.rethrow(ex);
    }
    store.getInstance().flush();
    store.getMetaInstance().flush();
    CouchbasePOJOSetOperator outputOperator = new CouchbasePOJOSetOperator();
    AttributeMap.DefaultAttributeMap attributeMap = new AttributeMap.DefaultAttributeMap();
    attributeMap.put(DAG.APPLICATION_ID, APP_ID);
    OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
    outputOperator.setStore(store);
    outputOperator.setup(context);
    ArrayList<String> expressions = new ArrayList<String>();
    expressions.add("getKey()");
    expressions.add("getTest()");
    outputOperator.setExpressions(expressions);
    CouchBaseJSONSerializer serializer = new CouchBaseJSONSerializer();
    outputOperator.setSerializer(serializer);
    TestPojo obj = new TestPojo();
    obj.setName("test");
    obj.setPhone(123344555);
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("test", 12345);
    obj.setMap(map);
    TestEvent1 testEvent = new TestEvent1();
    testEvent.setKey("key1");
    testEvent.setTest(obj);
    outputOperator.beginWindow(0);
    outputOperator.input.process(testEvent);
    outputOperator.endWindow();
    Assert.assertEquals("Value in couchbase is", "{\"name\":\"test\",\"map\":{\"test\":12345},\"phone\":123344555}", store.getInstance().get("key1").toString());
    outputOperator.teardown();
    outputOperator = new CouchbasePOJOSetOperator();
    store = new CouchBaseWindowStore();
    store.setBucket(bucketConfiguration.name);
    store.setPasswordConfig(password);
    store.setPassword(bucketConfiguration.password);
    store.setUriString("localhost:" + port1 + "," + "localhost:" + port1);
    try {
        store.connect();
    } catch (IOException ex) {
        DTThrowable.rethrow(ex);
    }
    store.getInstance().flush();
    store.getMetaInstance().flush();
    outputOperator.setStore(store);
    outputOperator.setup(context);
    expressions = new ArrayList<String>();
    expressions.add("getKey()");
    expressions.add("getNum()");
    outputOperator.setExpressions(expressions);
    TestEvent2 simpleEvent = new TestEvent2("key2", 123);
    outputOperator.beginWindow(0);
    outputOperator.input.process(simpleEvent);
    outputOperator.endWindow();
    Assert.assertEquals("Value in couchbase is", "123", store.getInstance().get("key2").toString());
    outputOperator.teardown();
    mockCouchbase1.stop();
}
Also used : CouchbaseMock(org.couchbase.mock.CouchbaseMock) CouchbaseConnectionFactoryBuilder(com.couchbase.client.CouchbaseConnectionFactoryBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BucketConfiguration(org.couchbase.mock.BucketConfiguration) IOException(java.io.IOException) URI(java.net.URI) AttributeMap(com.datatorrent.api.Attribute.AttributeMap) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) Test(org.junit.Test)

Example 3 with BucketConfiguration

use of org.couchbase.mock.BucketConfiguration in project apex-malhar by apache.

the class CouchBaseInputOperatorTest method createMock.

protected CouchbaseMock createMock(String name, String password, BucketConfiguration bucketConfiguration) throws Exception {
    bucketConfiguration.numNodes = numNodes;
    bucketConfiguration.numReplicas = numReplicas;
    bucketConfiguration.name = name;
    bucketConfiguration.type = BucketType.COUCHBASE;
    bucketConfiguration.password = password;
    bucketConfiguration.hostname = "localhost";
    ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
    configList.add(bucketConfiguration);
    CouchbaseMock mockCouchbase = new CouchbaseMock(0, configList);
    return mockCouchbase;
}
Also used : CouchbaseMock(org.couchbase.mock.CouchbaseMock) ArrayList(java.util.ArrayList) BucketConfiguration(org.couchbase.mock.BucketConfiguration)

Example 4 with BucketConfiguration

use of org.couchbase.mock.BucketConfiguration in project apex-malhar by apache.

the class CouchBaseInputOperatorTest method TestCouchBaseInputOperator.

@Test
public void TestCouchBaseInputOperator() throws Exception {
    BucketConfiguration bucketConfiguration = new BucketConfiguration();
    CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
    CouchbaseMock mockCouchbase1 = createMock("default", "", bucketConfiguration);
    CouchbaseMock mockCouchbase2 = createMock("default", "", bucketConfiguration);
    mockCouchbase1.start();
    mockCouchbase1.waitForStartup();
    List<URI> uriList = new ArrayList<URI>();
    int port1 = mockCouchbase1.getHttpPort();
    logger.debug("port is {}", port1);
    mockCouchbase2.start();
    mockCouchbase2.waitForStartup();
    int port2 = mockCouchbase2.getHttpPort();
    logger.debug("port is {}", port2);
    uriList.add(new URI("http", null, "localhost", port1, "/pools", "", ""));
    connectionFactory = cfb.buildCouchbaseConnection(uriList, bucketConfiguration.name, bucketConfiguration.password);
    client = new CouchbaseClient(connectionFactory);
    CouchBaseStore store = new CouchBaseStore();
    keyList = new ArrayList<String>();
    store.setBucket(bucketConfiguration.name);
    store.setPasswordConfig(password);
    store.setPassword(bucketConfiguration.password);
    store.setUriString("localhost:" + port1 + "," + "localhost:" + port1);
    // couchbaseBucket.getCouchServers();
    AttributeMap.DefaultAttributeMap attributeMap = new AttributeMap.DefaultAttributeMap();
    attributeMap.put(DAG.APPLICATION_ID, APP_ID);
    TestInputOperator inputOperator = new TestInputOperator();
    inputOperator.setStore(store);
    inputOperator.insertEventsInTable(10);
    CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
    inputOperator.outputPort.setSink(sink);
    List<Partition<AbstractCouchBaseInputOperator<String>>> partitions = Lists.newArrayList();
    Collection<Partition<AbstractCouchBaseInputOperator<String>>> newPartitions = inputOperator.definePartitions(partitions, new PartitioningContextImpl(null, 0));
    Assert.assertEquals(2, newPartitions.size());
    for (Partition<AbstractCouchBaseInputOperator<String>> p : newPartitions) {
        Assert.assertNotSame(inputOperator, p.getPartitionedInstance());
    }
    // Collect all operators in a list
    List<AbstractCouchBaseInputOperator<String>> opers = Lists.newArrayList();
    for (Partition<AbstractCouchBaseInputOperator<String>> p : newPartitions) {
        TestInputOperator oi = (TestInputOperator) p.getPartitionedInstance();
        oi.setServerURIString("localhost:" + port1);
        oi.setStore(store);
        oi.setup(null);
        oi.outputPort.setSink(sink);
        opers.add(oi);
        port1 = port2;
    }
    sink.clear();
    int wid = 0;
    for (int i = 0; i < 10; i++) {
        for (AbstractCouchBaseInputOperator<String> o : opers) {
            o.beginWindow(wid);
            o.emitTuples();
            o.endWindow();
        }
        wid++;
    }
    Assert.assertEquals("Tuples read should be same ", 10, sink.collectedTuples.size());
    for (AbstractCouchBaseInputOperator<String> o : opers) {
        o.teardown();
    }
    mockCouchbase1.stop();
    mockCouchbase2.stop();
}
Also used : CouchbaseConnectionFactoryBuilder(com.couchbase.client.CouchbaseConnectionFactoryBuilder) ArrayList(java.util.ArrayList) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient) Partition(com.datatorrent.api.Partitioner.Partition) CouchbaseMock(org.couchbase.mock.CouchbaseMock) BucketConfiguration(org.couchbase.mock.BucketConfiguration) AttributeMap(com.datatorrent.api.Attribute.AttributeMap) PartitioningContextImpl(org.apache.apex.malhar.lib.partitioner.StatelessPartitionerTest.PartitioningContextImpl) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 BucketConfiguration (org.couchbase.mock.BucketConfiguration)4 CouchbaseMock (org.couchbase.mock.CouchbaseMock)4 CouchbaseConnectionFactoryBuilder (com.couchbase.client.CouchbaseConnectionFactoryBuilder)2 AttributeMap (com.datatorrent.api.Attribute.AttributeMap)2 URI (java.net.URI)2 Test (org.junit.Test)2 CouchbaseClient (com.couchbase.client.CouchbaseClient)1 OperatorContext (com.datatorrent.api.Context.OperatorContext)1 Partition (com.datatorrent.api.Partitioner.Partition)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 OperatorContextTestHelper.mockOperatorContext (org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext)1 PartitioningContextImpl (org.apache.apex.malhar.lib.partitioner.StatelessPartitionerTest.PartitioningContextImpl)1 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)1