/** * * @return An iterator over the nodes history. */ public AssetHistoryIterator getHistory() { if(isHistoricalVersion()) { Node frozenNode = getNode(); try { Node headNode = frozenNode.getSession().getNodeByIdentifier( frozenNode.getProperty("jcr:frozenUuid") .getString()); return new AssetHistoryIterator( this.rulesRepository, headNode ); } catch (RepositoryException e) { throw new RulesRepositoryException(e); } } return new AssetHistoryIterator( this.rulesRepository, this.node ); }
/** * This will rename an asset and apply the change immediately. * * @return the UUID of the new asset */ public String renameAsset(String uuid, String newAssetName) { try { AssetItem itemOriginal = loadAssetByUUID(uuid); log.info("Renaming asset: " + itemOriginal.getNode().getPath() + " to " + newAssetName); Node node = itemOriginal.getNode(); String sourcePath = node.getPath(); String destPath = node.getParent().getPath() + "/" + newAssetName; this.session.move(sourcePath, destPath); this.session.save(); itemOriginal.updateTitle(newAssetName); itemOriginal.checkin("Renamed asset " + itemOriginal.getName()); return itemOriginal.getUUID(); } catch (RepositoryException e) { log.error(e.getMessage(), e); throw new RulesRepositoryException(e); } }
/** * * @return An iterator over the nodes history. */ public AssetHistoryIterator getHistory() { if(isHistoricalVersion()) { Node frozenNode = getNode(); try { Node headNode = frozenNode.getSession().getNodeByIdentifier( frozenNode.getProperty("jcr:frozenUuid") .getString()); return new AssetHistoryIterator( this.rulesRepository, headNode ); } catch (RepositoryException e) { throw new RulesRepositoryException(e); } } return new AssetHistoryIterator( this.rulesRepository, this.node ); }
/** * This will rename an asset and apply the change immediately. * * @return the UUID of the new asset */ public String renameAsset(String uuid, String newAssetName) { try { AssetItem itemOriginal = loadAssetByUUID(uuid); log.info("Renaming asset: " + itemOriginal.getNode().getPath() + " to " + newAssetName); Node node = itemOriginal.getNode(); String sourcePath = node.getPath(); String destPath = node.getParent().getPath() + "/" + newAssetName; this.session.move(sourcePath, destPath); this.session.save(); itemOriginal.updateTitle(newAssetName); itemOriginal.checkin("Renamed asset " + itemOriginal.getName()); return itemOriginal.getUUID(); } catch (RepositoryException e) { log.error(e.getMessage(), e); throw new RulesRepositoryException(e); } }
try { AssetItem source = loadAssetByUUID(uuidSource); String sourcePath = source.getNode().getPath(); String safeDestinationName = NodeUtils.makeJSR170ComplaintName(destinationName); String destPath = this.getAreaNode(MODULE_AREA).getPath() + "/" + destinationModule + "/" + ModuleItem.ASSET_FOLDER_NAME + "/" + safeDestinationName;
@Test public void testArchiveBooleanFlag() throws Exception { RulesRepository repo = RepositorySessionUtil.getRepository(); AssetItem item = repo.loadDefaultModule().addAsset( "testArchiveBooleanFlag", "yeah" ); assertFalse( item.isArchived() ); item.archiveItem( true ); assertFalse( item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME ) ); assertTrue( item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_ARCHIVE_FLAG ) ); assertTrue( item.isArchived() ); item.checkin( "lalalala" ); }
@Test public void testGetFormat() throws Exception { AssetItem ruleItem1 = getRepo().loadDefaultModule().addAsset("testGetFormat", "test content"); ruleItem1.updateContent( "la" ); assertEquals(AssetItem.DEFAULT_CONTENT_FORMAT, ruleItem1.getFormat()); assertTrue(ruleItem1.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME )); assertFalse(ruleItem1.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME )); ruleItem1.updateFormat( "blah" ); assertEquals("blah", ruleItem1.getFormat()); }
@Test public void testAddRuleCalendarWithDates() { RulesRepository rulesRepository = getRepo(); Calendar effectiveDate = Calendar.getInstance(); Calendar expiredDate = Calendar.getInstance(); expiredDate.setTimeInMillis( effectiveDate.getTimeInMillis() + (1000 * 60 * 60 * 24) ); AssetItem ruleItem1 = rulesRepository.loadDefaultModule().addAsset( "testAddRuleCalendarCalendar", "desc" ); ruleItem1.updateDateEffective( effectiveDate ); ruleItem1.updateDateExpired( expiredDate ); assertNotNull( ruleItem1 ); assertNotNull( ruleItem1.getNode() ); //Comparing two Calendar instances entirely doesn't work as JackRabbit persists Calendar objects as Strings using //http://svn.apache.org/repos/asf/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java //Not all Calendar properties are taken into consideration so we only check the getTime() values. assertEquals( effectiveDate.getTimeInMillis(), ruleItem1.getDateEffective().getTimeInMillis() ); //Comparing two Calendar instances entirely doesn't work as JackRabbit persists Calendar objects as Strings using //http://svn.apache.org/repos/asf/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java //Not all Calendar properties are taken into consideration so we only check the getTime() values. assertEquals( expiredDate.getTimeInMillis(), ruleItem1.getDateExpired().getTimeInMillis() ); ruleItem1.checkin( "ho " ); }
@Test public void testGetFormatForShareableAsset() throws Exception { AssetItem asset = getRepo().loadGlobalArea().addAsset("testGetFormatForShareableAsset", "desc"); asset.checkin("version0"); AssetItem linkedAsset = getDefaultPackage().addAssetImportedFromGlobalArea(asset.getName()); linkedAsset.updateContent("la"); assertEquals(AssetItem.DEFAULT_CONTENT_FORMAT, linkedAsset.getFormat()); assertTrue(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_NAME)); assertFalse(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_BINARY_NAME)); linkedAsset.updateFormat("blah"); assertEquals("blah", linkedAsset.getFormat()); }
@Test public void testImportExportWithShareableNodes() throws Exception { RulesRepository repo = getRepo(); AssetItem item = repo.loadDefaultModule().addAsset("testImportExportShareableNodeOriginal", "desc"); item.updateContent("la"); ModuleItem.ensureMixinType(item, "mix:shareable"); ModuleItem source = repo.createModule("testImportExportShareableNodesPackage", "desc"); repo.save(); source.checkout(); Session session = item.getNode().getSession(); Workspace workspace = session.getWorkspace(); String path = "/drools:repository/drools:package_area/testImportExportShareableNodesPackage/assets/testImportExportShareableNodeShared"; workspace.clone(workspace.getName(), item.getNode().getPath(), path, false); repo.save(); byte[] repository_backup; ByteArrayOutputStream bout = new ByteArrayOutputStream(); repo.exportRepositoryToStream(bout); repository_backup = bout.toByteArray(); assertNotNull(repository_backup); repo.importRulesRepositoryFromStream(new ByteArrayInputStream( repository_backup)); assertTrue(repo.containsModule("testImportExportShareableNodesPackage")); assertTrue(repo.loadModule("testImportExportShareableNodesPackage").containsAsset("testImportExportShareableNodeOriginal")); }
@Test public void testShareableNodes() throws Exception { RulesRepository repo = getRepo(); AssetItem item = repo.loadDefaultModule().addAsset("testShareableNodeOriginal", "desc"); item.updateContent("la"); item.getNode().addMixin("mix:shareable"); ModuleItem source = repo.createModule("testShareableNodesPackage", "desc"); repo.save(); source.checkout(); Session session = repo.getSession(); Workspace workspace = session.getWorkspace(); String path = "/drools:repository/drools:package_area/testShareableNodesPackage/assets/testShareableNodeShared"; workspace.clone(workspace.getName(), item.getNode().getPath(), path, false); repo.save(); AssetItem originalItem = repo.loadDefaultModule().loadAsset("testShareableNodeOriginal"); AssetItem sharedItem = repo.loadModule("testShareableNodesPackage").loadAsset("testShareableNodeShared"); assertTrue( originalItem.getContent().equals("la")); assertTrue( sharedItem.getContent().equals("la")); originalItem.remove(); }
@Test public void testShareableNodesWithQuery() throws Exception { RulesRepository repo = getRepo(); AssetItem item = repo.loadGlobalArea().addAsset("testShareableNodesWithQueryOriginal", "desc"); item.updateFormat("xyz"); item.getNode().addMixin("mix:shareable"); ModuleItem source = repo.createModule("testShareableNodesWithQueryPackage", "desc"); repo.save(); AssetItemIterator it = repo.loadGlobalArea().queryAssets( "drools:format='xyz'" ); List list = iteratorToList( it ); assertEquals(1, list.size()); assertTrue(list.get( 0 ) instanceof AssetItem); source.checkout(); Session session = repo.getSession(); Workspace workspace = session.getWorkspace(); String path = "/drools:repository/drools:package_area/testShareableNodesWithQueryPackage/assets/testShareableNodesWithQueryShared"; workspace.clone(workspace.getName(), item.getNode().getPath(), path, false); repo.save(); AssetItem originalItem = repo.loadGlobalArea().loadAsset("testShareableNodesWithQueryOriginal"); AssetItem sharedItem = repo.loadModule("testShareableNodesWithQueryPackage").loadAsset("testShareableNodesWithQueryShared"); assertTrue( originalItem.getFormat().equals("xyz")); assertTrue( sharedItem.getFormat().equals("xyz")); it = repo.loadGlobalArea().queryAssets( "drools:format='xyz'" ); list = iteratorToList( it ); assertEquals(1, list.size()); assertTrue(list.get( 0 ) instanceof AssetItem); }
@Test public void testPackageInstanceWrongNodeType() throws Exception { ModuleItem pack = getRepo().loadDefaultModule(); AssetItem rule = pack.addAsset( "packageInstanceWrongNodeType", "" ); try { new ModuleItem(this.getRepo(), rule.getNode()); fail("Can't create a package from a rule node."); } catch (RulesRepositoryException e) { assertNotNull(e.getMessage()); } }
@Test public void testBinaryAsset() throws Exception { AssetItem item = getRepo().loadDefaultModule().addAsset( "testBinaryAsset", "yeah" ); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); item.updateBinaryContentAttachment( in ); item.updateBinaryContentAttachmentFileName( "x.x" ); in.close(); assertEquals(data, item.getContent()); assertFalse(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME )); assertTrue(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME )); item.checkin( "lalalala" ); assertTrue(item.isBinary()); item = getRepo().loadDefaultModule().loadAsset( "testBinaryAsset" ); InputStream in2 = item.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = item.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", item.getBinaryContentAttachmentFileName()); assertTrue(item.isBinary()); item.updateContent("qed"); item.checkin(""); item = getRepo().loadAssetByUUID(item.getUUID()); assertEquals("qed", item.getContent()); }
@Test public void testBinaryAssetForShareableAsset() throws Exception { AssetItem asset = getRepo().loadGlobalArea().addAsset("testBinaryAssetForShareableAsset", "desc"); asset.checkin("version0"); AssetItem linkedAsset = getDefaultPackage().addAssetImportedFromGlobalArea(asset.getName()); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); linkedAsset.updateBinaryContentAttachment(in); linkedAsset.updateBinaryContentAttachmentFileName("x.x"); in.close(); assertEquals(data, linkedAsset.getContent()); assertFalse(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_NAME)); assertTrue(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_BINARY_NAME)); linkedAsset.checkin("lalalala"); assertTrue(linkedAsset.isBinary()); asset = getRepo().loadGlobalArea().loadAsset("testBinaryAssetForShareableAsset"); InputStream in2 = asset.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = asset.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", asset.getBinaryContentAttachmentFileName()); assertTrue(asset.isBinary()); linkedAsset.updateContent("qed"); linkedAsset.checkin(""); linkedAsset = getRepo().loadAssetByUUID(linkedAsset.getUUID()); assertEquals("qed", linkedAsset.getContent()); }
@Test public void testGetContent() { AssetItem ruleItem1 = getRepo().loadDefaultModule().addAsset("testGetContent", "test content"); ruleItem1.updateContent( "test content" ); ruleItem1.updateFormat("drl"); assertNotNull(ruleItem1); assertNotNull(ruleItem1.getNode()); assertEquals("test content", ruleItem1.getContent()); assertFalse(ruleItem1.isBinary()); assertNotNull(ruleItem1.getBinaryContentAsBytes()); assertNotNull(ruleItem1.getBinaryContentAttachment()); String content = new String(ruleItem1.getBinaryContentAsBytes()); assertNotNull(content); }
@Test public void testAssetItemCreation() throws Exception { Calendar now = Calendar.getInstance(); Thread.sleep(500); //MN: need this sleep to get the correct date AssetItem ruleItem1 = getDefaultPackage().addAsset("testRuleItem", "test content"); assertNotNull(ruleItem1); assertNotNull(ruleItem1.getNode()); assertEquals("testRuleItem", ruleItem1.getName()); assertNotNull(ruleItem1.getCreatedDate()); assertTrue(now.before( ruleItem1.getCreatedDate() )); String packName = getDefaultPackage().getName(); assertEquals(packName, ruleItem1.getModuleName()); assertNotNull(ruleItem1.getUUID()); //try constructing with node of wrong type try { ModuleItem pitem = getRepo().loadDefaultModule(); new AssetItem(getRepo(), pitem.getNode()); fail("Exception not thrown for node of wrong type"); } catch(RulesRepositoryException e) { assertNotNull(e.getMessage()); } catch(Exception e) { fail("Caught unexpected exception: " + e); } }
@Test public void testGetContentForShareableAsset() { AssetItem asset = getRepo().loadGlobalArea().addAsset("testGetContentForShareableAsset", "test content"); AssetItem linkedAsset = getRepo().loadDefaultModule().addAssetImportedFromGlobalArea(asset.getName()); linkedAsset.updateContent("test content"); linkedAsset.updateFormat("drl"); assertNotNull(linkedAsset.getNode()); assertEquals("test content", linkedAsset.getContent()); assertEquals("test content", asset.getContent()); assertFalse(linkedAsset.isBinary()); assertFalse(asset.isBinary()); assertNotNull(linkedAsset.getBinaryContentAsBytes()); assertNotNull(linkedAsset.getBinaryContentAttachment()); String content = new String(linkedAsset.getBinaryContentAsBytes()); assertNotNull(content); content = new String(asset.getBinaryContentAsBytes()); assertNotNull(content); }