Skip to content

Commit

Permalink
#38 Testing Framework - database cleanup and import of test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
spuliaiev-sfdc committed Aug 3, 2018
1 parent 46546a9 commit 0faf6d0
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);


Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion ui/web/src/main/java/gallerymine/GalleryMineApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,7 +51,7 @@ public class GalleryMineApplication implements CommandLineRunner {
@Autowired
private CustomerRepository repository;

@Bean
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,25 @@
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;
import java.security.Principal;
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;

Expand All @@ -47,6 +46,14 @@ public class MainPageController {
@Autowired
private ObjectMapper jacksonObjectMapper;

@GetMapping("/version")
@ResponseBody
public Map<String, String> getVersion() {
Map<String, String> info = new HashMap<>();
info.put("database", mongoTemplate.getDb().getName());
return info;
}

@GetMapping
public ModelAndView list(Principal principal) {
Page<FileInformation> files = fileRepository.findAll(new PageRequest(0, 50, new Sort(new Sort.Order(Sort.Direction.DESC, "id"))));
Expand Down
1 change: 1 addition & 0 deletions ui/web/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}

This file was deleted.

12 changes: 12 additions & 0 deletions ui/web/src/test/java/gallerymine/test/ui/CucumberSetupTest.java
Original file line number Diff line number Diff line change
@@ -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 {

}
119 changes: 68 additions & 51 deletions ui/web/src/test/java/gallerymine/test/ui/SpringIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap> latestResponseMap = null;

static Map<String, Object> context = new HashMap<>();

@LocalServerPort
public int serverPort;

@Autowired
protected RestTemplate restTemplate;

void executeGet(String url) throws IOException {
final Map<String, String> 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<HashMap> lrm) {
latestResponseMap = lrm;
context.put("response", lrm);
}

void executePost() throws IOException {
final Map<String, String> 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<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json; charset=UTF-8");
HttpEntity<SourceCriteria> 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<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json; charset=UTF-8");
HttpEntity<SourceCriteria> 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> T resolveExpression(String expression) {
return resolveExpression(expression , null);
}

@Override
public void handleError(ClientHttpResponse response) throws IOException {
results = new ResponseResults(response);
public <T> T resolveExpression(String expression, Class<T> 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> T get(String key) {
return (T)context.get(key);
}

}
Loading

0 comments on commit 0faf6d0

Please sign in to comment.