Skip to content

Commit

Permalink
Add class agnostic fetch node on appfilesystem (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Bui-Quang <[email protected]>

Co-authored-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
pl-buiquang and geofjamg committed Jan 5, 2020
1 parent 750a7b0 commit db43442
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
39 changes: 39 additions & 0 deletions afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,45 @@ public <T extends ProjectFile> T findProjectFile(String projectFileId, Class<T>
return (T) projectFile;
}

/**
* Retrieve a project node with undefined class
* @param nodeId the node Id
* @return a typed node
*/
public AbstractNodeBase fetchNode(String nodeId) {
Objects.requireNonNull(nodeId);

NodeInfo projectFileInfo = storage.getNodeInfo(nodeId);
NodeInfo parentInfo = storage.getParentNode(projectFileInfo.getId()).orElse(null);
while (parentInfo != null && !Project.PSEUDO_CLASS.equals(parentInfo.getPseudoClass())) {
parentInfo = storage.getParentNode(parentInfo.getId()).orElse(null);
}

NodeInfo projectInfo = parentInfo;
while (projectInfo != null && !Project.PSEUDO_CLASS.equals(projectInfo.getPseudoClass())) {
projectInfo = storage.getParentNode(projectInfo.getId()).orElse(null);
}
Project project = projectInfo != null && Project.PSEUDO_CLASS.equals(projectInfo.getPseudoClass()) ?
new Project(new FileCreationContext(projectInfo, storage, this)) : null;

if (parentInfo == null || project == null) {
return createNode(projectFileInfo);
}

ProjectFileCreationContext context = new ProjectFileCreationContext(projectFileInfo, storage, project);

if (ProjectFolder.PSEUDO_CLASS.equals(projectFileInfo.getPseudoClass())) {
return new ProjectFolder(context);
}

ProjectFileExtension extension = data.getProjectFileExtensionByPseudoClass(projectFileInfo.getPseudoClass());
if (extension != null) {
return extension.createProjectFile(context);
}
return createNode(projectFileInfo);
}


/**
* Get a project by its ID
*
Expand Down
38 changes: 38 additions & 0 deletions afs-core/src/test/java/com/powsybl/afs/AfsBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -297,4 +298,41 @@ public void findProjectTest() {
assertEquals(project.getFileSystem(), foundProject.getFileSystem());
assertEquals(project.getCodeVersion(), foundProject.getCodeVersion());
}

@Test
public void fetchNodeTest() {
Folder folder = afs.getRootFolder().createFolder("testFolder");
Project project = folder.createProject("test");
FooFile createdFile = project.getRootFolder().fileBuilder(FooFileBuilder.class)
.withName("foo")
.build();
ProjectFolder projectFolder = project.getRootFolder().createFolder("testFolder");
FooFile nestedFile = projectFolder.fileBuilder(FooFileBuilder.class)
.withName("bar")
.build();

BiConsumer<AbstractNodeBase, AbstractNodeBase> checkResult = (source, result) -> {
assertNotNull(result);
assertEquals(source.getClass(), result.getClass());
assertEquals(source.getId(), result.getId());
assertEquals(source.getName(), result.getName());
assertEquals(source.getName(), result.getName());
assertEquals(source.getDescription(), result.getDescription());
assertEquals(source.getCreationDate(), result.getCreationDate());
assertEquals(source.getModificationDate(), result.getModificationDate());
assertEquals(source.getCodeVersion(), result.getCodeVersion());

if (source instanceof ProjectNode) {
assertEquals(((ProjectNode) source).getProject().getId(), ((ProjectNode) result).getProject().getId());
assertEquals(((ProjectNode) source).getFileSystem(), ((ProjectNode) result).getFileSystem());
}
};

checkResult.accept(project, afs.fetchNode(project.getId()));
checkResult.accept(folder, afs.fetchNode(folder.getId()));
checkResult.accept(createdFile, afs.fetchNode(createdFile.getId()));
checkResult.accept(nestedFile, afs.fetchNode(nestedFile.getId()));
checkResult.accept(projectFolder, afs.fetchNode(projectFolder.getId()));
}

}

0 comments on commit db43442

Please sign in to comment.