UITemplateInitializer { public List<UITemplate> read() { List<UITemplate> uiTemplates = new ArrayList<>(); if (Objects.nonNull(yamlData)) { List templates = (List) yamlData.get("templates"); if (templates != null) { templates.forEach(templateObj -> { final Map template = (Map) templateObj; UITemplate newTemplate = new UITemplate(); final String name = (String) template.get("name"); if (StringUtil.isEmpty(name)) { throw new IllegalArgumentException("template name shouldn't be null"); } newTemplate.setName(name); final String type = (String) template.getOrDefault("type", TemplateType.DASHBOARD.name()); TemplateType.forName(type); newTemplate.setType(type); final String configuration = (String) template.get("configuration"); if (StringUtil.isEmpty(configuration)) { throw new IllegalArgumentException("template configuration shouldn't be null"); } newTemplate.setConfiguration(configuration); newTemplate.setActivated( BooleanUtils.booleanToValue( (Boolean) template.getOrDefault("activated", false) ) ); newTemplate.setDisabled( BooleanUtils.booleanToValue( (Boolean) template.getOrDefault("disabled", false) ) ); if (uiTemplates.contains(newTemplate)) { throw new IllegalArgumentException("Template " + newTemplate.getName() + " name conflicts"); } uiTemplates.add(newTemplate); }); } } return uiTemplates; } UITemplateInitializer(InputStream inputStream);  UITemplateInitializer(Reader io); List<UITemplate> read();  }
@Test public void testReadFile() { UITemplateInitializer initializer = new UITemplateInitializer(this.getClass() .getClassLoader() .getResourceAsStream( "ui-initialized-templates.yml")); final List<UITemplate> uiTemplates = initializer.read(); Assert.assertEquals(2, uiTemplates.size()); UITemplate uiTemplate = uiTemplates.get(0); Assert.assertEquals("APM (Agent based)", uiTemplate.getName()); Assert.assertTrue(uiTemplate.getConfiguration().length() > 0); Assert.assertEquals(BooleanUtils.TRUE, uiTemplate.getActivated()); Assert.assertEquals(BooleanUtils.FALSE, uiTemplate.getDisabled()); uiTemplate = uiTemplates.get(1); Assert.assertEquals("APM (Service Mesh)", uiTemplate.getName()); Assert.assertTrue(uiTemplate.getConfiguration().length() > 0); Assert.assertEquals(BooleanUtils.FALSE, uiTemplate.getActivated()); Assert.assertEquals(BooleanUtils.TRUE, uiTemplate.getDisabled()); }
@Test(expected = IllegalArgumentException.class) public void testTemplateConflict() { UITemplateInitializer initializer = new UITemplateInitializer(this.getClass() .getClassLoader() .getResourceAsStream( "ui-initialized-templates-conflict.yml")); final List<UITemplate> uiTemplates = initializer.read(); }