SimpleCollectionTest.java

/***************************************************************************
   Copyright 2013 Emily Estes

   Licensed 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 net.metanotion.contentstore;


import java.util.Map;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.metanotion.io.JavaFileSystem;
import net.metanotion.json.JsonObject;
import net.metanotion.scripting.ObjectServer;
import net.metanotion.sql.DbUtil;
import net.metanotion.simpletemplate.ResourceDispatcher;
import net.metanotion.simpletemplate.ResourceFactory;
import net.metanotion.simpletemplate.StaticResources;
import net.metanotion.simpletemplate.MountPointResources;
import net.metanotion.util.AppUtil;
import net.metanotion.util.Extends;
import net.metanotion.util.Message;
import net.metanotion.util.Service;
import net.metanotion.util.Unknown;
import net.metanotion.util.UnknownMagic;
import net.metanotion.web.Raw;
import net.metanotion.web.RequestObject;
import net.metanotion.web.SessionFactory;
import net.metanotion.web.concrete.RawDispatcher;
import net.metanotion.web.concrete.HttpUtil;
import net.metanotion.web.concrete.URIPrefixDispatcher;
import net.metanotion.web.servlets.ServerUtil;

import net.metanotion.contentstore.simple.HttpSimpleCollection;
import net.metanotion.contentstore.simple.JList;
import net.metanotion.contentstore.simple.SimpleCollectionApi;

public final class SimpleCollectionTest {
	private static void assertTrue(final boolean result) {
		if(!result) { throw new AssertionError("Expected true"); }
	}

	private static final class Test {
		public String key;
		public Test() { }
		public Test(final String key) { this.key = key; }
		@Override public boolean equals(final Object o) {
			if(!(o instanceof Test)) { return false; }
			final Test t = (Test) o;
			return this.key.equals(t.key);
		}
		@Override public int hashCode() { return this.key.hashCode(); }
	}

	public static void main(final String[] args) throws Exception {
		final SimpleMemoryCollection<JsonObject> smc = new SimpleMemoryCollection<>();

		final JsonObject o1 = JsonObject.read("{ \"key\": \"A\" }");
		final JsonObject o2 = JsonObject.read("{ \"key\": \"B\" }");
		final SimpleCollection<JsonObject> c = smc.get("1");
		final String id1 = c.create(o1);
		c.get(id1).equals(o1);
		for(final Map.Entry<String, JsonObject> o: c.list(10, 0)) {
			assertTrue(id1.equals(o.getKey()));
			assertTrue(o1.equals(o.getValue()));
		}

		final SimpleMemoryCollection<Test> st = new SimpleMemoryCollection<>();
		final MarshalledSimpleCollection<Test> m = new MarshalledSimpleCollection<>(Test.class, st.get("1"));
		String mid1 = m.create(o1);
		assertTrue(o1.equals(m.get(mid1)));
		for(final Map.Entry<String, JsonObject> o: m.list(10, 0)) {
			assertTrue(mid1.equals(o.getKey()));
			assertTrue(o1.equals(o.getValue()));
		}
		m.update(mid1, o2);
		assertTrue(!o1.equals(m.get(mid1)));
		assertTrue(o2.equals(m.get(mid1)));
		assertTrue(m.remove(mid1));
		assertTrue(!m.remove(mid1));
		m.create(o1);
		m.create(o1);
		m.create(o1);
		m.create(o1);
		for(final Map.Entry<String, JsonObject> o: m.list(2, 1)) { }

		final HttpSimpleCollection sc = new HttpSimpleCollection("/simple/", 5);
		final SimpleCollectionApi scapi = sc.newSession(smc);
		assertTrue(o1.equals(scapi.create("test", o1).get("value")));
		for(final JsonObject o: scapi.list("test", 0).entries) {
			assertTrue(o1.equals(o.get("value")));
		}
	}
}