Java tutorial
package com.conwet.silbops.model; /* * #%L * SilboPS API * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.junit.Before; import org.junit.Test; import com.conwet.silbops.model.basic.Attribute; import com.conwet.silbops.model.basic.Type; /** * * @author sergio */ public class SubscriptionTest { private Subscription subscription; @Before public void setUp() { subscription = new Subscription().constrain("symbol", Type.STRING).eq("GOOG").constrain("value", Type.LONG) .gt(10L).subscription(); } @Test public void shouldReturnAttributes() { assertThat(subscription.getAttributes()).containsOnly(new Attribute("symbol", Type.STRING), new Attribute("value", Type.LONG)); } @Test public void shouldReturnConstraintEntries() { assertThat(subscription.entries()).hasSize(2); } @Test public void shouldReturnSize() { assertThat(subscription.size()).isEqualTo(2); } @Test public void shouldBuildSubscription() { Attribute attribute = new Attribute("attr", Type.STRING); Subscription instance = new Subscription().constrain(attribute).exists().subscription(); assertThat(instance.getAttributes()).containsOnly(attribute); assertThat(instance.entries()).hasSize(1); } @Test public void shouldSetID() { String id = "myID"; subscription.setId(id); assertThat(subscription.getId()).isEqualTo(id); } @Test(expected = NullPointerException.class) public void shouldntSetNullID() { subscription.setId(null); } @Test public void shouldSetContextFunction() { ContextFunction func = new ContextFunction(); subscription.setContextFunction(func); assertThat(subscription.getContextFunction()).isEqualTo(func); } @Test(expected = NullPointerException.class) public void shouldntSetNullContextFunction() { subscription.setContextFunction(null); } @Test(expected = IllegalArgumentException.class) public void shouldntAddConstraintsWithDifferentTypeFromAttribute() { subscription.constrain("wrong", Type.LONG).startsWith("hi"); } // @Test // public void testMatch() { // // System.out.println("match"); // // assertThat(subscription.match(new Notification())).isFalse(); // assertThat(subscription.match(new Notification() // .attribute("symbol", "GOOG"))).isFalse(); // assertThat(subscription.match(new Notification() // .attribute("symbol", "GOOG") // .attribute("value", 20L))).isTrue(); // assertThat(subscription.match(new Notification() // .attribute("symbol", "GOOG") // .attribute("value", 20D))).isFalse(); // assertThat(subscription.match(new Notification() // .attribute("symbol", "GOOG") // .attribute("value", 20L) // .attribute("unrelated", "dont mind"))).isTrue(); // // Subscription instance = new Subscription() // .constrain("mandatory").exists(ValueType.STRING) // .constrain("starting").startsWith("hello") // .constrain("contains").contains("waldo") // .subscription(); // // assertThat(instance.match(new Notification() // .attribute("mandatory", "") // .attribute("starting", "hello world!") // .attribute("contains", "where is waldo?"))).isTrue(); // } @Test public void shouldMatchAdvertises() { Advertise exact = new Advertise().attribute("symbol", Type.STRING).attribute("value", Type.LONG); assertThat(subscription.match(exact)).isTrue(); Advertise partial = new Advertise().attribute("symbol", Type.STRING).attribute("value", Type.LONG) .attribute("other", Type.DOUBLE); assertThat(subscription.match(partial)).isTrue(); Advertise noMatch = new Advertise().attribute("symbol", Type.STRING).attribute("value", Type.DOUBLE); assertThat(subscription.match(noMatch)).isFalse(); } @Test public void shouldToJSONString() throws ParseException { // uses a JSON object to be independent of keys order. String string = "{\"id\":\"\",\"contextFunction\": {},\"constraints\": {" + "\"symbol:str\":[{\"=\":\"GOOG\"}]," + "\"value:long\":[{\">\": 10}]}}"; JSONObject json = (JSONObject) new JSONParser().parse(string); assertThat(subscription.toJSONString()).isEqualTo(json.toJSONString()); } @Test public void shouldToJSONStringWithExists() throws ParseException { String string = "{\"id\":\"\",\"contextFunction\": {},\"constraints\": {" + "\"attr:str\":[{\"exists\":\"\"}]}}"; JSONObject json = (JSONObject) new JSONParser().parse(string); Subscription attrExists = new Subscription().constrain("attr", Type.STRING).exists().subscription(); assertThat(attrExists.toJSONString()).isEqualTo(json.toJSONString()); } @Test @SuppressWarnings("unchecked") public void shouldJSONize() throws ParseException { String string = "{\"id\":\"\",\"contextFunction\":{}, \"constraints\":{" + "\"symbol:str\":[{\"=\":\"GOOG\"}]," + "\"value:long\":[{\">\": 10}]}}"; JSONObject json = (JSONObject) new JSONParser().parse(string); assertThat(subscription.toJSON()).isEqualTo(json); assertThat(subscription).isEqualTo(Subscription.fromJSON(json)); } @Test public void shouldExternalize() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutput output = new ObjectOutputStream(baos); output.writeObject(subscription); output.close(); ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); assertThat((Subscription) input.readObject()).isEqualTo(subscription); } }