SimpleRuntimeEnvironment implements RuntimeEnvironment, SchedulerProvider { public void addAsset(Resource resource, ResourceType type) { boolean replaced = false; if (resource.getSourcePath() != null ) { String path = resource.getSourcePath(); String typeStr = null; if( path.toLowerCase().endsWith(".csv") ) { typeStr = DecisionTableInputType.CSV.toString(); } else if( path.toLowerCase().endsWith(".xls") ) { typeStr = DecisionTableInputType.XLS.toString(); } if( typeStr != null ) { String worksheetName = null; boolean replaceConfig = true; ResourceConfiguration config = resource.getConfiguration(); if( config != null && config instanceof DecisionTableConfiguration ) { DecisionTableInputType realType = DecisionTableInputType.valueOf(typeStr); if( ((DecisionTableConfiguration) config).getInputType().equals(realType) ) { replaceConfig = false; } else { worksheetName = ((DecisionTableConfiguration) config).getWorksheetName(); } } if( replaceConfig ) { Properties prop = new Properties(); prop.setProperty(ResourceTypeImpl.KIE_RESOURCE_CONF_CLASS, DecisionTableConfigurationImpl.class.getName()); prop.setProperty(DecisionTableConfigurationImpl.DROOLS_DT_TYPE, typeStr); if( worksheetName != null ) { prop.setProperty(DecisionTableConfigurationImpl.DROOLS_DT_WORKSHEET, worksheetName); } ResourceConfiguration conf = ResourceTypeImpl.fromProperties(prop); this.kbuilder.add(resource, type, conf); replaced = true; } } } if( ! replaced ) { this.kbuilder.add(resource, type); } if (this.kbuilder.hasErrors()) { StringBuffer errorMessage = new StringBuffer(); for( KnowledgeBuilderError error : kbuilder.getErrors()) { errorMessage.append(error.getMessage() + ","); } this.kbuilder.undo(); throw new IllegalArgumentException("Cannot add asset: " + errorMessage.toString()); } } SimpleRuntimeEnvironment();  SimpleRuntimeEnvironment(RegisterableItemsFactory registerableItemsFactory); void init(); void addAsset(Resource resource, ResourceType type); void addToEnvironment(String name, Object value); void addToConfiguration(String name, String value); @Override KieBase getKieBase(); Environment getEnvironmentTemplate(); @Override Environment getEnvironment(); @Override KieSessionConfiguration getConfiguration(); @Override boolean usePersistence(); @Override RegisterableItemsFactory getRegisterableItemsFactory(); @Override void close(); @Override Mapper getMapper(); @Override UserGroupCallback getUserGroupCallback(); void setUserGroupCallback(UserGroupCallback userGroupCallback); @Override UserInfo getUserInfo(); void setUserInfo(UserInfo userInfo); Properties getSessionConfigProperties(); void setSessionConfigProperties(Properties sessionConfigProperties); void setUsePersistence(boolean usePersistence); void setKieBase(KieBase kbase); void setMapper(Mapper mapper); @Override GlobalSchedulerService getSchedulerService(); void setSchedulerService(GlobalSchedulerService schedulerService); void setRegisterableItemsFactory(RegisterableItemsFactory registerableItemsFactory); EntityManagerFactory getEmf(); void setEmf(EntityManagerFactory emf); ClassLoader getClassLoader(); void setClassLoader(ClassLoader classLoader);  }
@Test public void addAssetCsvXlsTest() { doNothing().when(this.kbuilder).add(any(Resource.class), any(ResourceType.class), any(ResourceConfiguration.class)); doThrow(new IllegalStateException("CSV resource not handled correctly!")).when(this.kbuilder).add(any(Resource.class), any(ResourceType.class)); Resource resource = ResourceFactory.newClassPathResource("/data/resource.csv", getClass()); environment.addAsset(resource, ResourceType.DTABLE); doThrow(new IllegalStateException("XLS resource not handled correctly!")).when(this.kbuilder).add(any(Resource.class), any(ResourceType.class)); resource = ResourceFactory.newClassPathResource("/data/resource.xls", getClass()); environment.addAsset(resource, ResourceType.DTABLE); doThrow(new IllegalStateException("BPMN2 resource not handled correctly!")).when(this.kbuilder).add(any(Resource.class), any(ResourceType.class), any(ResourceConfiguration.class)); doNothing().when(this.kbuilder).add(any(Resource.class), any(ResourceType.class)); resource = ResourceFactory.newClassPathResource("/data/resource.bpmn2", getClass()); environment.addAsset(resource, ResourceType.BPMN2); }
@Test public void addAssetCsvXlsReplaceConfigTest() { ArgumentCaptor<ResourceConfiguration> resourceConfigCaptor = ArgumentCaptor.forClass(ResourceConfiguration.class); doThrow(new IllegalStateException("XLS resource not handled correctly!")).when(this.kbuilder).add(any(Resource.class), any(ResourceType.class)); Resource resource = ResourceFactory.newClassPathResource("/data/resource.xls", getClass()); DecisionTableConfigurationImpl config = new DecisionTableConfigurationImpl(); config.setInputType(DecisionTableInputType.CSV); String worksheetName = "test-worksheet-name"; config.setWorksheetName(worksheetName); resource.setConfiguration(config); environment.addAsset(resource, ResourceType.DTABLE); verify(this.kbuilder).add(any(Resource.class), any(ResourceType.class), resourceConfigCaptor.capture()); ResourceConfiguration replacedConfig = resourceConfigCaptor.getValue(); assertTrue("Not a DecisionTableConfiguration, but a " + replacedConfig.getClass().getSimpleName(), replacedConfig instanceof DecisionTableConfiguration); assertEquals("Incorrect file type", DecisionTableInputType.XLS, ((DecisionTableConfiguration) replacedConfig).getInputType()); assertEquals("Worksheet name not preserved", worksheetName, ((DecisionTableConfiguration) replacedConfig).getWorksheetName()); }
@Test public void addAssetXLSDtableWithOwnConfigTest() { Resource resource = ResourceFactory.newClassPathResource("/data/resource.xls", getClass()); DecisionTableConfigurationImpl config = new DecisionTableConfigurationImpl(); config.setInputType(DecisionTableInputType.XLS); String worksheetName = "test-worksheet-name"; config.setWorksheetName(worksheetName); resource.setConfiguration(config); environment.addAsset(resource, ResourceType.DTABLE); verify(this.kbuilder).add(any(Resource.class), any(ResourceType.class)); }