InMemoryUsernameProvider implements UsernameProvider { public String toJson() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(this.getApplicationsMap()); return json; } InMemoryUsernameProvider(); String getInitFileLocation(); void setInitFileLocation(String initFileLocation); Map<String, ApplicationMappings> getApplicationsMap(); void setApplicationsMap(Map<String, ApplicationMappings> applicationsMap); @Override String getUsername(String externalApplicationName, String externalUsername); void addMapping(String app, String externalUsername, String pentahoUser); String removeMapping(String app, String externalUsername); String toJson(); void loadJsonMappings(String json); void loadJsonMappings(InputStream stream); void initFromConfiguration(); void loadJsonMappingsFromFile(String fileName); @Override boolean isAppNameMapped(String externalAppName); public String initFileLocation; }
@Test public void testToJson() throws IOException { inMemoryProvider.addMapping("app0", "user0.0", "pentaho0"); inMemoryProvider.addMapping("app0", "user0.1", "pentaho1"); inMemoryProvider.addMapping("app0", "user0.2", "pentaho2"); inMemoryProvider.addMapping("app1", "user0.0", "pentaho3"); inMemoryProvider.addMapping("app1", "user0.1", "pentaho4"); String json = inMemoryProvider.toJson(); System.out.println(json); ObjectMapper mapper = new ObjectMapper(); Object result = mapper.readValue(json, new TypeReference <Map<String, ApplicationMappings>>() { }); assertNotNull(result); @SuppressWarnings("unchecked") Map<String, ApplicationMappings> maps = (Map<String, ApplicationMappings>) result; InMemoryUsernameProvider checkProvider = new InMemoryUsernameProvider(); checkProvider.setApplicationsMap(maps); String pentahoUser = checkProvider.getUsername("app0", "user0.0"); assertNotNull(pentahoUser); assertEquals("pentaho0", pentahoUser); pentahoUser = checkProvider.getUsername("app1", "user0.1"); assertNotNull(pentahoUser); assertEquals("pentaho4", pentahoUser); }