com.xlrit.gears.server.graphql.TestingResolver Maven / Gradle / Ivy
The newest version!
package com.xlrit.gears.server.graphql;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.time.OffsetDateTime;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.xlrit.gears.base.exception.AuthException;
import com.xlrit.gears.base.exception.DataException;
import com.xlrit.gears.base.function.DefaultFunctions;
import com.xlrit.gears.engine.data.DataManager;
import com.xlrit.gears.engine.export.ExportManager;
import com.xlrit.gears.engine.importer.ImportManager;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequiredArgsConstructor
public class TestingResolver {
private static final Logger LOG = LoggerFactory.getLogger(TestingResolver.class);
private final DataManager dataManager;
private final ExportManager exportManager;
private final ImportManager importManager;
@Value("${gears.testing.enabled}")
private boolean enableTesting;
// === queries === //
@QueryMapping
public boolean checkData(@Argument String deploymentId, @Argument String jpql) {
LOG.info("checkData: deploymentId={}, jpql={}", deploymentId, jpql);
ensureTestingEnabled();
return dataManager.checkData(jpql);
}
@QueryMapping
public JsonNode queryData(@Argument String deploymentId, @Argument String jpql, @Argument List types) {
LOG.info("queryData: deploymentId={}, jpql={}", deploymentId, jpql);
ensureTestingEnabled();
return types == null ? dataManager.queryData(jpql) : dataManager.queryData(jpql, types);
}
@QueryMapping
public JsonNode evaluate(@Argument String deploymentId, @Argument String snel) {
LOG.info("queryData: deploymentId={}, snel={}", deploymentId, snel);
ensureTestingEnabled();
return dataManager.evaluate(snel);
}
@QueryMapping
public OffsetDateTime currentDateTime() {
ensureTestingEnabled();
return DefaultFunctions.currentDatetime();
}
// === mutations === //
@MutationMapping
public boolean loadData(@Argument List files) {
ensureTestingEnabled();
for (MultipartFile file : files) {
try (InputStream is = file.getInputStream(); Reader r = new InputStreamReader(is)) {
dataManager.loadData(file.getOriginalFilename(), r);
}
catch (Exception e) {
throw new DataException("Unable to load data", e);
}
}
return true;
}
@MutationMapping("import")
public boolean importRaw(@Argument String target, @Argument String source, @Argument List raw) {
LOG.info("import: target={}, source={}", target, source);
ensureTestingEnabled();
for (MultipartFile file : raw) {
try (InputStream is = file.getInputStream(); Reader reader = new InputStreamReader(is)) {
importManager.importRaw(target, source, reader);
}
catch (Exception e) {
throw new DataException("Unable to load data", e);
}
}
return true;
}
@MutationMapping
public boolean initializeAuditing(@Argument String prefix) {
ensureTestingEnabled();
try {
dataManager.initializeAuditing(prefix);
}
catch (Exception e) {
throw new DataException("Unable to initialize auditing tables", e);
}
return true;
}
@MutationMapping
public JsonNode export(@Argument String deploymentId, @Argument String target, @Argument String exportSpec) {
LOG.info("export: deploymentId={}, target={}, exportSpec={}", deploymentId, target, exportSpec);
ensureTestingEnabled();
return exportManager.export(exportSpec, target);
}
@MutationMapping
public boolean overrideCurrentDateTime(@Argument OffsetDateTime value) {
ensureTestingEnabled();
DefaultFunctions.setCurrentDateTime(value);
return true;
}
@MutationMapping
public boolean clearCurrentDateTime() {
ensureTestingEnabled();
DefaultFunctions.setCurrentDateTime(null);
return true;
}
@MutationMapping
public boolean collectGarbage() {
ensureTestingEnabled();
System.gc();
return true;
}
private void ensureTestingEnabled() {
if (!enableTesting) {
throw new AuthException("Testing features are not enabled");
}
}
}