-
Notifications
You must be signed in to change notification settings - Fork 8
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
Refactor and add ConversionConfig class as argument to createDcat #24
Open
jonasseglare
wants to merge
1
commit into
diggsweden:main
Choose a base branch
from
jonasseglare:custom-work-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−69
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
121 changes: 121 additions & 0 deletions
121
src/main/java/se/ams/dcatprocessor/ConversionConfig.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,121 @@ | ||
/* | ||
* This file is part of dcat-ap-se-processor. | ||
* | ||
* dcat-ap-se-processor is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* dcat-ap-se-processor is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with dcat-ap-se-processor. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package se.ams.dcatprocessor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.commons.collections4.MultiValuedMap; | ||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
||
public class ConversionConfig { | ||
private Path workDir; | ||
private MultiValuedMap<String, String> apiSpecMap; | ||
|
||
public static Path getDefaultWorkDir() { | ||
return Path.of(System.getProperty("user.dir")); | ||
} | ||
|
||
public boolean isValid() { | ||
return apiSpecMap != null && !apiSpecMap.isEmpty(); | ||
} | ||
|
||
private ConversionConfig(MultiValuedMap<String, String> apiSpecMap, Path workDir) { | ||
this.apiSpecMap = apiSpecMap; | ||
this.workDir = workDir; | ||
} | ||
|
||
private ConversionConfig copy() { | ||
return new ConversionConfig(apiSpecMap, workDir); | ||
} | ||
|
||
public ConversionConfig withWorkDir(Path workDir) { | ||
ConversionConfig dst = copy(); | ||
dst.workDir = workDir; | ||
return dst; | ||
} | ||
|
||
public MultiValuedMap<String, String> getApiSpecMap() { | ||
assert(isValid()); | ||
return apiSpecMap; | ||
} | ||
|
||
public static ConversionConfig fromApiSpecMap(MultiValuedMap<String, String> apiSpecMap) { | ||
return new ConversionConfig(apiSpecMap, getDefaultWorkDir()); | ||
} | ||
|
||
public static ConversionConfig fromKeyValue(String key, String value) { | ||
MultiValuedMap<String, String> apiSpecMap = new ArrayListValuedHashMap<>(); | ||
apiSpecMap.put(key, value); | ||
return ConversionConfig.fromApiSpecMap(apiSpecMap); | ||
} | ||
|
||
public static ConversionConfig fromFile(Path path) throws IOException { | ||
return ConversionConfig.fromKeyValue(path.toString(), Files.readString(path)); | ||
} | ||
|
||
public static ConversionConfig fromDirectory(Path dir) throws IOException { | ||
MultiValuedMap<String, String> apiSpecMap = new ArrayListValuedHashMap<>(); | ||
|
||
// returns pathnames for files and directory | ||
File[] files = dir.toFile().listFiles((dir1, name) -> name.endsWith(".raml") || name.endsWith(".yaml") || name.endsWith(".json")); | ||
|
||
// for each file in file array | ||
if (files != null && files.length > 0) { | ||
for (File file : files) { | ||
Path path = Path.of(String.valueOf(file)); | ||
try { | ||
String content = Files.readString(path); | ||
apiSpecMap.put(path.toString(), content); | ||
} finally { | ||
} | ||
} | ||
} | ||
return ConversionConfig.fromApiSpecMap(apiSpecMap); | ||
} | ||
|
||
public static ConversionConfig fromList(List<MultipartFile> apiFiles, List<Result> outResults) { | ||
MultiValuedMap<String, String> apiSpecMap = new ArrayListValuedHashMap<>(); | ||
for (MultipartFile apiFile : apiFiles) { | ||
if (!apiFile.isEmpty()) { | ||
String apiSpecificationFromFile; | ||
Scanner scanner; | ||
try { | ||
scanner = new Scanner(apiFile.getInputStream(), StandardCharsets.UTF_8.name()); | ||
apiSpecificationFromFile = scanner.useDelimiter("\\A").next(); | ||
scanner.close(); | ||
apiSpecMap.put(apiFile.getOriginalFilename(), apiSpecificationFromFile); | ||
} catch (Exception e) { //Catch and show processing errors in web-gui | ||
outResults.add(new Result(null, e.getMessage())); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return ConversionConfig.fromApiSpecMap(apiSpecMap); | ||
} | ||
|
||
public Path getWorkDir() { | ||
return workDir; | ||
} | ||
} |
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
95 changes: 95 additions & 0 deletions
95
src/test/java/se/ams/dcatprocessor/ConversionConfigTest.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,95 @@ | ||
/* | ||
* This file is part of dcat-ap-se-processor. | ||
* | ||
* dcat-ap-se-processor is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* dcat-ap-se-processor is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with dcat-ap-se-processor. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package se.ams.dcatprocessor; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.apache.commons.collections4.MultiValuedMap; | ||
import se.ams.dcatprocessor.rdf.DcatException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class ConversionConfigTest { | ||
|
||
@Test | ||
void testFromKeyValue() throws Exception { | ||
ConversionConfig cfg = ConversionConfig.fromKeyValue("k119", "the spec goes here"); | ||
assertEquals(ConversionConfig.getDefaultWorkDir(), cfg.getWorkDir()); | ||
assertTrue(cfg.isValid()); | ||
MultiValuedMap m = cfg.getApiSpecMap(); | ||
assertEquals(1, m.size()); | ||
assertEquals(List.of("the spec goes here"), m.get("k119")); | ||
checkWorkDirChangeIsWorking(cfg); | ||
} | ||
|
||
@Test | ||
void testFromFileAndDirectory() throws Exception { | ||
ConversionConfig[] configsToTest = new ConversionConfig[]{ | ||
ConversionConfig.fromFile(Path.of("src/test/resources/apidef/json_oas/obl_rek_oas.json")), | ||
ConversionConfig.fromDirectory(Path.of("src/test/resources/apidef/json_oas")), | ||
}; | ||
for (ConversionConfig cfg: configsToTest) { | ||
checkJsonOasContents(cfg); | ||
checkWorkDirChangeIsWorking(cfg); | ||
} | ||
} | ||
|
||
@Test | ||
void testMultipartFile() throws Exception { | ||
String filename = "src/test/resources/apidef/json_oas/obl_rek_oas.json"; | ||
Path srcPath = Path.of(filename); | ||
byte[] bytes = Files.readAllBytes(srcPath); | ||
MockMultipartFile file = new MockMultipartFile(filename, filename, StandardCharsets.UTF_8.name(), bytes); | ||
|
||
ArrayList<Result> results = new ArrayList<Result>(); | ||
ConversionConfig cfg = ConversionConfig.fromList(List.of(file), results); | ||
checkJsonOasContents(cfg); | ||
|
||
// If no exceptions are thrown, then nothing is pushed to results. | ||
assertTrue(results.isEmpty()); | ||
|
||
checkWorkDirChangeIsWorking(cfg); | ||
} | ||
|
||
// Common checks for the unit tests | ||
private ConversionConfig checkWorkDirChangeIsWorking(ConversionConfig src) throws Exception { | ||
Path tempDir = Files.createTempDirectory("DCAT_workdir"); | ||
ConversionConfig dst = src.withWorkDir(tempDir); | ||
assertEquals(tempDir, dst.getWorkDir()); | ||
assertEquals(ConversionConfig.getDefaultWorkDir(), src.getWorkDir()); | ||
return dst; | ||
} | ||
|
||
private void checkJsonOasContents(ConversionConfig cfg) { | ||
assertTrue(cfg.isValid()); | ||
MultiValuedMap m = cfg.getApiSpecMap(); | ||
assertEquals(1, m.size()); | ||
Collection<String> coll = m.get("src/test/resources/apidef/json_oas/obl_rek_oas.json"); | ||
assertEquals(1, coll.size()); | ||
String first = coll.iterator().next(); | ||
assertTrue(first.startsWith("{")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the description of the PR, I mentioned a bug that I fixed: In case we enter the
catch
branch above, then the same exception message is going to be pushed a second time toresults
because of this line. That is probably not what we want.The updated code should address that issue.