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 com.conwet.silbops.model.Advertise; import com.conwet.silbops.model.basic.Type; 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 static org.assertj.core.api.Assertions.*; /** * * @author sergio */ public class UnadvertiseMsgTest { @Test public void shouldSerializeAndDeserialize() throws ParseException { Set<Advertise> uncovSet = new HashSet<>(); uncovSet.add(new Advertise().attribute("attr2", Type.STRING).attribute("attr3", Type.DOUBLE)); UnadvertiseMsg msg = new UnadvertiseMsg(); msg.setUncovered(uncovSet); msg.setAdvertise(new Advertise().attribute("attr1", Type.STRING).attribute("attr3", Type.DOUBLE)); String jsonStr = msg.toJSONString(); JSONObject json = (JSONObject) new JSONParser().parse(jsonStr); UnadvertiseMsg newMsg = (UnadvertiseMsg) UnadvertiseMsg.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 UnadvertiseMsg msg = new UnadvertiseMsg(); msg.setAdvertise(new Advertise().attribute("attr1", Type.STRING).attribute("attr2", Type.STRING) .attribute("attr3", Type.DOUBLE)); Advertise covered = new Advertise(); covered.attribute("attrCov", Type.DOUBLE); Set<Advertise> uncovered = new HashSet<>(); uncovered.add(covered); 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); UnadvertiseMsg newMsg = (UnadvertiseMsg) UnadvertiseMsg.fromJSON(json); assertThat(newMsg).isEqualTo(msg); assertThat(newMsg.getUncovered()).isEqualTo(uncovered); } @Test public void shouldExternalize() throws Exception { UnadvertiseMsg unAdvMsg = new UnadvertiseMsg(); Advertise subscription = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG) .attribute("attr3", Type.STRING); unAdvMsg.setAdvertise(subscription); Advertise uncovered1 = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG) .attribute("attr3", Type.STRING).attribute("attr4", Type.DOUBLE); Advertise uncovered2 = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG) .attribute("attr3", Type.STRING).attribute("attr5", Type.LONG); Set<Advertise> uncovered = new HashSet<>(); uncovered.add(uncovered1); uncovered.add(uncovered2); unAdvMsg.setUncovered(uncovered); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutput output = new ObjectOutputStream(baos); output.writeObject(unAdvMsg); output.close(); ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); assertThat((UnadvertiseMsg) input.readObject()).isEqualTo(unAdvMsg); } }