Skip to content

Instantly share code, notes, and snippets.

@russelldb
Created August 31, 2011 09:13
Show Gist options
  • Save russelldb/1183136 to your computer and use it in GitHub Desktop.
Save russelldb/1183136 to your computer and use it in GitHub Desktop.
Test filter_notfound
/*
* This file is provided to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.basho.riak.client.http.itest;
import static com.basho.riak.client.http.Hosts.RIAK_URL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.basho.riak.client.http.RiakClient;
import com.basho.riak.client.http.RiakObject;
import com.basho.riak.client.http.mapreduce.ErlangFunction;
import com.basho.riak.client.http.request.MapReduceBuilder;
import com.basho.riak.client.http.response.MapReduceResponse;
/**
* Exercises map/reduce features of the Riak client. Assumes Riak is reachable
* at {@link com.basho.riak.client.http.Hosts#RIAK_URL }.
*
* @see com.basho.riak.client.http.Hosts#RIAK_URL
*/
public class ITestMapReduce {
public static String BUCKET_NAME = "mr_test_java";
public static int TEST_ITEMS = 20;
@BeforeClass public static void setup() {
RiakClient c = new RiakClient(RIAK_URL);
for (int i = 0; i < TEST_ITEMS; i++) {
RiakObject object = new RiakObject(BUCKET_NAME, "java_" + Integer.toString(i));
object.setContentType("text/plain");
object.setValue(Integer.toString(i));
c.store(object);
}
}
@AfterClass public static void teardown() {
RiakClient c = new RiakClient(RIAK_URL);
for (int i = 0; i < TEST_ITEMS; i++) {
c.delete(BUCKET_NAME, "java_" + Integer.toString(i));
}
}
@Test public void doErlangMapReduce_filterNotFound() throws IOException, JSONException {
RiakClient c = new RiakClient(RIAK_URL);
MapReduceBuilder builder = new MapReduceBuilder(c);
builder.addRiakObject(BUCKET_NAME, "java_0");
builder.addRiakObject(BUCKET_NAME, "java_1");
builder.addRiakObject(BUCKET_NAME, UUID.randomUUID().toString());
builder.addRiakObject(BUCKET_NAME, UUID.randomUUID().toString());
builder.addRiakObject(BUCKET_NAME, UUID.randomUUID().toString());
builder.map(new ErlangFunction("riak_kv_mapreduce", "map_object_value"), "filter_notfound", true);
MapReduceResponse response = builder.submit();
assertTrue(response.isSuccess());
JSONArray results = response.getResults();
assertEquals(2, results.length());
assertEquals(0, results.getInt(0));
assertEquals(1, results.getInt(1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment