forked from concretesolutions/mock-api
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permite variáveis nos nomes das pastas (#27)
* Suporte a variáveis no path de arquivos * Renames e testes
- Loading branch information
1 parent
4240ad6
commit 3078551
Showing
9 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/br/com/elementalsource/mock/generic/repository/impl/ExistingFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package br.com.elementalsource.mock.generic.repository.impl; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
public class ExistingFile implements Comparable<ExistingFile> { | ||
|
||
private final Pattern regex; | ||
private final String originalPath; | ||
|
||
ExistingFile(String path) { | ||
originalPath = path; | ||
regex = Pattern.compile(path.replaceAll("#\\{\\{\\w*\\}\\}", "(.*)")); | ||
} | ||
|
||
public PathParamExtractor extract(String rawPath) { | ||
return new PathParamExtractor(rawPath); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("regex", regex) | ||
.add("originalPath", originalPath) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public int compareTo(final ExistingFile o) { | ||
return this.originalPath.compareTo(o.originalPath) * -1; | ||
} | ||
|
||
public class PathParamExtractor { | ||
|
||
private boolean matches; | ||
private Map<String, String> parameters = new HashMap<>(); | ||
|
||
public PathParamExtractor(String rawPath ) { | ||
Matcher rawPathMatcher = regex.matcher(rawPath); | ||
Matcher originalPathMatcher = regex.matcher(originalPath); | ||
|
||
matches = rawPathMatcher.matches() && originalPathMatcher.matches(); | ||
|
||
if(matches){ | ||
for (int i = 1; i <= rawPathMatcher.groupCount(); i++){ | ||
parameters.put(originalPathMatcher.group(i).replaceAll("\\W", ""),rawPathMatcher.group(i)); | ||
} | ||
|
||
} | ||
} | ||
|
||
public boolean matches() { | ||
return matches; | ||
} | ||
|
||
public Map<String, String> getParameters() { | ||
return parameters; | ||
} | ||
|
||
public String getOriginalPath(){ | ||
return originalPath; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/br/com/elementalsource/mock/generic/repository/impl/ExistingFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package br.com.elementalsource.mock.generic.repository.impl; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.annotation.ApplicationScope; | ||
|
||
import br.com.elementalsource.mock.infra.property.FileProperty; | ||
|
||
@Component | ||
@ApplicationScope | ||
public class ExistingFiles { | ||
@Autowired @Qualifier("FilePropertyModel") | ||
private FileProperty fileProperty; | ||
private List<ExistingFile> existingFiles; | ||
|
||
@PostConstruct | ||
public void setup() throws IOException { | ||
String fileBase = fileProperty.getFileBase(); | ||
existingFiles = Files.walk(Paths.get(fileBase)) | ||
.filter(Files::isRegularFile) | ||
.map(Path::getParent) | ||
.map(Path::toString) | ||
.map(ExistingFile::new) | ||
.sorted() | ||
.distinct() | ||
.collect(toList()); | ||
} | ||
|
||
public List<ExistingFile> getExistingFiles(){ | ||
return existingFiles; | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
src/main/java/br/com/elementalsource/mock/infra/component/file/BaseFileNameBuilderBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 27 additions & 1 deletion
28
src/main/java/br/com/elementalsource/mock/infra/component/file/BaseFileNameBuilderModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
package br.com.elementalsource.mock.infra.component.file; | ||
|
||
import br.com.elementalsource.mock.generic.repository.impl.ExistingFile; | ||
import br.com.elementalsource.mock.generic.repository.impl.ExistingFile.PathParamExtractor; | ||
import br.com.elementalsource.mock.generic.repository.impl.ExistingFiles; | ||
import br.com.elementalsource.mock.infra.property.FileProperty; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@Component("BaseFileNameBuilderModel") | ||
public class BaseFileNameBuilderModel extends BaseFileNameBuilderBase implements BaseFileNameBuilder { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BaseFileNameBuilderModel.class); | ||
private ExistingFiles existingFiles; | ||
private HttpServletRequest request; | ||
|
||
@Autowired | ||
public BaseFileNameBuilderModel(@Qualifier("FilePropertyModel") FileProperty fileProperty) { | ||
public BaseFileNameBuilderModel(@Qualifier("FilePropertyModel") FileProperty fileProperty, ExistingFiles existingFiles, HttpServletRequest request) { | ||
super(fileProperty); | ||
this.existingFiles = existingFiles; | ||
this.request = request; | ||
final String fileBase = fileProperty.getFileBase(); | ||
final File file = new File(fileBase); | ||
LOGGER.info("Base path to files fileBase={}, exists?{}, path={}", fileBase, file.exists(), file.getAbsoluteFile()); | ||
} | ||
|
||
@Override | ||
public String buildPath(RequestMethod requestMethod, String pathUri) { | ||
List<ExistingFile> files = existingFiles.getExistingFiles(); | ||
String rawPath = super.buildPath(requestMethod, pathUri); | ||
|
||
return files.stream() | ||
.map(ef -> ef.extract(rawPath)) | ||
.filter(PathParamExtractor::matches) | ||
.peek(pe -> { | ||
pe.getParameters().forEach(request::setAttribute); | ||
}) | ||
.map(pe -> pe.getOriginalPath()) | ||
.findFirst() | ||
.orElse(rawPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/br/com/elementalsource/mock/generic/repository/impl/ExistingFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package br.com.elementalsource.mock.generic.repository.impl; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class ExistingFileTest { | ||
|
||
@Test | ||
public void shouldMatchesSamePath(){ | ||
String path = "/some/path"; | ||
ExistingFile existingFile = new ExistingFile(path); | ||
|
||
ExistingFile.PathParamExtractor paramExtractor = existingFile.extract(path); | ||
|
||
assertTrue(paramExtractor.matches()); | ||
assertEquals(0, paramExtractor.getParameters().size()); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldMatchesAPathWithAVariable(){ | ||
String filePath = "/some/#{{variable}}/path"; | ||
String urlPath = "/some/value/path"; | ||
|
||
ExistingFile existingFile = new ExistingFile(filePath); | ||
|
||
ExistingFile.PathParamExtractor paramExtractor = existingFile.extract(urlPath); | ||
|
||
assertTrue(paramExtractor.matches()); | ||
assertEquals(1, paramExtractor.getParameters().size()); | ||
assertEquals("value", paramExtractor.getParameters().get("variable")); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldMatchesAPathWithMultipleVariables(){ | ||
String filePath = "/some/#{{variable1}}/path/#{{variable2}}"; | ||
String urlPath = "/some/value/path/another"; | ||
|
||
ExistingFile existingFile = new ExistingFile(filePath); | ||
|
||
ExistingFile.PathParamExtractor paramExtractor = existingFile.extract(urlPath); | ||
|
||
assertTrue(paramExtractor.matches()); | ||
assertEquals(2, paramExtractor.getParameters().size()); | ||
assertEquals("value", paramExtractor.getParameters().get("variable1")); | ||
assertEquals("another", paramExtractor.getParameters().get("variable2")); | ||
|
||
} | ||
|
||
} |