Skip to content

Commit

Permalink
feature delete group folder when folder is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
alberts-tid committed May 6, 2014
1 parent 20aa56b commit b26d0af
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface CatalogManager {
public String generateSiteStr();

public void removeNodesByGroupName(String groupName);

public boolean isLastGroupNode(String groupName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public void removeNodesByGroupName(String groupName) {
mongoTemplate.remove(searchNodeQuery, Node.class);

}

public boolean isLastGroupNode(String groupName) {
Query searchNodeQuery = new Query(Criteria.where("groupName").is(groupName));
List<Node> nodeList = mongoTemplate.find(searchNodeQuery, Node.class);
if(nodeList!= null && nodeList.size()==1){
return true;
}else{
return false;
}
}

// public void setMongoTemplate(MongoTemplate mongoTemplate) {
// this.mongoTemplate = mongoTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public void deleteNodeFiles(String nodeName) throws IOException {

if (!file.delete()) {
logger.info(format("File {0} could not be deleted. Did it exist?", path + "/" + node.getId() + ".pp"));
}else{
logger.info(format("File {0} deleted.", path + "/" + node.getId() + ".pp"));
}

if(catalogManager.isLastGroupNode(node.getGroupName())){
deleteGoupFolder(node.getGroupName());
}

} catch (NoSuchElementException e) {
Expand All @@ -147,6 +153,8 @@ public void deleteGoupFolder(String groupName) throws IOException {
File path = new File(defaultManifestsPath + groupName);

FileUtils.deleteDirectory(path);

logger.info(format("Folder {0} deleted.", path + "/" + groupName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.telefonica.euro_iaas.sdc.pupperwrapper.services.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyObject;
Expand Down Expand Up @@ -103,6 +104,16 @@ public void setUp() {
}
});

Query queryFindOK = new Query(Criteria.where("groupName").is("groupNameOK"));
when(mongoTemplateMock.find(queryFindOK, Node.class)).thenReturn(new ArrayList<Node>() {
{
add(node);
}
});

Query queryFindErr = new Query(Criteria.where("groupName").is("groupNameErr"));
when(mongoTemplateMock.find(queryFindErr, Node.class)).thenReturn(null);

}

@Test(expected = NoSuchElementException.class)
Expand Down Expand Up @@ -194,4 +205,18 @@ public void removeNodesByGroupNameTest() {
assertTrue(catalogManagerMongo.getNodeLength() == 3);

}

@Test
public void isLastGroupNodeTestOK(){

assertTrue(catalogManagerMongo.isLastGroupNode("groupNameOK"));

}

@Test
public void isLastGroupNodeTestFail(){

assertFalse(catalogManagerMongo.isLastGroupNode("groupNameErr"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public void setUp(){
when(catalogManager.generateSiteStr()).thenReturn("site.pp content");
when(catalogManager.generateManifestStr("test")).thenReturn("manifest test1 content");
when(catalogManager.generateManifestStr("test2")).thenReturn("manifest test2 content");

when(catalogManager.isLastGroupNode("group")).thenReturn(false).thenReturn(true);
}

@Test
Expand Down

0 comments on commit b26d0af

Please sign in to comment.