YamlFileParser { public static <T> T parse(FileReference yamlFile, Class<T> type) throws ConfigurationException { T loadedBean; try { loadedBean = new Yaml(new CustomClassLoaderConstructor()).loadAs(yamlFile.getContent(), type); } catch (FileOperationException e) { throw new ConfigurationException( "Cannot read the configuration file '" + yamlFile.getAbsolutePath() + "'.", e); } catch (Exception e) { throw new ConfigurationException( "Cannot parse the configuration file '" + yamlFile.getAbsolutePath() + "'.", e); } if (loadedBean == null) { throw new ConfigurationException( "Cannot load the configuration file '" + yamlFile.getAbsolutePath() + "' as it is empty."); } return loadedBean; }  static T parse(FileReference yamlFile, Class<T> type);  }
@Test public void testCannotReadFile() { FileReference yamlFile = mock(FileReference.class); when(yamlFile.getContent()).thenThrow(FileOperationException.class); Assert.assertThrows(ConfigurationException.class, () -> YamlFileParser.parse(yamlFile, Object.class)); }
@Test public void testInvalidYaml() { FileReference yamlFile = mock(FileReference.class); when(yamlFile.getContent()).thenReturn("# testing\n" + "artifactId: org.wso2.carbon.uuf.sample.pets-store.feature\n" + " - version: 1.0.0-SNAPSHOT\n" + "- contextPath: pets-store\n" + "dependencies:"); Assert.assertThrows(ConfigurationException.class, () -> YamlFileParser.parse(yamlFile, DependencyNode.class)); }
@Test public void testEmptyYaml() { FileReference yamlFile = mock(FileReference.class); when(yamlFile.getContent()).thenReturn("# Only a comment.\n"); Assert.assertThrows(ConfigurationException.class, () -> YamlFileParser.parse(yamlFile, ComponentConfig.class)); }
@Test public void test() { FileReference yamlFile = mock(FileReference.class); when(yamlFile.getContent()).thenReturn("# Auto-generated by UUF Maven Plugin. Do NOT modify manually.\n" + "contextPath: null\n" + "errorPages:\n" + " '404': /foundation/error/404\n" + " default: /foundation/error/default\n" + "loginPageUri: /simple-auth/login\n" + "menus:\n" + "- name: main\n" + " items:\n" + " - icon: fw-home\n" + " link: '#'\n" + " submenus: []\n" + " text: Home\n" + "other:\n" + " appName: Pet Store\n" + "security:\n" + "theme: org.wso2.carbon.uuf.sample.default.theme\n" + "sessionManagement:\n" + " factoryClassName: org.wso2.carbon.uuf.auth.SMFactory\n" + " timeout: 600\n" + "authorizer: org.wso2.carbon.uuf.auth.CaasAuthorizer"); AppConfig appConfig = YamlFileParser.parse(yamlFile, AppConfig.class); Assert.assertEquals(appConfig.getContextPath(), null); Assert.assertEquals(appConfig.getErrorPages().get("404"), "/foundation/error/404"); Assert.assertEquals(appConfig.getLoginPageUri(), "/simple-auth/login"); Assert.assertEquals(appConfig.getMenus().size(), 1); Assert.assertEquals(appConfig.getMenus().get(0).getName(), "main"); Assert.assertEquals(appConfig.getMenus().get(0).getItems().size(), 1); Assert.assertEquals(appConfig.getMenus().get(0).getItems().get(0).getText(), "Home"); Assert.assertEquals(appConfig.getOther().size(), 1); Assert.assertEquals(appConfig.getOther().get("appName"), "Pet Store"); Assert.assertEquals(appConfig.getTheme(), "org.wso2.carbon.uuf.sample.default.theme"); Assert.assertNotEquals(appConfig.getSessionManagement(), null); Assert.assertEquals(appConfig.getSessionManagement().getFactoryClassName(), "org.wso2.carbon.uuf.auth.SMFactory"); Assert.assertEquals(appConfig.getSessionManagement().getTimeout(), 600); Assert.assertEquals(appConfig.getAuthorizer(), "org.wso2.carbon.uuf.auth.CaasAuthorizer"); }
@Test public void testNullSessionManagement() { FileReference yamlFile = mock(FileReference.class); when(yamlFile.getContent()).thenReturn("# Auto-generated by UUF Maven Plugin. Do NOT modify manually.\n" + "contextPath: null\n" + "errorPages:\n" + " '404': /foundation/error/404\n" + " default: /foundation/error/default\n" + "loginPageUri: /simple-auth/login\n" + "menus:\n" + "- name: main\n" + " items:\n" + " - icon: fw-home\n" + " link: '#'\n" + " submenus: []\n" + " text: Home\n" + "other:\n" + " appName: Pet Store\n" + "security:\n" + "theme: org.wso2.carbon.uuf.sample.default.theme\n"); AppConfig appConfig = YamlFileParser.parse(yamlFile, AppConfig.class); Assert.assertNotEquals(appConfig.getSessionManagement(), null); Assert.assertEquals(appConfig.getSessionManagement().getFactoryClassName(), null); Assert.assertEquals(appConfig.getSessionManagement().getTimeout(), 0); }