Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code and fix sonar issues #174

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3d47d3e
JwtTokenNeededFilter : skipTokenValidityCheck becomes final
rolnico Feb 14, 2025
c5646a2
Clean Utils
rolnico Feb 14, 2025
3aff3ea
Create SafeLogger class and use it
rolnico Feb 14, 2025
60ae956
add comment
rolnico Feb 14, 2025
1e520f0
clean EventsTest
rolnico Feb 14, 2025
3ac12fc
use regex: !([\w().]+)\.isPresent\(\)
rolnico Feb 14, 2025
405bf9f
clean NodeInfoTest
rolnico Feb 14, 2025
c0e845a
remove method same as parent
rolnico Feb 14, 2025
c38bef2
clean ActionScriptTest
rolnico Feb 14, 2025
c2075cf
replace ImmutableList.of with List.of
rolnico Feb 14, 2025
01a2249
clean import and some checkstyle
rolnico Feb 14, 2025
a7a0dda
clean part of AppData
rolnico Feb 14, 2025
c93e1ab
clean part of AppFileSystem
rolnico Feb 14, 2025
076cb46
clean part of AppFileSystemTool
rolnico Feb 14, 2025
2a79c7f
clean LocalTaskMonitor
rolnico Feb 14, 2025
39fd6aa
clean AbstractProjectFileTest
rolnico Feb 14, 2025
adc251b
start to clean AfsBaseTest
rolnico Feb 14, 2025
8f3d077
clean AfsBaseTest
rolnico Feb 17, 2025
324054a
clean LocalTaskMonitorTest
rolnico Feb 17, 2025
d6a4528
add deprecation date on AbstractModificationScript
rolnico Feb 17, 2025
83a575c
clean AbstractScript
rolnico Feb 17, 2025
0c8440a
clean ImportedCaseTest
rolnico Feb 17, 2025
ddc7f6c
clean ModificationScriptTest + create GenericScriptTest
rolnico Feb 17, 2025
5dce23d
clean GenericScriptTest
rolnico Feb 17, 2025
833357a
clean TestImporter
rolnico Feb 17, 2025
f32459f
clean VirtualCaseTest
rolnico Feb 18, 2025
aa1b57d
clean LocalAppFileSystemConfigTest
rolnico Feb 18, 2025
61bd6dd
clean LocalAppFileSystemProviderTest
rolnico Feb 18, 2025
ea9ffd8
clean LocalAppStorageTest
rolnico Feb 18, 2025
4a1398b
clean some classes
rolnico Feb 20, 2025
a3f072a
clean NodeGenericMetadataTest
rolnico Feb 21, 2025
c7552b7
clean NodeInfoTest
rolnico Feb 21, 2025
47bb97e
clean UtilsTest
rolnico Feb 21, 2025
42cd73e
clean StorageChangeTest
rolnico Feb 21, 2025
d09994b
clean NodeEventTest
rolnico Feb 21, 2025
ed8ce3d
add RemoteAppFileSystemProviderTest
rolnico Feb 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.afs.action.dsl;

