Skip to content
Snippets Groups Projects
Commit a8b4ef43 authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Added easier to use CouchDB view

parent 84d5a26f
Branches
Tags
No related merge requests found
/*
* Copyright (C) 2012 THM webMedia
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.dao;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.fourspaces.couchdb.View;
public class NovaView extends View {
public NovaView(String fullname) {
super(fullname);
}
@Override
public void setStartKey(String key) {
this.startKey = quote(key);
}
@Override
public void setEndKey(String key) {
this.endKey = quote(key);
}
public void setStartKey(String... keys) {
this.startKey = toJsonArray(keys);
}
public void setEndKey(String... keys) {
this.endKey = toJsonArray(keys);
}
@Override
public void setKey(String key) {
this.key = quote(key);
}
public void setKey(String... keys) {
this.key = toJsonArray(keys);
}
private String toJsonArray(String[] strings) {
StringBuilder sb = new StringBuilder();
for (String str : strings) {
if (str.equals("{}")) {
sb.append(str + ",");
} else {
sb.append("\"" + str + "\"" + ",");
}
}
sb.replace(sb.length() - 1, sb.length(), ""); // remove final comma
sb.insert(0, "[");
sb.append("]");
return encode(sb.toString());
}
private String quote(String string) {
return encode("\"" + string + "\"");
}
private String encode(String string) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Since we're using 'UTF-8', this should Exception should never occur.
}
return "";
}
}
/*
* Copyright (C) 2012 THM webMedia
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.dao;
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.junit.Test;
public class NovaViewTest {
@Test
public void setKeyShouldAcceptSingleArgument() {
NovaView v = new NovaView(null);
v.setKey("foo");
assertEncodedEquals("key", "\"foo\"", v.getQueryString());
}
@Test
public void setKeyShouldAcceptMultipleArgument() {
NovaView v = new NovaView(null);
v.setKey("foo", "bar", "baz");
assertEncodedEquals("key", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString());
}
@Test
public void setStartKeyShouldAcceptSingleArgument() {
NovaView v = new NovaView(null);
v.setStartKey("foo");
assertEncodedEquals("startkey", "\"foo\"", v.getQueryString());
}
@Test
public void setEndKeyShouldAcceptSingleArgument() {
NovaView v = new NovaView(null);
v.setEndKey("foo");
assertEncodedEquals("endkey", "\"foo\"", v.getQueryString());
}
@Test
public void setStartKeyShouldAcceptMultipleArgument() {
NovaView v = new NovaView(null);
v.setStartKey("foo", "bar", "baz");
assertEncodedEquals("startkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString());
}
@Test
public void setEndKeyShouldAcceptMultipleArgument() {
NovaView v = new NovaView(null);
v.setEndKey("foo", "bar", "baz");
assertEncodedEquals("endkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString());
}
@Test
public void keysShouldSupportEmptyObject() {
NovaView v = new NovaView(null);
v.setKey("foo", "bar", "{}");
assertEncodedEquals("key", "[\"foo\",\"bar\",{}]", v.getQueryString());
}
private void assertEncodedEquals(String key, String expected, String actual) {
try {
assertEquals(key + "=" + URLEncoder.encode(expected, "UTF-8"), actual);
} catch (UnsupportedEncodingException e) {
fail(e.getLocalizedMessage());
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment