/** * Method creates {@link SlotContract} contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. * @return created and ready {@link SlotContract} contract. * @throws IOException if something went wrong */ public static SlotContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new SlotContract().initializeWithDsl(binder); } }
/** * Create contract importing its parameters with passed .yaml file. No signatures are added automatically. It is required to add signatures before check. * @param fileName path to file containing Yaml representation of contract. */ public static Contract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new Contract().initializeWithDsl(binder); } }
/** * Method creates smart contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. */ public static NSmartContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new NSmartContract().initializeWithDsl(binder); } }
/** * Method creates {@link UnsContract} contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. */ public static UnsContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new UnsContract().initializeWithDsl(binder); } }
@Test public void testAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); Set<PublicKey> pubKeys = new HashSet<>(); pubKeys.add(keys.get(0).getPublicKey()); assertTrue(sr.isAllowedForKeys(pubKeys)); assertTrue(r1.isAllowedForKeys(pubKeys)); assertEquals(sr, r1); }
@Test public void serializeNone() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(r1.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertTrue(r1.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); }
@Test public void serializeMany() throws Exception { SimpleRole sr = new SimpleRole("tr1"); keys.forEach(k-> sr.addKeyRecord(new KeyRecord(k.getPublicKey()))); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); Set<PublicKey> kk = ((SimpleRole)r1).getSimpleKeys(); keys.forEach(k->assertTrue(kk.contains(k.getPublicKey()))); }
@Test public void serializeAny() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref"); }
@Test public void serializeAll() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ALL_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).iterator().next(),"ref"); }
@Test public void processBytes() throws Exception { byte x[] = Do.randomBytes(10); Binder s = DefaultBiMapper.serialize(x); byte[] result = ((Bytes)DefaultBiMapper.deserialize(s)).getData(); assertArrayEquals(x, result); } }
@Test public void autoSerializableBinder() throws Exception { // if the object is instance of Map (for example Binder), it could also have // it's own serialization: Rebinder b = new Rebinder(); DefaultBiMapper.registerClass(Rebinder.class); b.put("foo", "bar"); Binder s = DefaultBiMapper.serialize(b); Object x = DefaultBiMapper.deserialize(s); assertThat(x, instanceOf(Rebinder.class)); assertEquals("bar", ((Map)x).get("foo")); }
@Test public void serializeToBinder() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); // System.out.println(yaml.dump(b)); // System.out.println(yaml.dump(c1.serializeToBinder())); assertProperSimpleRootContract(c1); Contract c2 = c.copy(); assertProperSimpleRootContract(c2); }
@Test public void serializeAny() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void serializeWithMoreRoles() { SimpleRole s1 = new SimpleRole("s1"); SimpleRole s2 = new SimpleRole("s2"); ListRole lr1 = new ListRole("lr1", ListRole.Mode.ALL, Do.listOf(s1, s2)); ListRole lr2 = new ListRole("lr2", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertEquals(lr1.getRoles(), lr2.getRoles()); Binder blr = BossBiMapper.serialize(lr1); ListRole slr1 = DefaultBiMapper.deserialize(blr); blr = BossBiMapper.serialize(lr1); ListRole slr2 = DefaultBiMapper.deserialize(blr); assertEquals(slr1.getRoles(), slr2.getRoles()); }
@Test public void serializeQuorum() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); SimpleRole s3 = new SimpleRole("owner3"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); s3.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", 2, Do.listOf(s1, s2, s3)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void serialize() throws Exception { RoleLink r1 = new RoleLink("name", "target"); r1.addRequiredReference("ref", Role.RequiredMode.ALL_OF); Binder s = DefaultBiMapper.serialize(r1); RoleLink r2 = DefaultBiMapper.deserialize(s); assertEquals(r1, r2); assertEquals(r1.getName(), r2.getName()); }
@Test public void serializeAll() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); s1.addRequiredReference("refName", Role.RequiredMode.ALL_OF); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); ListRole roleList = new ListRole("listAllMode", ListRole.Mode.ALL, Do.listOf(s1)); c.registerRole(s1); c.registerRole(roleList); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void serializeContractWithListRole() throws Exception { Contract c = Contract.fromDslFile("./src/test_contracts/simple_root_contract.yml"); SimpleRole s1 = new SimpleRole("role"); ListRole listRole = new ListRole("owner", 1, Do.listOf(s1)); c.registerRole(listRole); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); assertEquals(c.getRole("owner"), c1.getRole("owner")); }
@Test public void autoSerializable() throws Exception { Test1 t1 = new Test1("foo"); DefaultBiMapper.registerClass(Test1.class); Binder s = DefaultBiMapper.serialize(t1); assertEquals(s.getStringOrThrow("__type"), "foobar1"); assertEquals(s.getStringOrThrow("value"), "foo"); Test1 t2 = DefaultBiMapper.deserialize(s); assertThat(t2, instanceOf(Test1.class) ); assertEquals("foo", t2.getValue()); }
@Test public void autoSerializableInheritance() throws Exception { Test1 t1 = new Test2("foo"); DefaultBiMapper.registerClass(Test2.class); Binder s = DefaultBiMapper.serialize(t1); assertEquals(s.getStringOrThrow("__type"), "net.sergeych.biserializer.Test2"); Test2 t2 = DefaultBiMapper.deserialize(s); assertThat(t2, instanceOf(Test1.class) ); assertEquals("bad", t2.getValue()); assertEquals("foo", t2.getBar()); }