@Override public String renderSchemaEntry(SchemaEntry schemaEntry, boolean requestForLatest) { return renderTemplate("Schema with ID = " + schemaEntry.getId() + (requestForLatest ? " (latest)" : ""), "schemaEntry", Collections.singletonMap("schemaEntry", schemaEntry)); }
/** * Decode a string created by {@link #schemasToString(Iterable)} * * @param str * The String to decode * @return An {@link java.lang.Iterable} of {@link SchemaEntry} */ public static Iterable<SchemaEntry> schemasFromString(String str) { List<SchemaEntry> schemas = new ArrayList<SchemaEntry>(); if (str != null && !str.isEmpty()) { String[] strs = str.split("\n"); for (String s : strs) { schemas.add(new SchemaEntry(s)); } } return schemas; }
@Test public void testToString() { SchemaEntry entry = new SchemaEntry("id", "schema"); SchemaEntry toAndFrom = new SchemaEntry(entry.toString()); Assert.assertEquals(entry, toAndFrom); }
@Test public void testGetters() { String id = "id"; String sc = "sc"; SchemaEntry entry = new SchemaEntry(id, sc); Assert.assertEquals(id, entry.getId()); Assert.assertEquals(sc, entry.getSchema()); }
sub.registerIfLatest(FOO, new SchemaEntry("not", "there")); Assert.assertNull( "registerIfLatest must return null if there are no schemas in the " + boolean foundfoo = false, foundbar = false; for (SchemaEntry s : sub3.allEntries()) { if (s.equals(foo)) { foundfoo = true; if (s.equals(bar)) { foundbar = true; SchemaEntry resultfoo = subject.lookupBySchema(foo.getSchema()); Assert.assertEquals("lookup by Schema did not return same result", foo, resultfoo); Assert.assertNull("non existent schema should return null", notThere); SchemaEntry resultfooid = subject.lookupById(foo.getId()); Assert.assertEquals("lookup by ID did not return same result", foo, resultfooid); Integer.parseInt(resultfoo.getId());
@Test public void testEqualsAndHashCode() { SchemaEntry entry = new SchemaEntry("id", "schema"); SchemaEntry entry2 = new SchemaEntry("id", "schema"); SchemaEntry nullId = new SchemaEntry(null, "schema"); SchemaEntry nullSchema = new SchemaEntry("id", null); SchemaEntry idDiffers = new SchemaEntry("iddd", "schema"); SchemaEntry sDiffers = new SchemaEntry("id", "schemaaaa"); SchemaEntry allNull = new SchemaEntry(null, null); SchemaEntry allNull2 = new SchemaEntry(null, null); Assert.assertEquals(entry, entry); Assert.assertEquals(entry, entry2); Assert.assertEquals(entry2, entry); Assert.assertEquals(entry.hashCode(), entry2.hashCode()); Assert.assertFalse(entry.equals(null)); Assert.assertFalse(entry.equals(new Object())); Assert.assertFalse(entry.equals(nullId)); Assert.assertFalse(nullId.equals(entry)); Assert.assertFalse(entry.equals(nullSchema)); Assert.assertFalse(nullSchema.equals(entry)); Assert.assertFalse(entry.equals(idDiffers)); Assert.assertFalse(idDiffers.equals(entry)); Assert.assertFalse(entry.equals(sDiffers)); Assert.assertFalse(sDiffers.equals(entry)); Assert.assertEquals(allNull, allNull2); Assert.assertEquals(allNull.hashCode(), allNull2.hashCode()); }
SchemaEntry schemaEntry = subject.lookupById(convertId.toString(id)); if (schemaEntry != null) { schema = convertSchema.fromString(schemaEntry.getSchema());
@Override public synchronized SchemaEntry registerIfLatest(String schema, SchemaEntry latest) throws SchemaValidationException { if (latest == this.latest || (latest != null && latest.equals(this.latest))) { return register(schema); } else { return null; } }
SchemaEntry schemaEntry = subject.lookupById(convertId.toString(id)); if (schemaEntry != null) { schema = convertSchema.fromString(schemaEntry.getSchema());
@Override public synchronized SchemaEntry registerIfLatest(String schema, SchemaEntry latest) throws SchemaValidationException { if (latest == this.latest || (latest != null && latest.equals(this.latest))) { return register(schema); } else { return null; } }
/** * Decode a string created by {@link #schemasToString(Iterable)} * * @param str * The String to decode * @return An {@link java.lang.Iterable} of {@link SchemaEntry} */ public static Iterable<SchemaEntry> schemasFromString(String str) { List<SchemaEntry> schemas = new ArrayList<SchemaEntry>(); if (str != null && !str.isEmpty()) { String[] strs = str.split("\n"); for (String s : strs) { schemas.add(new SchemaEntry(s)); } } return schemas; }
@Override public String renderSchemaEntry(SchemaEntry schemaEntry, boolean requestForLatest) { return renderTemplate("Schema with ID = " + schemaEntry.getId() + (requestForLatest ? " (latest)" : ""), "schemaEntry", Collections.singletonMap("schemaEntry", schemaEntry)); }
/** * Encode {@link SchemaEntry} objects into a {@link String} for use by * {@link #schemasFromString(String)} * * @param allEntries * the SchemaEntry objects to encode * @return The {@link SchemaEntry} objects encoded as a String */ public static String schemasToString(Iterable<SchemaEntry> allEntries) { StringBuilder sb = new StringBuilder(); boolean scheamWithNewLine = false; for (SchemaEntry s : allEntries) { if (s.getSchema().contains("\n")) { scheamWithNewLine = true; } sb.append(s.toString()).append("\n"); } if (scheamWithNewLine) { return new StringBuilder() .append(MessageStrings.SCHEMA_WITH_NEWLINE_ERROR) .append(sb).toString(); } else { return sb.toString(); } }
@Test public void testAllEntriesMultiLineSchema() throws Exception { String endOfLine = System.getProperty("line.separator"); String multiLineSchema1 = "first line" + endOfLine + "second line"; String multiLineSchema2 = "first line" + endOfLine + "second line" + endOfLine; repo.register("sub1", null).register(multiLineSchema1); repo.register("sub1", null).register(multiLineSchema2); Subject s1 = repo.lookup("sub1"); Assert.assertNotNull(s1); Iterable<SchemaEntry> allEntries = s1.allEntries(); boolean foundSub1 = false, foundSub2 = false; for (SchemaEntry entry: allEntries) { if (entry.getSchema().equals(multiLineSchema1)) { foundSub1 = true; } else if (entry.getSchema().equals(multiLineSchema2)) { foundSub2 = true; } } Assert.assertTrue("Check that allEntries() returns proper multi-line schemas", foundSub1 && foundSub2); }
@Override public synchronized SchemaEntry registerIfLatest(String schema, SchemaEntry latest) throws SchemaValidationException { isValid(); if (latest == this.latest // both null || (latest != null && latest.equals(this.latest))) { return register(schema); } else { return null; } }
@Override public synchronized SchemaEntry register(String schema) throws SchemaValidationException { String id = String.valueOf(nextId); SchemaEntry toRegister = new SchemaEntry(id, schema); SchemaEntry valueInCache = schemas.add(toRegister); if (toRegister == valueInCache) { // schema is new nextId++; this.latest = toRegister; } return valueInCache; }