-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
from_version-5/fix_issue_769 - fix for settings import error along wi…
…th auto-text substitution of projectIds in pasted project settings JSON from_version-5/fix_issue_769-HG-5463 - remove Slack webhook URL from unit test settings.json
- Loading branch information
1 parent
2500943
commit 0577f86
Showing
9 changed files
with
719 additions
and
17 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
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
20 changes: 20 additions & 0 deletions
20
webprotege-server-core/src/main/java/edu/stanford/bmir/protege/web/server/util/JavaUtil.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,20 @@ | ||
package edu.stanford.bmir.protege.web.server.util; | ||
|
||
public class JavaUtil { | ||
/** | ||
* The purpose of this cast utility to be able to suppress unchecked | ||
* cast in one and only one place so we don't have to pollute the main | ||
* application code with @SuppressWarnings and thus be able to flag | ||
* places where the type cast warning(s) are/were unexpected. | ||
* @param <T> | ||
* @param obj | ||
* @return | ||
* | ||
* This great idea came from: | ||
* @see http://www.whizu.org/articles/how-to-avoid-unchecked-cast-warnings-with-java-generics.whizu | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T cast(Object obj) { | ||
return (T)obj; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...re/src/main/java/edu/stanford/bmir/protege/web/server/util/TypelessJSONSerialization.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,94 @@ | ||
package edu.stanford.bmir.protege.web.server.util; | ||
|
||
import static edu.stanford.bmir.protege.web.server.util.JavaUtil.cast; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
|
||
/** | ||
* These utility methods are for manipulating arbitrary JSON without needing to worry about what | ||
* typed entities the JSON should be de/serialized to/from. | ||
* | ||
* Initial motivation is to be able to change the value of "projectId" in project settings and | ||
* project forms import JSON. | ||
* | ||
* @author Chris Wolf <[email protected]> | ||
*/ | ||
public class TypelessJSONSerialization { | ||
|
||
// TODO: should probably be injected, but effects the whole upstream DI call chain... | ||
// TODO: ...and plus, ObjectMapper very expensive, so should be created only once. | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public TypelessJSONSerialization() { | ||
} | ||
|
||
/** | ||
* Accepts any arbitrary JSON and deserializes to nested <code>LinkedHashMap<String, Object></code> | ||
* instances. Note that the <code>Object</code> type parameter could be of type <code>String</code>, | ||
* <code>List</code>, or <code>Map</code> where the latter is for arbitrary nesting levels. | ||
* | ||
* <b>N.B.</b> For normal, strongly typed Webprotege serialization, use the <code>ObjectMapper</code> | ||
* obtained from <code>ObjectMapperProvider</code> from <i>webprotege-server-core</i>. | ||
* | ||
* @param json | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static Map<String, Object> deserializeJSON(@Nonnull String json) throws IOException { | ||
ObjectReader objectReader = objectMapper.readerFor(new TypeReference<Map<String, Object>>() { | ||
}); | ||
return objectReader.readValue(json); | ||
} | ||
|
||
/** | ||
* | ||
* @param object | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String serializeToJSON(Map<String, Object> object) throws IOException { | ||
return serializeToJSON(object, false); | ||
} | ||
|
||
public static String serializeToJSON(Map<String, Object> object, boolean prettyPrint) throws IOException { | ||
ObjectWriter objectWriter = prettyPrint ? objectMapper.writerWithDefaultPrettyPrinter() : objectMapper.writer(); | ||
return objectWriter.writeValueAsString(object); | ||
} | ||
|
||
public static String resplaceAllStringValue(@Nonnull String json, @Nonnull String keyName, | ||
@Nonnull String replacementValue) throws IOException { | ||
|
||
Map<String, Object> deserialized = deserializeJSON(json); | ||
|
||
walkMapAndDReplace(deserialized, keyName, replacementValue); | ||
|
||
String serialized = serializeToJSON(deserialized); | ||
|
||
return serialized; | ||
} | ||
|
||
static void walkMapAndDReplace(Map<String, Object> data, String keyName, String replacementValue) { | ||
for (var mapEntry : data.entrySet()) { | ||
if (mapEntry.getValue() instanceof String && mapEntry.getKey().equals(keyName)) { | ||
mapEntry.setValue(replacementValue); | ||
} else if (mapEntry.getValue() instanceof List) { | ||
List<Object> objlist = cast(mapEntry.getValue()); | ||
for (Object listEntry : objlist) { | ||
if (listEntry instanceof Map) | ||
walkMapAndDReplace(cast(listEntry), keyName, replacementValue); | ||
} | ||
} else if (mapEntry.getValue() instanceof Map) { | ||
walkMapAndDReplace(cast(mapEntry.getValue()), keyName, replacementValue); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
webprotege-server-core/src/test/java/edu/stanford/bmir/protege/web/TestUtils.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,32 @@ | ||
package edu.stanford.bmir.protege.web; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class TestUtils { | ||
/** | ||
* Read contents of text file resource from <code>src/test/resources</code>. | ||
* Entire content read into memory, so best for small files. | ||
* | ||
* @param resourceName | ||
* @return text content and assuming UTF-8 encoding | ||
* @throws IOException | ||
*/ | ||
public static String readResourceTestFile(String resourceName) throws IOException { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
URI resource; | ||
try { | ||
resource = classLoader.getResource(resourceName).toURI(); | ||
} catch (URISyntaxException e) { | ||
String msg = String.format("Can't read '%s'", resourceName); | ||
throw new IOException(msg, e); | ||
} | ||
byte[] rawContent = Files.readAllBytes(Paths.get(resource)); | ||
|
||
return new String(rawContent, StandardCharsets.UTF_8); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...st/java/edu/stanford/bmir/protege/web/server/util/TypelessJSONSerialization_TestCase.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,56 @@ | ||
package edu.stanford.bmir.protege.web.server.util; | ||
|
||
import static edu.stanford.bmir.protege.web.server.util.JavaUtil.cast; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner.Silent; | ||
|
||
import edu.stanford.bmir.protege.web.TestUtils; | ||
import edu.stanford.bmir.protege.web.shared.project.ProjectId; | ||
|
||
@RunWith(Silent.class) | ||
public class TypelessJSONSerialization_TestCase { | ||
private ProjectId projectId = ProjectId.get("c4e39f8f-d2b3-4212-8888-28fabd2aa5ac"); | ||
|
||
@Test | ||
public void testJSONSerialization() throws Exception { | ||
// test basic round-trip deserialize/serialize | ||
|
||
String testSettings = TestUtils | ||
.readResourceTestFile("edu/stanford/bmir/protege/web/server/util/project_settings_notags.json"); | ||
// out.println(testSettings); | ||
Map<String, Object> data = TypelessJSONSerialization.deserializeJSON(testSettings); | ||
|
||
String newSettings = TypelessJSONSerialization.serializeToJSON(data, true); | ||
|
||
assertEquals(StringUtils.deleteWhitespace(testSettings), StringUtils.deleteWhitespace(newSettings)); | ||
// out.println(newSettings); | ||
} | ||
|
||
@Test | ||
public void testJSONSerializationNameValueReplacement() throws Exception { | ||
String testSettings = TestUtils | ||
.readResourceTestFile("edu/stanford/bmir/protege/web/server/util/project_settings_notags.json"); | ||
|
||
String newSettings = TypelessJSONSerialization.resplaceAllStringValue(testSettings, "projectId", | ||
projectId.getId()); | ||
Map<String, Object> data = TypelessJSONSerialization.deserializeJSON(newSettings); | ||
|
||
String id = projectId.getId(); | ||
Map<String, Object> projectSettings = cast(data.get("projectSettings")); | ||
Map<String, Object> sharingSettings = cast(data.get("sharingSettings")); | ||
Map<String, Object> searchSettings = cast(data.get("searchSettings")); | ||
List<Map<String, Object>> searchFilters = cast(searchSettings.get("searchFilters")); | ||
|
||
assertEquals(id, projectSettings.get("projectId")); | ||
assertEquals(id, sharingSettings.get("projectId")); | ||
assertEquals(id, searchSettings.get("projectId")); | ||
assertEquals(id, searchFilters.get(0).get("projectId")); | ||
} | ||
} |
Oops, something went wrong.