SchemaMetaDataManager { public SchemaMetaDataManager(String schemaRegistryLocation) throws IOException { _idNameMapFile = schemaRegistryLocation + "/" + SRC_ID_NAME_MAP_FILE; _physicalToLogicalSrcMapFile = schemaRegistryLocation + "/" + PHYSICAL_TO_LOGICAL_SRC_MAP_FILE; populatePhysicalToLogicalSrcMap(); populateSrcNameIdMap(); } SchemaMetaDataManager(String schemaRegistryLocation); void store(); short updateAndGetNewSrcId(String dbName, String srcName); List<String> getManagedSourcesForDB(String db); short getSrcId(String srcName); Map<String, TreeSet<String>> getDbToSrcMap(); static final String SRC_ID_NAME_MAP_FILE; static final String PHYSICAL_TO_LOGICAL_SRC_MAP_FILE; }
@Test public void testSchemaMetaDataManager() throws Exception { File schema_dir = FileUtils.createTempDir("dir_testSchemaMetaDataManager"); String[] idNameMapContents = { "1:com.linkedin.events.example.person", "2:com.linkedin.events.example.address", "3:com.linkedin.events.example.date", "10:com.linkedin.events.another_example.zipcode", }; String[] dbToSrcMapContents = { "{", " \"another_example\" : [ \"com.linkedin.events.another_example.zipcode\" ],", " \"example\" : [ \"com.linkedin.events.example.address\", \"com.linkedin.events.example.date\", \"com.linkedin.events.example.person\" ]", "}" }; String dbToSrcFile1 = schema_dir.getAbsolutePath() + "/physical_logical_src_map.json"; String idNameMapFile1 = schema_dir.getAbsolutePath() + "/idToName.map"; FileUtils.storeLinesToTempFile(dbToSrcFile1,dbToSrcMapContents); FileUtils.storeLinesToTempFile(idNameMapFile1, idNameMapContents); File newSchemaDir = FileUtils.createTempDir("dir_testSchemaMetaDataManager_backup"); String dbToSrcFile2 = newSchemaDir.getAbsolutePath() + "/physical_logical_src_map.json"; String idNameMapFile2 = newSchemaDir.getAbsolutePath() + "/idToName.map"; Runtime.getRuntime().exec("cp " + dbToSrcFile1 + " " + dbToSrcFile2 ); Runtime.getRuntime().exec("cp " + idNameMapFile1 + " " + idNameMapFile2 ); SchemaMetaDataManager mgr = new SchemaMetaDataManager(schema_dir.getAbsolutePath()); List<String> sources = mgr.getManagedSourcesForDB("another_example"); Assert.assertEquals( sources.size(), 1, "Number of Sources"); Assert.assertEquals(sources.get(0),"com.linkedin.events.another_example.zipcode","Number of Sources"); sources = mgr.getManagedSourcesForDB("example"); Assert.assertEquals(sources.size(),3, "Number of Sources"); Assert.assertEquals( sources.get(0), "com.linkedin.events.example.person", "Source 1"); Assert.assertEquals( sources.get(1), "com.linkedin.events.example.address", "Source 2"); Assert.assertEquals( sources.get(2), "com.linkedin.events.example.date","Source 3"); Assert.assertNull(mgr.getManagedSourcesForDB("unknown_source"),"Unknown Source"); Assert.assertEquals( mgr.getSrcId("com.linkedin.events.another_example.zipcode"),10,"ZipCode SrcId"); Assert.assertEquals( mgr.getSrcId("com.linkedin.events.example.person"),1, "Person SrcId"); Assert.assertEquals( mgr.getSrcId("com.linkedin.events.example.address"), 2, "Address SrcId"); Assert.assertEquals( mgr.getSrcId("com.linkedin.events.example.date"),3, "Date SrcId"); boolean exception = false; try { mgr.getSrcId("Unknown SrcId"); } catch (RuntimeException re) { exception = true; } Assert.assertTrue(exception, "Got exception ?"); mgr.store(); FileUtils.compareTwoTextFiles(dbToSrcFile1, dbToSrcFile2); FileUtils.compareTwoTextFiles(idNameMapFile1, idNameMapFile2); short srcId = mgr.updateAndGetNewSrcId("another_example", "com.linkedin.events.another_example.city"); Assert.assertEquals(srcId,11,"New SrcId "); sources = mgr.getManagedSourcesForDB("another_example"); Assert.assertEquals( sources.size(), 2, "Number of Sources"); Assert.assertEquals(sources.get(0),"com.linkedin.events.another_example.zipcode","Zipcode Sources"); Assert.assertEquals(sources.get(1),"com.linkedin.events.another_example.city","City Source "); Assert.assertEquals( mgr.getSrcId("com.linkedin.events.another_example.city"),11,"City SrcId"); sources = mgr.getManagedSourcesForDB("example"); Assert.assertEquals(sources.size(),3, "Number of Sources"); Assert.assertEquals( sources.get(0), "com.linkedin.events.example.person", "Source 1"); Assert.assertEquals( sources.get(1), "com.linkedin.events.example.address", "Source 2"); Assert.assertEquals( sources.get(2), "com.linkedin.events.example.date","Source 3"); Assert.assertNull(mgr.getManagedSourcesForDB("unknown_source"),"Unknown Source"); System.out.println(mgr.getDbToSrcMap()); }