import com.google.common.collect.ImmutableList;
import com.powsybl.afs.AbstractProjectFileTest;
import com.powsybl.afs.Project;
import com.powsybl.afs.ProjectFileExtension;
Expand Down Expand Up @@ -39,7 +38,7 @@ protected AppStorage createStorage() {

@Override
protected List<ProjectFileExtension> getProjectFileExtensions() {
return ImmutableList.of(new ActionScriptExtension());
return List.of(new ActionScriptExtension());
}

@Test
Expand All @@ -49,13 +48,13 @@ void test() {

// create contingency list
ActionScript actionScript = project.getRootFolder().fileBuilder(ActionScriptBuilder.class)
.withName("contingencies")
.withContent(String.join(System.lineSeparator(),
"contingency('c1') {",
" equipments 'l1'",
"}",
""))
.build();
.withName("contingencies")
.withContent(String.join(System.lineSeparator(),
"contingency('c1') {",
" equipments 'l1'",
"}",
""))
.build();
List<Contingency> contingencies = Collections.singletonList(new Contingency("c1", new LineContingency("l1")));

Network network = Mockito.mock(Network.class);
Expand All @@ -75,15 +74,15 @@ void testActionScripCreationWithCustomPseudoClass() {

// Build an ActionScript using the custom pseudo-class
project.getRootFolder().fileBuilder(ActionScriptBuilder.class)
.withName("customScript")
.withContent("script content")
.withPseudoClass(customPseudoClass)
.build();
.withName("customScript")
.withContent("script content")
.withPseudoClass(customPseudoClass)
.build();

// Retrieve the node info for the created script from storage
NodeInfo nodeInfo = storage
.getChildNode(project.getRootFolder().getId(), "customScript")
.orElseThrow(() -> new AssertionError("Node 'customScript' not found"));
.getChildNode(project.getRootFolder().getId(), "customScript")
.orElseThrow(() -> new AssertionError("Node 'customScript' not found"));

// Assert that the pseudo-class of the node is set to the custom value
assertEquals(customPseudoClass, nodeInfo.getPseudoClass());
Expand All @@ -96,14 +95,14 @@ void testActionScripCreationWithDefaultPseudoClass() {

// Build an ActionScript without specifying a pseudo-class
project.getRootFolder().fileBuilder(ActionScriptBuilder.class)
.withName("defaultScript")
.withContent("script content")
.build();
.withName("defaultScript")
.withContent("script content")
.build();

// Retrieve the node info for the created script from storage
NodeInfo nodeInfo = storage
.getChildNode(project.getRootFolder().getId(), "defaultScript")
.orElseThrow(() -> new AssertionError("Node 'defaultScript' not found"));
.getChildNode(project.getRootFolder().getId(), "defaultScript")
.orElseThrow(() -> new AssertionError("Node 'defaultScript' not found"));

// Assert that the pseudo-class of the node is set to the default value defined in ActionScript.PSEUDO_CLASS
assertEquals(ActionScript.PSEUDO_CLASS, nodeInfo.getPseudoClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.afs.contingency;

import com.google.common.collect.ImmutableList;
import com.powsybl.afs.AbstractProjectFileTest;
import com.powsybl.afs.Project;
import com.powsybl.afs.ProjectFileExtension;
Expand Down Expand Up @@ -35,7 +34,7 @@ protected AppStorage createStorage() {

@Override
protected List<ProjectFileExtension> getProjectFileExtensions() {
return ImmutableList.of(new ContingencyStoreExtension());
return List.of(new ContingencyStoreExtension());
}

@Test
Expand Down
10 changes: 7 additions & 3 deletions afs-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@

<dependencies>
<!-- Compilation dependencies -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-afs-storage-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-computation-local</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powsybl-afs-storage-api</artifactId>
<version>${project.version}</version>
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder</artifactId>
</dependency>

<!-- Test dependencies -->
Expand Down
98 changes: 45 additions & 53 deletions afs-core/src/main/java/com/powsybl/afs/AppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* An instance of AppData is the root of an application file system.
*
* <p>
* Usually, an application will only have one instance of AppData.
* It is the first entrypoint of AFS, through which you can access to individual {@link AppFileSystem} objects.
*
Expand Down Expand Up @@ -45,28 +45,17 @@
*/
public class AppData implements AutoCloseable {

private ComputationManager shortTimeExecutionComputationManager;

private ComputationManager longTimeExecutionComputationManager;

private Map<String, AppFileSystem> fileSystems;

private final List<AppFileSystemProvider> fileSystemProviders;

private final List<ServiceExtension> serviceExtensions;

private final Map<Class<? extends File>, FileExtension> fileExtensions = new HashMap<>();

private final Map<String, FileExtension> fileExtensionsByPseudoClass = new HashMap<>();

private final Map<Class<?>, ProjectFileExtension> projectFileExtensions = new HashMap<>();

private final Map<String, ProjectFileExtension> projectFileExtensionsByPseudoClass = new HashMap<>();

private final Set<Class<? extends ProjectFile>> projectFileClasses = new HashSet<>();

private final EventsBus eventsBus;

private ComputationManager shortTimeExecutionComputationManager;
private ComputationManager longTimeExecutionComputationManager;
private Map<String, AppFileSystem> fileSystems;
private Map<ServiceExtension.ServiceKey, Object> services;

private SecurityTokenProvider tokenProvider = () -> null;
Expand All @@ -77,31 +66,35 @@ public AppData(ComputationManager shortTimeExecutionComputationManager, Computat

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager, EventsBus eventsBus) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
getDefaultFileSystemProviders(), getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
getDefaultFileSystemProviders(), getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
}

public AppData(ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders) {
public AppData(
ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
fileSystemProviders, getDefaultEventsBus());
fileSystemProviders, getDefaultEventsBus());
}

public AppData(ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders, EventsBus eventsBus) {
public AppData(
ComputationManager shortTimeExecutionComputationManager,
ComputationManager longTimeExecutionComputationManager, List<AppFileSystemProvider> fileSystemProviders, EventsBus eventsBus) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager,
fileSystemProviders, getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
fileSystemProviders, getDefaultFileExtensions(), getDefaultProjectFileExtensions(), getDefaultServiceExtensions(), eventsBus);
}

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions) {
public AppData(
ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions) {
this(shortTimeExecutionComputationManager, longTimeExecutionComputationManager, fileSystemProviders, fileExtensions,
projectFileExtensions, serviceExtensions, getDefaultEventsBus());
projectFileExtensions, serviceExtensions, getDefaultEventsBus());
}

public AppData(ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions, EventsBus eventsBus) {
public AppData(
ComputationManager shortTimeExecutionComputationManager, ComputationManager longTimeExecutionComputationManager,
List<AppFileSystemProvider> fileSystemProviders, List<FileExtension> fileExtensions,
List<ProjectFileExtension> projectFileExtensions, List<ServiceExtension> serviceExtensions, EventsBus eventsBus) {
Objects.requireNonNull(fileSystemProviders);
Objects.requireNonNull(fileExtensions);
Objects.requireNonNull(projectFileExtensions);
Expand All @@ -110,11 +103,11 @@ public AppData(ComputationManager shortTimeExecutionComputationManager, Computat
this.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
this.fileSystemProviders = Objects.requireNonNull(fileSystemProviders);
this.serviceExtensions = Objects.requireNonNull(serviceExtensions);
for (FileExtension extension : fileExtensions) {
for (FileExtension<?> extension : fileExtensions) {
this.fileExtensions.put(extension.getFileClass(), extension);
this.fileExtensionsByPseudoClass.put(extension.getFilePseudoClass(), extension);
}
for (ProjectFileExtension extension : projectFileExtensions) {
for (ProjectFileExtension<?, ?> extension : projectFileExtensions) {
this.projectFileExtensions.put(extension.getProjectFileClass(), extension);
this.projectFileExtensions.put(extension.getProjectFileBuilderClass(), extension);
this.projectFileExtensionsByPseudoClass.put(extension.getProjectFilePseudoClass(), extension);
Expand Down Expand Up @@ -142,6 +135,10 @@ private static List<ServiceExtension> getDefaultServiceExtensions() {
return new ServiceLoaderCache<>(ServiceExtension.class).getServices();
}

private static String[] more(String[] path) {
return path.length > 2 ? Arrays.copyOfRange(path, 2, path.length - 1) : new String[] {};
}

private synchronized void loadFileSystems() {
if (fileSystems == null) {
fileSystems = new HashMap<>();
Expand All @@ -159,7 +156,7 @@ private synchronized void loadServices() {
if (services == null) {
services = new HashMap<>();
ServiceCreationContext context = new ServiceCreationContext(tokenProvider.getToken());
for (ServiceExtension extension : serviceExtensions) {
for (ServiceExtension<?> extension : serviceExtensions) {
services.put(extension.getServiceKey(), extension.createService(context));
}
}
Expand Down Expand Up @@ -214,10 +211,6 @@ public void setTokenProvider(SecurityTokenProvider tokenProvider) {
services = null;
}

private static String[] more(String[] path) {
return path.length > 2 ? Arrays.copyOfRange(path, 2, path.length - 1) : new String[] {};
}

public Optional<Node> getNode(String pathStr) {
Objects.requireNonNull(pathStr);
loadFileSystems();
Expand All @@ -231,7 +224,7 @@ public Optional<Node> getNode(String pathStr) {
return Optional.empty();
}
return path.length == 1 ? Optional.of(fileSystem.getRootFolder())
: fileSystem.getRootFolder().getChild(path[1], more(path));
: fileSystem.getRootFolder().getChild(path[1], more(path));
}

public Set<Class<? extends ProjectFile>> getProjectFileClasses() {
Expand All @@ -240,7 +233,7 @@ public Set<Class<? extends ProjectFile>> getProjectFileClasses() {

FileExtension getFileExtension(Class<? extends File> fileClass) {
Objects.requireNonNull(fileClass);
FileExtension extension = fileExtensions.get(fileClass);
FileExtension<?> extension = fileExtensions.get(fileClass);
if (extension == null) {
throw new AfsException("No extension found for file class '" + fileClass.getName() + "'");
}
Expand All @@ -254,10 +247,10 @@ FileExtension getFileExtensionByPseudoClass(String filePseudoClass) {

ProjectFileExtension getProjectFileExtension(Class<?> projectFileOrProjectFileBuilderClass) {
Objects.requireNonNull(projectFileOrProjectFileBuilderClass);
ProjectFileExtension extension = projectFileExtensions.get(projectFileOrProjectFileBuilderClass);
ProjectFileExtension<?, ?> extension = projectFileExtensions.get(projectFileOrProjectFileBuilderClass);
if (extension == null) {
throw new AfsException("No extension found for project file or project file builder class '"
+ projectFileOrProjectFileBuilderClass.getName() + "'");
+ projectFileOrProjectFileBuilderClass.getName() + "'");
}
return extension;
}
Expand All @@ -271,20 +264,27 @@ public ComputationManager getShortTimeExecutionComputationManager() {
return shortTimeExecutionComputationManager;
}

public void setShortTimeExecutionComputationManager(ComputationManager shortTimeExecutionComputationManager) {
this.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
}

public ComputationManager getLongTimeExecutionComputationManager() {
return longTimeExecutionComputationManager != null ? longTimeExecutionComputationManager : shortTimeExecutionComputationManager;
}

public void setLongTimeExecutionComputationManager(ComputationManager longTimeExecutionComputationManager) {
this.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
}

/**
* Gets the list of remotely accessible file systems. Should not be used by the AFS API users.
*/
public List<String> getRemotelyAccessibleFileSystemNames() {
loadFileSystems();
return fileSystems.entrySet().stream()
.map(Map.Entry::getValue)
.filter(AppFileSystem::isRemotelyAccessible)
.map(AppFileSystem::getName)
.collect(Collectors.toList());
return fileSystems.values().stream()
.filter(AppFileSystem::isRemotelyAccessible)
.map(AppFileSystem::getName)
.collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -333,12 +333,4 @@ public EventsBus getEventsBus() {
public void close() {
closeFileSystems();
}

public void setShortTimeExecutionComputationManager(ComputationManager shortTimeExecutionComputationManager) {
this.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
}

public void setLongTimeExecutionComputationManager(ComputationManager longTimeExecutionComputationManager) {
this.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
}
}
6 changes: 3 additions & 3 deletions afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.afs;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.afs.storage.AfsStorageException;
import com.powsybl.afs.storage.AppStorage;
Expand All @@ -18,6 +17,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

/**
* An AppFileSystem instance is a tree of {@link Node} objects, starting with its root folder.
Expand Down Expand Up @@ -98,7 +98,7 @@ public Node createNode(NodeInfo nodeInfo) {
} else if (Project.PSEUDO_CLASS.equals(nodeInfo.getPseudoClass())) {
return new Project(context);
} else {
FileExtension extension = data.getFileExtensionByPseudoClass(nodeInfo.getPseudoClass());
FileExtension<?> extension = data.getFileExtensionByPseudoClass(nodeInfo.getPseudoClass());
if (extension != null) {
return extension.createFile(context);
} else {
Expand Down Expand Up @@ -153,7 +153,7 @@ public AbstractNodeBase fetchNode(String nodeId, boolean connected) {
return new ProjectFolder(context);
}

ProjectFileExtension extension = data.getProjectFileExtensionByPseudoClass(projectFileInfo.getPseudoClass());
ProjectFileExtension<?, ?> extension = data.getProjectFileExtensionByPseudoClass(projectFileInfo.getPseudoClass());
if (extension != null) {
return extension.createProjectFile(context);
}
Expand Down
Loading