From 0faf6d0f0a7721ce3d2222c605e06113a94501f7 Mon Sep 17 00:00:00 2001 From: Sergii Date: Thu, 2 Aug 2018 20:11:04 -0700 Subject: [PATCH] #38 Testing Framework - database cleanup and import of test folder --- .../gallerymine/backend/beans/AppConfig.java | 9 ++ .../gallerymine/model/PictureInformation.java | 2 +- .../backend/services/ImportService.java | 6 +- .../backend/utils/ImportUtils.java | 19 ++- .../gallerymine/GalleryMineApplication.java | 3 +- .../mvc/ImportRequestsController.java | 5 +- .../frontend/mvc/MainPageController.java | 21 ++-- ....properties => application.properties_old} | 0 ui/web/src/main/resources/application.yml | 1 + ...lTest.java => CucumberFunctionalTest.java} | 8 +- .../test/ui/CucumberIntegrationTest.java | 14 --- .../test/ui/CucumberSetupTest.java | 12 ++ .../test/ui/SpringIntegrationTest.java | 119 ++++++++++-------- .../test/ui/StepDefsDatabaseLowLevelTest.java | 86 +++++++++++++ .../test/ui/StepDefsIntegrationTest.java | 66 +--------- .../src/test/resources/application.properties | 3 + ui/web/src/test/resources/application.yml | 8 -- ui/web/src/test/resources/folders.feature | 7 -- .../test/resources/functional/folders.feature | 7 ++ .../setup/ReImportTestDatabase.feature | 8 ++ 20 files changed, 243 insertions(+), 161 deletions(-) rename ui/web/src/main/resources/{application.properties => application.properties_old} (100%) rename ui/web/src/test/java/gallerymine/test/ui/{CucumberLocalTest.java => CucumberFunctionalTest.java} (52%) delete mode 100644 ui/web/src/test/java/gallerymine/test/ui/CucumberIntegrationTest.java create mode 100644 ui/web/src/test/java/gallerymine/test/ui/CucumberSetupTest.java create mode 100644 ui/web/src/test/java/gallerymine/test/ui/StepDefsDatabaseLowLevelTest.java create mode 100644 ui/web/src/test/resources/application.properties delete mode 100644 ui/web/src/test/resources/application.yml delete mode 100644 ui/web/src/test/resources/folders.feature create mode 100644 ui/web/src/test/resources/functional/folders.feature create mode 100644 ui/web/src/test/resources/setup/ReImportTestDatabase.feature diff --git a/core/beans/src/main/java/gallerymine/backend/beans/AppConfig.java b/core/beans/src/main/java/gallerymine/backend/beans/AppConfig.java index 114f01e..5fadbed 100644 --- a/core/beans/src/main/java/gallerymine/backend/beans/AppConfig.java +++ b/core/beans/src/main/java/gallerymine/backend/beans/AppConfig.java @@ -31,6 +31,9 @@ public class AppConfig { @Value("${gallery.paths.importExposedRootFolder}") public String importExposedRootFolder; + @Value("${gallery.paths.importTestRootFolder}") + public String importTestRootFolder; + @Value("${gallery.debug.dryRunImportMoves}") public boolean dryRunImportMoves = true; @@ -72,6 +75,11 @@ public String getImportExposedRootFolder() { return importExposedRootFolder; } + public String getImportTestRootFolder() { + checkFolders(); + return importTestRootFolder; + } + public String relativizePathToGallery(String filePath) throws FileNotFoundException { return relativizePath(filePath, galleryRootFolder); } @@ -137,6 +145,7 @@ private void checkFolders() { thumbsRootFolder = amendFolder(thumbsRootFolder); importRootFolder = amendFolder(importRootFolder); importExposedRootFolder = amendFolder(importExposedRootFolder); + importTestRootFolder = amendFolder(importTestRootFolder); if (importRootFolderPath == null) { importRootFolderPath = Paths.get(importRootFolder); diff --git a/core/beans/src/main/java/gallerymine/model/PictureInformation.java b/core/beans/src/main/java/gallerymine/model/PictureInformation.java index e4fe8da..b45d96c 100644 --- a/core/beans/src/main/java/gallerymine/model/PictureInformation.java +++ b/core/beans/src/main/java/gallerymine/model/PictureInformation.java @@ -16,7 +16,7 @@ @Document @Data @ToString(callSuper = true) -public class PictureInformation extends FileInformation { +public abstract class PictureInformation extends FileInformation { private long width; private long height; diff --git a/core/libs/image-importer/src/main/java/gallerymine/backend/services/ImportService.java b/core/libs/image-importer/src/main/java/gallerymine/backend/services/ImportService.java index 6979274..3254b30 100644 --- a/core/libs/image-importer/src/main/java/gallerymine/backend/services/ImportService.java +++ b/core/libs/image-importer/src/main/java/gallerymine/backend/services/ImportService.java @@ -111,14 +111,14 @@ public ImportRequest getFresh(ImportRequest request) { return request; } - public ImportRequest prepareImportFolder(boolean enforce) throws ImportFailedException { + public ImportRequest prepareImportFolder(boolean enforce, boolean testFolder) throws ImportFailedException { Process process = new Process(); ImportRequest importRequest = null; try { - process.setName("Processing of Import"); + process.setName("Processing of Import"+(testFolder?" of TEST folder":"")); process.setStatus(ProcessStatus.PREPARING); process.setType(ProcessType.IMPORT); - String importInternalPath = importUtils.prepareImportFolder(enforce, process); + String importInternalPath = importUtils.prepareImportFolder(enforce, process, testFolder); importRequest = registerNewImportFolderRequest(importInternalPath, null, process.getId()); } catch (ImportFailedException e) { diff --git a/core/libs/image-importer/src/main/java/gallerymine/backend/utils/ImportUtils.java b/core/libs/image-importer/src/main/java/gallerymine/backend/utils/ImportUtils.java index 285f821..90af7d4 100644 --- a/core/libs/image-importer/src/main/java/gallerymine/backend/utils/ImportUtils.java +++ b/core/libs/image-importer/src/main/java/gallerymine/backend/utils/ImportUtils.java @@ -193,8 +193,11 @@ public void moveFileStructure(Path source, Path target, AtomicLong files, Atomic } } - public String prepareImportFolder(boolean enforce, Process process) throws ImportFailedException { - Path pathExposed = Paths.get(appConfig.getImportExposedRootFolder()); + public String prepareImportFolder(boolean enforce, Process process, boolean testFolder) throws ImportFailedException { + Path pathExposed = testFolder ? + Paths.get(appConfig.getImportTestRootFolder()) + : + Paths.get(appConfig.getImportExposedRootFolder()); Path pathToImport = appConfig.getImportRootFolderPath().resolve(DateTime.now().toString("yyyy-MM-dd_HH-mm-ss_SSS")); Path pathToImportSource = pathToImport.resolve(FOLDER_SOURCE); @@ -234,7 +237,14 @@ public String prepareImportFolder(boolean enforce, Process process) throws Impor AtomicLong files = new AtomicLong(); AtomicLong folders = new AtomicLong(); + boolean dryRunBefore = appConfig.dryRunImportMoves; try { + if (testFolder) { + process.addNote("Import of test folder"); + process.addNote(" Set the dryRun ON. Before was {}", dryRunBefore); + process.addNote(" Test folder {}", pathExposed); + appConfig.setDryRunImportMoves(true); + } moveFileStructure(pathExposed, pathToImportSource, files, folders); @@ -269,6 +279,11 @@ public String prepareImportFolder(boolean enforce, Process process) throws Impor return processEntity; }); throw new ImportFailedException("Failed to index. Reason: Failed to move files from exposed folder. Reason: " + e.getMessage(), e); + } finally { + if (testFolder) { + process.addNote("Import of test folder. Restore the dryRun to {}", dryRunBefore); + appConfig.setDryRunImportMoves(dryRunBefore); + } } // String pathToIndex = appConfig.relativizePathToImport(pathToImport.resolve(FOLDER_SOURCE)).toString(); diff --git a/ui/web/src/main/java/gallerymine/GalleryMineApplication.java b/ui/web/src/main/java/gallerymine/GalleryMineApplication.java index a7fb5cd..8cfe721 100644 --- a/ui/web/src/main/java/gallerymine/GalleryMineApplication.java +++ b/ui/web/src/main/java/gallerymine/GalleryMineApplication.java @@ -24,6 +24,7 @@ import org.springframework.context.annotation.*; import org.springframework.core.task.TaskExecutor; import org.springframework.data.mongodb.config.EnableMongoAuditing; +import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import gallerymine.model.Customer; import gallerymine.backend.beans.repository.CustomerRepository; @@ -50,7 +51,7 @@ public class GalleryMineApplication implements CommandLineRunner { @Autowired private CustomerRepository repository; - @Bean + @Bean public RestTemplate restTemplate() { return new RestTemplate(); } diff --git a/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java b/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java index aa02860..4630d88 100755 --- a/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java +++ b/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java @@ -91,9 +91,10 @@ public Object listActive() { @GetMapping("import") @ResponseBody - public Object importFolder(@RequestParam(value = "enforce", defaultValue = "false", required = false) boolean enforce) { + public Object importFolder(@RequestParam(value = "enforce", defaultValue = "false", required = false) boolean enforce, + @RequestParam(value = "test", defaultValue = "", required = false) boolean testFolder) { try { - ImportRequest request = importService.prepareImportFolder(enforce); + ImportRequest request = importService.prepareImportFolder(enforce, testFolder); return responseOk() .put("id", request.getId()) diff --git a/ui/web/src/main/java/gallerymine/frontend/mvc/MainPageController.java b/ui/web/src/main/java/gallerymine/frontend/mvc/MainPageController.java index 384c18d..71db8f6 100644 --- a/ui/web/src/main/java/gallerymine/frontend/mvc/MainPageController.java +++ b/ui/web/src/main/java/gallerymine/frontend/mvc/MainPageController.java @@ -15,11 +15,9 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestAttribute; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.websocket.server.PathParam; @@ -27,14 +25,15 @@ import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.Date; -import java.util.List; -import java.util.Optional; +import java.util.*; @Controller @RequestMapping("/") public class MainPageController { + @Autowired + protected MongoTemplate mongoTemplate = null; + @Autowired FileRepository fileRepository; @@ -47,6 +46,14 @@ public class MainPageController { @Autowired private ObjectMapper jacksonObjectMapper; + @GetMapping("/version") + @ResponseBody + public Map getVersion() { + Map info = new HashMap<>(); + info.put("database", mongoTemplate.getDb().getName()); + return info; + } + @GetMapping public ModelAndView list(Principal principal) { Page files = fileRepository.findAll(new PageRequest(0, 50, new Sort(new Sort.Order(Sort.Direction.DESC, "id")))); diff --git a/ui/web/src/main/resources/application.properties b/ui/web/src/main/resources/application.properties_old similarity index 100% rename from ui/web/src/main/resources/application.properties rename to ui/web/src/main/resources/application.properties_old diff --git a/ui/web/src/main/resources/application.yml b/ui/web/src/main/resources/application.yml index cf4db92..859d872 100644 --- a/ui/web/src/main/resources/application.yml +++ b/ui/web/src/main/resources/application.yml @@ -43,6 +43,7 @@ gallery: thumbsRootFolder: ~/work_mine/data/thumbsFolder/ importRootFolder: ~/work_mine/data/importFolder/ importExposedRootFolder: ~/work_mine/data/importExposedFolder/ + importTestRootFolder: ~/work_mine/data/importTestFolder/ debug: dryRunImportMoves: true diff --git a/ui/web/src/test/java/gallerymine/test/ui/CucumberLocalTest.java b/ui/web/src/test/java/gallerymine/test/ui/CucumberFunctionalTest.java similarity index 52% rename from ui/web/src/test/java/gallerymine/test/ui/CucumberLocalTest.java rename to ui/web/src/test/java/gallerymine/test/ui/CucumberFunctionalTest.java index 06b2687..4b27de4 100644 --- a/ui/web/src/test/java/gallerymine/test/ui/CucumberLocalTest.java +++ b/ui/web/src/test/java/gallerymine/test/ui/CucumberFunctionalTest.java @@ -1,12 +1,12 @@ package gallerymine.test.ui; +import org.junit.runner.RunWith; + import cucumber.api.CucumberOptions; -import cucumber.api.java.Before; import cucumber.api.junit.Cucumber; -import org.junit.runner.RunWith; @RunWith(Cucumber.class) -@CucumberOptions(features = "src/test/resources") -public class CucumberLocalTest extends SpringIntegrationTest { +@CucumberOptions(features = "src/test/resources/functional") +public class CucumberFunctionalTest extends SpringIntegrationTest { } \ No newline at end of file diff --git a/ui/web/src/test/java/gallerymine/test/ui/CucumberIntegrationTest.java b/ui/web/src/test/java/gallerymine/test/ui/CucumberIntegrationTest.java deleted file mode 100644 index 34cf84c..0000000 --- a/ui/web/src/test/java/gallerymine/test/ui/CucumberIntegrationTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package gallerymine.test.ui; - -import org.junit.runner.RunWith; - -import cucumber.api.CucumberOptions; -import cucumber.api.junit.Cucumber; -import org.springframework.context.annotation.Bean; -import org.springframework.web.client.RestTemplate; - -@RunWith(Cucumber.class) -@CucumberOptions(features = "src/test/resources") -public class CucumberIntegrationTest extends SpringIntegrationTest { - -} \ No newline at end of file diff --git a/ui/web/src/test/java/gallerymine/test/ui/CucumberSetupTest.java b/ui/web/src/test/java/gallerymine/test/ui/CucumberSetupTest.java new file mode 100644 index 0000000..c835250 --- /dev/null +++ b/ui/web/src/test/java/gallerymine/test/ui/CucumberSetupTest.java @@ -0,0 +1,12 @@ +package gallerymine.test.ui; + +import org.junit.runner.RunWith; + +import cucumber.api.CucumberOptions; +import cucumber.api.junit.Cucumber; + +@RunWith(Cucumber.class) +@CucumberOptions(features = "src/test/resources/setup") +public class CucumberSetupTest extends SpringIntegrationTest { + +} \ No newline at end of file diff --git a/ui/web/src/test/java/gallerymine/test/ui/SpringIntegrationTest.java b/ui/web/src/test/java/gallerymine/test/ui/SpringIntegrationTest.java index 07cc749..20776c4 100644 --- a/ui/web/src/test/java/gallerymine/test/ui/SpringIntegrationTest.java +++ b/ui/web/src/test/java/gallerymine/test/ui/SpringIntegrationTest.java @@ -4,88 +4,105 @@ import java.util.HashMap; import java.util.Map; -import cucumber.api.java.Before; import gallerymine.GalleryMineApplication; +import gallerymine.model.mvc.SourceCriteria; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.context.annotation.Bean; +import org.springframework.context.expression.MapAccessor; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.SpelParserConfiguration; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.StandardEvaluationContext; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.http.client.ClientHttpResponse; import org.springframework.test.context.ContextConfiguration; -import org.springframework.web.client.ResponseErrorHandler; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + //@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = GalleryMineApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration public class SpringIntegrationTest { - static ResponseResults latestResponse = null; static ResponseEntity latestResponseMap = null; + static Map context = new HashMap<>(); + @LocalServerPort public int serverPort; @Autowired protected RestTemplate restTemplate; - void executeGet(String url) throws IOException { - final Map headers = new HashMap<>(); - headers.put("Accept", "application/json"); - final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); - final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler(); - - restTemplate.setErrorHandler(errorHandler); - latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, response -> { - if (errorHandler.hadError) { - return (errorHandler.getResults()); - } else { - return (new ResponseResults(response)); - } - }); + protected void updateLastResponse(ResponseEntity lrm) { + latestResponseMap = lrm; + context.put("response", lrm); } - void executePost() throws IOException { - final Map headers = new HashMap<>(); - headers.put("Accept", "application/json"); - final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); - final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler(); + public void runPostRequestSuccess(Object requestEntity, String url) { + runPostRequest(requestEntity, url); + checkResponseOK(); + } - if (restTemplate == null) { - restTemplate = new RestTemplate(); - } + public void runGetRequestSuccess(String url) throws IOException { + runGetRequest(url); + checkResponseOK(); + } - restTemplate.setErrorHandler(errorHandler); - latestResponse = restTemplate - .execute("http://localhost:"+serverPort+"/baeldung", HttpMethod.POST, requestCallback, response -> { - if (errorHandler.hadError) { - return (errorHandler.getResults()); - } else { - return (new ResponseResults(response)); - } - }); + private void checkResponseOK() { + final HttpStatus currentStatusCode = latestResponseMap.getStatusCode(); + assertThat("status code is incorrect : " + latestResponseMap.getStatusCode(), currentStatusCode.value(), is(200)); } - private class ResponseResultErrorHandler implements ResponseErrorHandler { - private ResponseResults results = null; - private Boolean hadError = false; + public void runPostRequest(Object requestEntity, String url) { + MultiValueMap headers = new LinkedMultiValueMap<>(); + headers.add("Accept", "application/json"); + headers.add("Content-Type", "application/json; charset=UTF-8"); + HttpEntity request = new HttpEntity(requestEntity, headers); + updateLastResponse(restTemplate .exchange("http://localhost:"+serverPort+url, HttpMethod.POST, request, HashMap.class)); + } - private ResponseResults getResults() { - return results; - } + public void runGetRequest(String url) throws IOException { + MultiValueMap headers = new LinkedMultiValueMap<>(); + headers.add("Accept", "application/json"); + headers.add("Content-Type", "application/json; charset=UTF-8"); + HttpEntity request = new HttpEntity(null, headers); + updateLastResponse(restTemplate.exchange("http://localhost:"+serverPort+url, HttpMethod.GET, request, HashMap.class)); + } - @Override - public boolean hasError(ClientHttpResponse response) throws IOException { - hadError = response.getRawStatusCode() >= 400; - return hadError; - } + public T resolveExpression(String expression) { + return resolveExpression(expression , null); + } - @Override - public void handleError(ClientHttpResponse response) throws IOException { - results = new ResponseResults(response); + public T resolveExpression(String expression, Class clazzRequired) { + SpelParserConfiguration config = new SpelParserConfiguration(true,true); + ExpressionParser parser = new SpelExpressionParser(config); + StandardEvaluationContext contextEL = new StandardEvaluationContext(context); + contextEL.addPropertyAccessor(new MapAccessor()); + Expression exp = parser.parseExpression(expression); + if (clazzRequired != null) { + return exp.getValue(contextEL, clazzRequired); + } else { + return (T)exp.getValue(contextEL); } } + + /** Put value into context */ + public void put(String key, Object value) { + context.put(key, value); + } + + /** Put value into context */ + public T get(String key) { + return (T)context.get(key); + } + } \ No newline at end of file diff --git a/ui/web/src/test/java/gallerymine/test/ui/StepDefsDatabaseLowLevelTest.java b/ui/web/src/test/java/gallerymine/test/ui/StepDefsDatabaseLowLevelTest.java new file mode 100644 index 0000000..eec573e --- /dev/null +++ b/ui/web/src/test/java/gallerymine/test/ui/StepDefsDatabaseLowLevelTest.java @@ -0,0 +1,86 @@ +package gallerymine.test.ui; + +import cucumber.api.java.en.Given; +import cucumber.api.java.en.When; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class StepDefsDatabaseLowLevelTest extends SpringIntegrationTest { + + private static Logger log = LoggerFactory.getLogger(StepDefsDatabaseLowLevelTest.class); + + @Autowired + protected MongoTemplate template = null; + + @Given("^check if test database$") + public void checkIfTestDatabase() throws Throwable { + runGetRequestSuccess("/configprops"); + String databaseName = resolveExpression("response.body['spring.data.mongodb-org.springframework.boot.autoconfigure.mongo.MongoProperties'].properties.uri", String.class); + assertTrue("Database is not test!", databaseName.endsWith("/galleryMineTest")); + } + + @Given("^run import for test folder$") + public void runImportForTestFolder() throws Throwable { + log.info("Initiated import of test folder"); + runGetRequestSuccess("/importing/import?test=true"); + + String importRequestId = resolveExpression("response.body.id", String.class); + put("importRequestId", importRequestId); + log.info("Import Request id={}", importRequestId); + } + + @Given("^wait import request become (.*) for (\\d+) seconds$") + public void waitImportRequestStatus(String statusExpected, long seconds) throws Throwable { + long startedAt = System.currentTimeMillis(); + String importRequestId = get("importRequestId"); + String statusCurrent; + do { + runGetRequestSuccess("/importing/" + importRequestId); + statusCurrent = resolveExpression("response.body.result.status"); + Thread.sleep(1000); + log.info(" import request id={} status={} expected status={}", importRequestId, statusCurrent, statusExpected); + } while ( + ((startedAt+seconds*100) < System.currentTimeMillis()) + && + !statusExpected.equals(statusCurrent) + ); + assertEquals("Status of import request is not right", statusExpected, statusCurrent); + log.info(" import request id={} status={} in {} ms", importRequestId, statusCurrent, System.currentTimeMillis() - startedAt); + } + + @When("^database cleanup all$") + public void databaseCleanupAll() { + template.dropCollection("customer"); + template.dropCollection("fileInformation"); + template.dropCollection("getCodeRequest"); + template.dropCollection("importRequest"); + template.dropCollection("importSource"); + template.dropCollection("indexRequest"); + template.dropCollection("picture"); + template.dropCollection("pictureFolder"); + template.dropCollection("process"); + template.dropCollection("source"); + template.dropCollection("thumbRequest"); + } + + @When("^database cleanup but gallery") + public void databaseCleanupAllButGallery() { + template.dropCollection("customer"); + template.dropCollection("fileInformation"); + template.dropCollection("getCodeRequest"); + template.dropCollection("importRequest"); + template.dropCollection("importSource"); + template.dropCollection("indexRequest"); +// template.dropCollection("picture"); +// template.dropCollection("pictureFolder"); + template.dropCollection("process"); + template.dropCollection("source"); + template.dropCollection("thumbRequest"); + } + +} \ No newline at end of file diff --git a/ui/web/src/test/java/gallerymine/test/ui/StepDefsIntegrationTest.java b/ui/web/src/test/java/gallerymine/test/ui/StepDefsIntegrationTest.java index 3ce1608..85bb1d0 100644 --- a/ui/web/src/test/java/gallerymine/test/ui/StepDefsIntegrationTest.java +++ b/ui/web/src/test/java/gallerymine/test/ui/StepDefsIntegrationTest.java @@ -8,69 +8,29 @@ import cucumber.api.java.en.Given; import gallerymine.model.mvc.SourceCriteria; import gallerymine.model.support.PictureGrade; -import org.springframework.context.expression.MapAccessor; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.Expression; -import org.springframework.expression.ExpressionParser; -import org.springframework.expression.spel.SpelParserConfiguration; -import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.ReflectivePropertyAccessor; -import org.springframework.expression.spel.support.StandardEvaluationContext; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import cucumber.api.java.en.And; import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; - -import java.util.HashMap; public class StepDefsIntegrationTest extends SpringIntegrationTest { - @When("^the client calls /baeldung$") - public void the_client_issues_POST_hello() throws Throwable { - executePost(); - } - - @Given("^the client calls /hello$") - public void the_client_issues_GET_hello() throws Throwable { - executeGet("http://localhost:"+serverPort+"/hello"); - } - - @Given("^api call /listFolders") - public void the_client_issues_list_folders() { + @Given("^api call /listFolders (.+)") + public void the_client_issues_list_folders(String path) { SourceCriteria searchCriteria = new SourceCriteria(); searchCriteria.setPage(0); searchCriteria.setSize(10); - searchCriteria.setPath("test"); + searchCriteria.setPath(path); searchCriteria.setGrade(PictureGrade.GALLERY); - MultiValueMap headers = new LinkedMultiValueMap<>(); - headers.add("Accept", "application/json"); - headers.add("Content-Type", "application/json; charset=UTF-8"); - HttpEntity request = new HttpEntity<>(searchCriteria, headers); - latestResponseMap = restTemplate .exchange("http://localhost:"+serverPort+"/sources/findPath", HttpMethod.POST, request, HashMap.class); - System.out.print("Run"); - } - - @When("^the client calls /version$") - public void the_client_issues_GET_version() throws Throwable { - executeGet("http://localhost:"+serverPort+"/version"); + runPostRequest(searchCriteria, "/sources/findPath"); } @Then("^response status code is (\\d+)$") - public void the_client_receives_status_code_of(int statusCode) { + public void checkResponseStatusCode(int statusCode) { final HttpStatus currentStatusCode = latestResponseMap.getStatusCode(); assertThat("status code is incorrect : " + latestResponseMap.getStatusCode(), currentStatusCode.value(), is(statusCode)); } - @And("^the client receives server version (.+)$") - public void the_client_receives_server_version_body(String version) { - assertThat(latestResponse.getBody(), is(version)); - } - @And("^response has (.+)$") public void responseHas(String key) { assertTrue("Key not found in response key='"+key+"'", latestResponseMap.getBody().containsKey(key)); @@ -83,20 +43,4 @@ public void responseValue(String key, String valueStr) { assertEquals("Value not equal for '"+key+"' which is '"+valueActual+"' != '"+valueExpected+"'",valueExpected, valueActual); } - public Object resolveExpression(String expression) { - return resolveExpression(expression , null); - } - - public Object resolveExpression(String expression, Class clazzRequired) { - SpelParserConfiguration config = new SpelParserConfiguration(true,true); - ExpressionParser parser = new SpelExpressionParser(config); - StandardEvaluationContext context = new StandardEvaluationContext(latestResponseMap); - context.addPropertyAccessor(new MapAccessor()); - Expression exp = parser.parseExpression(expression); - if (clazzRequired != null) { - return exp.getValue(context, clazzRequired); - } else { - return exp.getValue(context); - } - } } \ No newline at end of file diff --git a/ui/web/src/test/resources/application.properties b/ui/web/src/test/resources/application.properties new file mode 100644 index 0000000..fd3f44c --- /dev/null +++ b/ui/web/src/test/resources/application.properties @@ -0,0 +1,3 @@ +spring.data.mongodb.uri=mongodb://localhost:27017/galleryMineTest + +gallery.debug.dryRunImportMoves=true diff --git a/ui/web/src/test/resources/application.yml b/ui/web/src/test/resources/application.yml deleted file mode 100644 index 92b2b40..0000000 --- a/ui/web/src/test/resources/application.yml +++ /dev/null @@ -1,8 +0,0 @@ -spring: - data: - mongodb.uri: mongodb://localhost:27017/galleryMineTest - -javamelody: - # Enable JavaMelody auto-configuration (optional, default: true) - enabled: false - diff --git a/ui/web/src/test/resources/folders.feature b/ui/web/src/test/resources/folders.feature deleted file mode 100644 index 58fb84e..0000000 --- a/ui/web/src/test/resources/folders.feature +++ /dev/null @@ -1,7 +0,0 @@ -Feature: Test Main Controller methods - - Scenario: Test list Folders of gallery - When api call /listFolders - Then response status code is 200 - And response key body.status equal to 200 - And response key body.list.size equal to 10 diff --git a/ui/web/src/test/resources/functional/folders.feature b/ui/web/src/test/resources/functional/folders.feature new file mode 100644 index 0000000..ea135c2 --- /dev/null +++ b/ui/web/src/test/resources/functional/folders.feature @@ -0,0 +1,7 @@ +Feature: Test Sources Controller methods + + Scenario: Test list Folders of gallery + When api call /listFolders test + Then response status code is 200 + And response key response.body.status equal to 200 + And response key response.body.list.totalElements equal to 10 diff --git a/ui/web/src/test/resources/setup/ReImportTestDatabase.feature b/ui/web/src/test/resources/setup/ReImportTestDatabase.feature new file mode 100644 index 0000000..874d4f8 --- /dev/null +++ b/ui/web/src/test/resources/setup/ReImportTestDatabase.feature @@ -0,0 +1,8 @@ +Feature: Feature to cleanup and rebuild GalleryMineTest database + + Scenario: Cleanup database + When check if test database + And database cleanup all + Then run import for test folder + And wait import request become APPROVING for 30 seconds + And response key body.list.totalElements equal to 10