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

WIP Clean AbstractAppStorageTest #173

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -19,6 +19,7 @@
import com.powsybl.afs.storage.check.FileSystemCheckOptionsBuilder;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.CassandraContainer;
Expand Down Expand Up @@ -47,12 +48,12 @@ class CassandraAppStorageTest extends AbstractAppStorageTest {
private static CqlSession cassandraSession;

@BeforeAll
public static void dontRunOnWindows() {
static void dontRunOnWindows() {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
}

@BeforeAll
public static void setUpCassandra() {
static void setUpCassandra() {
// Create container
cassandra = new CassandraContainer<>("cassandra:3.11.5")
.withInitScript("afs.cql");
Expand All @@ -69,7 +70,7 @@ public static void setUpCassandra() {
}

@AfterAll
public static void tearDownCassandra() {
static void tearDownCassandra() {
if (cassandraSession != null) {
cassandraSession.close();
if (cassandra != null) {
Expand All @@ -78,12 +79,19 @@ public static void tearDownCassandra() {
}
}

// @Override
// @BeforeEach
// public void setUp() {
// eventStack = new LinkedBlockingQueue<>();
// this.storage = createStorage();
// this.storage.getEventsBus().addListener(l);
// }

@AfterEach
@Override
@BeforeEach
public void setUp() {
eventStack = new LinkedBlockingQueue<>();
this.storage = createStorage();
this.storage.getEventsBus().addListener(l);
public void tearDown() {
super.tearDown();
clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ public void setMetadata(String nodeId, NodeGenericMetadata metadata) {
@Override
public void renameNode(String nodeId, String name) {
UUID nodeUuid = checkNodeId(nodeId);
Objects.requireNonNull(name);
if (name.isEmpty()) {
throw new IllegalArgumentException("Impossible to rename node '" + nodeId + "' with an empty name");
}

NodeInfo nodeInfo = getNodeInfo(nodeId);
getParentNode(nodeId).ifPresent(parentNode -> {
UUID parentNodeUuid = checkNodeId(parentNode.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,57 @@
@Tag(name = "afs")
public class StorageServer {

private static final Logger LOGGER = LoggerFactory.getLogger(StorageServer.class);
public static final String API_VERSION = "v1";

private static final Logger LOGGER = LoggerFactory.getLogger(StorageServer.class);
private final AppDataWrapper appDataWrapper;

@Autowired
public StorageServer(AppDataWrapper appDataWrapper) {
this.appDataWrapper = appDataWrapper;
}

private static StreamingResponseBody copyToBodyAndClose(InputStream inputStream) {
return outputStream -> {
try (InputStream toClose = inputStream) {
IOUtils.copy(toClose, outputStream);
}
};
}

private static <T> ResponseEntity<T> ok() {
return ResponseEntity.ok().build();
}

private static <T> ResponseEntity<T> noContent() {
return ResponseEntity.noContent().build();
}

private static <T> ResponseEntity<T> ok(T body) {
return ResponseEntity.ok(body);
}

private static <T> ResponseEntity<T> okIfPresent(Optional<T> body) {
return body
.map(StorageServer::ok)
.orElseGet(StorageServer::noContent);
}

private static void logInfo(String message, Object... params) {
if (LOGGER.isInfoEnabled()) {
Object[] objects = Arrays.stream(params)
.map(StorageServer::encode)
.toArray();
LOGGER.info(message, objects);
}
}

private static Object encode(Object input) {
if (input instanceof String s) {
return s.replaceAll("[\n\r\t]", "_");
}
return input;
}

@GetMapping(value = "fileSystems")
@Operation(summary = "Get file system list", responses = {
@ApiResponse(content = @Content(schema = @Schema(implementation = List.class))),
Expand Down Expand Up @@ -418,14 +459,6 @@ public ResponseEntity<StreamingResponseBody> readBinaryAttribute(@Parameter(desc
return okIfPresent(storage.readBinaryData(nodeId, name).map(StorageServer::copyToBodyAndClose));
}

private static StreamingResponseBody copyToBodyAndClose(InputStream inputStream) {
return outputStream -> {
try (InputStream toClose = inputStream) {
IOUtils.copy(toClose, outputStream);
}
};
}

@GetMapping(value = "fileSystems/{fileSystemName}/nodes/{nodeId}/data/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
@Operation(summary = "", responses = {
@ApiResponse(content = @Content(schema = @Schema(implementation = String.class))),
Expand Down Expand Up @@ -698,38 +731,4 @@ public ResponseEntity<String> setMetadata(@Parameter(description = "File system
storage.setMetadata(nodeId, nodeMetadata);
return ok();
}

private static <T> ResponseEntity<T> ok() {
return ResponseEntity.ok().build();
}

private static <T> ResponseEntity<T> noContent() {
return ResponseEntity.noContent().build();
}

private static <T> ResponseEntity<T> ok(T body) {
return ResponseEntity.ok(body);
}

private static <T> ResponseEntity<T> okIfPresent(Optional<T> body) {
return body
.map(StorageServer::ok)
.orElseGet(StorageServer::noContent);
}

private static void logInfo(String message, Object... params) {
if (LOGGER.isInfoEnabled()) {
Object[] objects = Arrays.stream(params)
.map(StorageServer::encode)
.toArray();
LOGGER.info(message, objects);
}
}

private static Object encode(Object input) {
if (input instanceof String s) {
return s.replaceAll("[\n\r\t]", "_");
}
return input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.powsybl.computation.ComputationManager;
import jakarta.servlet.ServletContext;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
Expand Down Expand Up @@ -79,6 +80,12 @@ public AppData getAppData() {
}
}

@AfterEach
public void tearDown() {
clearAllNodes();
super.tearDown();
}

private URI getRestUri() {
try {
String sheme = "http";
Expand Down
Loading
Loading