Java tutorial
package com.conwet.silbops.msg; /* * #%L * SilboPS API Broker * %% * 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 java.util.HashSet; import java.util.Set; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.junit.Test; import com.conwet.silbops.model.Subscription; import com.conwet.silbops.model.basic.Type; /** * * @author sergio */ public class UnsubscribeMsgTest { @Test public void shouldSerializeAndDeserialize() throws ParseException { UnsubscribeMsg msg = new UnsubscribeMsg(); Subscription uncovered = new Subscription().constrain("attr2", Type.STRING).eq("hello") .constrain("attr3", Type.DOUBLE).gt(11.0D).subscription(); msg.setSubscription( new Subscription().constrain("attr1", Type.STRING).exists().constrain("attr2", Type.STRING) .eq("hello").constrain("attr3", Type.DOUBLE).gt(10.3D).subscription()); Set<Subscription> uncovSet = new HashSet<>(); uncovSet.add(uncovered); msg.setUncovered(uncovSet); String jsonStr = msg.toJSONString(); JSONObject json = (JSONObject) new JSONParser().parse(jsonStr); UnsubscribeMsg newMsg = (UnsubscribeMsg) UnsubscribeMsg.fromJSON(json); assertThat(newMsg).isEqualTo(msg); assertThat(newMsg.getUncovered()).isEqualTo(uncovSet); } @Test public void shouldDeserializeOptionalUncovered() throws ParseException { // this test covers the case clients are sending unsubscribe UnsubscribeMsg msg = new UnsubscribeMsg(); msg.setSubscription( new Subscription().constrain("attr1", Type.STRING).exists().constrain("attr2", Type.STRING) .eq("hello").constrain("attr3", Type.DOUBLE).gt(10.3D).subscription()); Set<Subscription> uncovered = new HashSet<>(); uncovered.add(new Subscription().constrain("attr1", Type.STRING).exists().subscription()); msg.setUncovered(uncovered); JSONObject jsonTmp = msg.toJSON(); // delete uncovered part ((JSONObject) jsonTmp.get("payload")).remove("uncovered"); uncovered.clear(); String jsonStr = jsonTmp.toJSONString(); JSONObject json = (JSONObject) new JSONParser().parse(jsonStr); UnsubscribeMsg newMsg = (UnsubscribeMsg) UnsubscribeMsg.fromJSON(json); assertThat(newMsg).isEqualTo(msg); assertThat(newMsg.getUncovered()).isEqualTo(uncovered); } @Test public void shouldExternalizeAndDesexternalize() throws Exception { UnsubscribeMsg unSubMsg = new UnsubscribeMsg(); Subscription subscription = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D) .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST") .subscription(); unSubMsg.setSubscription(subscription); Subscription uncovered1 = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D) .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST") .constrain("attr4", Type.DOUBLE).le(7D).subscription(); Subscription uncovered2 = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D) .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST") .constrain("attr4", Type.LONG).gt(7L).subscription(); Set<Subscription> uncovered = new HashSet<>(); uncovered.add(uncovered1); uncovered.add(uncovered2); unSubMsg.setUncovered(uncovered); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutput output = new ObjectOutputStream(baos); output.writeObject(unSubMsg); output.close(); ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); assertThat((UnsubscribeMsg) input.readObject()).isEqualTo(unSubMsg); } }