Skip to content

Commit 20465f2

Browse files
authored
feat: plugins to use jackson (#21062)
Update plugins to use jackson for json handling. part of #20741
1 parent 31492a5 commit 20465f2

File tree

11 files changed

+195
-188
lines changed

11 files changed

+195
-188
lines changed

flow-plugins/flow-gradle-plugin/src/functionalTest/kotlin/com/vaadin/gradle/MiscMultiModuleTest.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.vaadin.gradle
1818

19+
import com.vaadin.flow.internal.JacksonUtils
1920
import com.vaadin.flow.internal.StringUtil
2021
import com.vaadin.flow.server.InitParameters
21-
import elemental.json.Json
2222
import org.gradle.testkit.runner.BuildResult
2323
import org.junit.Test
2424
import java.io.File
@@ -120,10 +120,10 @@ class MiscMultiModuleTest : AbstractGradleTest() {
120120
expect(null) { b.task(":vaadinBuildFrontend") }
121121

122122
val tokenFile = File(testProject.dir, "web/build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
123-
val tokenFileContent = Json.parse(tokenFile.readText())
123+
val tokenFileContent = JacksonUtils.readTree(tokenFile.readText())
124124
expect("app-" + StringUtil.getHash("web",
125125
java.nio.charset.StandardCharsets.UTF_8
126-
)) { tokenFileContent.getString(InitParameters.APPLICATION_IDENTIFIER) }
126+
)) { tokenFileContent.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
127127
}
128128

129129
@Test
@@ -174,10 +174,10 @@ class MiscMultiModuleTest : AbstractGradleTest() {
174174
expect(null) { b.task(":vaadinBuildFrontend") }
175175

176176
val tokenFile = File(testProject.dir, "web/build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
177-
val tokenFileContent = Json.parse(tokenFile.readText())
177+
val tokenFileContent = JacksonUtils.readTree(tokenFile.readText())
178178
expect("app-" + StringUtil.getHash("MY_APP_ID",
179179
java.nio.charset.StandardCharsets.UTF_8
180-
)) { tokenFileContent.getString(InitParameters.APPLICATION_IDENTIFIER) }
180+
)) { tokenFileContent.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
181181
}
182182

183183
@Test
@@ -226,8 +226,8 @@ class MiscMultiModuleTest : AbstractGradleTest() {
226226
expect(null) { b.task(":vaadinBuildFrontend") }
227227

228228
val tokenFile = File(testProject.dir, "web/build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
229-
val tokenFileContent = Json.parse(tokenFile.readText())
230-
expect("MY_APP_ID") { tokenFileContent.getString(InitParameters.APPLICATION_IDENTIFIER) }
229+
val tokenFileContent = JacksonUtils.readTree(tokenFile.readText())
230+
expect("MY_APP_ID") { tokenFileContent.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
231231
}
232232

233233
@Test

flow-plugins/flow-gradle-plugin/src/functionalTest/kotlin/com/vaadin/gradle/MiscSingleModuleTest.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.vaadin.gradle
1818

19+
import com.vaadin.flow.internal.JacksonUtils
1920
import com.vaadin.flow.internal.StringUtil
2021
import com.vaadin.flow.server.InitParameters
21-
import elemental.json.Json
2222
import org.gradle.testkit.runner.BuildResult
2323
import org.gradle.testkit.runner.TaskOutcome
2424
import org.junit.Test
@@ -70,21 +70,21 @@ class MiscSingleModuleTest : AbstractGradleTest() {
7070
fun testWarProjectProductionMode() {
7171
doTestWarProjectProductionMode()
7272
val tokenFile = File(testProject.dir, "build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
73-
val tokenFileContent = Json.parse(tokenFile.readText())
73+
val tokenFileContent = JacksonUtils.readTree(tokenFile.readText())
7474
expect("app-" + StringUtil.getHash(testProject.dir.name,
7575
java.nio.charset.StandardCharsets.UTF_8
76-
)) { tokenFileContent.getString(InitParameters.APPLICATION_IDENTIFIER) }
76+
)) { tokenFileContent.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
7777
}
7878

7979
@Test
8080
fun testWarProjectProductionModeWithCustomName() {
8181
testProject.settingsFile.writeText("rootProject.name = 'my-test-project'")
8282
doTestWarProjectProductionMode()
8383
val tokenFile = File(testProject.dir, "build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
84-
val tokenFileContent = Json.parse(tokenFile.readText())
84+
val tokenFileContent = JacksonUtils.readTree(tokenFile.readText())
8585
expect("app-" + StringUtil.getHash("my-test-project",
8686
java.nio.charset.StandardCharsets.UTF_8
87-
)) { tokenFileContent.getString(InitParameters.APPLICATION_IDENTIFIER) }
87+
)) { tokenFileContent.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
8888
}
8989

9090
/**

flow-plugins/flow-gradle-plugin/src/functionalTest/kotlin/com/vaadin/gradle/VaadinSmokeTest.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package com.vaadin.gradle
1717

18+
import com.fasterxml.jackson.databind.JsonNode
19+
import com.vaadin.flow.internal.JacksonUtils
1820
import java.io.File
1921
import kotlin.test.assertContains
2022
import kotlin.test.expect
2123
import com.vaadin.flow.internal.StringUtil
2224
import com.vaadin.flow.server.InitParameters
2325
import com.vaadin.flow.server.frontend.FrontendUtils
24-
import elemental.json.JsonObject
25-
import elemental.json.impl.JsonUtil
2626
import org.gradle.testkit.runner.BuildResult
2727
import org.gradle.testkit.runner.TaskOutcome
2828
import org.junit.Before
@@ -71,8 +71,8 @@ class VaadinSmokeTest : AbstractGradleTest() {
7171

7272
val tokenFile = File(testProject.dir, "build/vaadin-generated/META-INF/VAADIN/config/flow-build-info.json")
7373
expect(true, tokenFile.toString()) { tokenFile.isFile }
74-
val buildInfo: JsonObject = JsonUtil.parse(tokenFile.readText())
75-
expect(false, buildInfo.toJson()) { buildInfo.getBoolean(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE) }
74+
val buildInfo: JsonNode = JacksonUtils.readTree(tokenFile.readText())
75+
expect(false, buildInfo.toString()) { buildInfo.get(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE).booleanValue() }
7676
}
7777

7878
@Test
@@ -100,11 +100,11 @@ class VaadinSmokeTest : AbstractGradleTest() {
100100
build.find("*.br", 4..10)
101101
build.find("*.js", 4..10)
102102
val tokenFile = File(testProject.dir, "build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
103-
val buildInfo: JsonObject = JsonUtil.parse(tokenFile.readText())
104-
expect(true, buildInfo.toJson()) { buildInfo.getBoolean(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE) }
103+
val buildInfo: JsonNode = JacksonUtils.readTree(tokenFile.readText())
104+
expect(true, buildInfo.toString()) { buildInfo.get(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE).booleanValue() }
105105
expect("app-" + StringUtil.getHash(testProject.dir.name,
106106
java.nio.charset.StandardCharsets.UTF_8
107-
), buildInfo.toJson()) { buildInfo.getString(InitParameters.APPLICATION_IDENTIFIER) }
107+
), buildInfo.toString()) { buildInfo.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
108108
}
109109

110110
@Test
@@ -115,8 +115,8 @@ class VaadinSmokeTest : AbstractGradleTest() {
115115
result.expectTaskSucceded("vaadinPrepareFrontend")
116116

117117
val tokenFile = File(testProject.dir, "build/resources/main/META-INF/VAADIN/config/flow-build-info.json")
118-
val buildInfo: JsonObject = JsonUtil.parse(tokenFile.readText())
119-
expect("MY_APP_ID", buildInfo.toJson()) { buildInfo.getString(InitParameters.APPLICATION_IDENTIFIER) }
118+
val buildInfo: JsonNode = JacksonUtils.readTree(tokenFile.readText())
119+
expect("MY_APP_ID", buildInfo.toString()) { buildInfo.get(InitParameters.APPLICATION_IDENTIFIER).textValue() }
120120
}
121121

122122
@Test

flow-plugins/flow-maven-plugin/src/test/java/com/vaadin/flow/plugin/TestUtils.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import java.util.List;
2727
import java.util.stream.Collectors;
2828

29-
import elemental.json.Json;
30-
import elemental.json.JsonObject;
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.fasterxml.jackson.databind.node.ObjectNode;
31+
32+
import com.vaadin.flow.internal.JacksonUtils;
3133

3234
/**
3335
* Shared code to use in the unit tests.
@@ -112,21 +114,21 @@ public static List<String> listFilesRecursively(File directory) {
112114
}
113115
}
114116

115-
public static JsonObject getInitialPackageJson() {
116-
JsonObject packageJson = Json.createObject();
117-
JsonObject vaadinPackages = Json.createObject();
117+
public static ObjectNode getInitialPackageJson() {
118+
ObjectNode packageJson = JacksonUtils.createObjectNode();
119+
ObjectNode vaadinPackages = JacksonUtils.createObjectNode();
118120

119-
vaadinPackages.put("dependencies", Json.createObject());
120-
JsonObject defaults = vaadinPackages.getObject("dependencies");
121+
vaadinPackages.set("dependencies", JacksonUtils.createObjectNode());
122+
ObjectNode defaults = (ObjectNode) vaadinPackages.get("dependencies");
121123
defaults.put("@polymer/polymer", "3.2.0");
122124
defaults.put("@webcomponents/webcomponentsjs", "^2.2.10");
123125

124-
vaadinPackages.put("devDependencies", Json.createObject());
125-
defaults = vaadinPackages.getObject("devDependencies");
126+
vaadinPackages.set("devDependencies", JacksonUtils.createObjectNode());
127+
defaults = (ObjectNode) vaadinPackages.get("devDependencies");
126128
defaults.put("vite", "3.4.5");
127129

128130
vaadinPackages.put("hash", "");
129-
packageJson.put("vaadin", vaadinPackages);
131+
packageJson.set("vaadin", vaadinPackages);
130132

131133
return packageJson;
132134
}

flow-plugins/flow-maven-plugin/src/test/java/com/vaadin/flow/plugin/maven/BuildFrontendMojoTest.java

+30-30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.stream.Collectors;
3737
import java.util.stream.Stream;
3838

39+
import com.fasterxml.jackson.databind.JsonNode;
40+
import com.fasterxml.jackson.databind.node.ObjectNode;
3941
import org.apache.maven.artifact.DefaultArtifact;
4042
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
4143
import org.apache.maven.model.Build;
@@ -60,6 +62,7 @@
6062
import org.mockito.Mockito;
6163

6264
import com.vaadin.flow.di.Lookup;
65+
import com.vaadin.flow.internal.JacksonUtils;
6366
import com.vaadin.flow.internal.StringUtil;
6467
import com.vaadin.flow.plugin.TestUtils;
6568
import com.vaadin.flow.server.Constants;
@@ -70,10 +73,6 @@
7073
import com.vaadin.flow.server.frontend.installer.NodeInstaller;
7174
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
7275

73-
import elemental.json.Json;
74-
import elemental.json.JsonObject;
75-
import elemental.json.impl.JsonUtil;
76-
7776
import static com.vaadin.flow.server.Constants.PACKAGE_JSON;
7877
import static com.vaadin.flow.server.Constants.TARGET;
7978
import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
@@ -203,7 +202,7 @@ public void setup() throws Exception {
203202
// need to run `npm install`
204203
createExpectedImports(frontendDirectory, nodeModulesPath);
205204
FileUtils.fileWrite(packageJson, "UTF-8",
206-
TestUtils.getInitialPackageJson().toJson());
205+
TestUtils.getInitialPackageJson().toString());
207206

208207
lookup = Mockito.mock(Lookup.class);
209208
Mockito.doReturn(new TestEndpointGeneratorTaskFactory()).when(lookup)
@@ -459,27 +458,27 @@ public void should_AddRemove_Imports() throws Exception {
459458
@Test
460459
public void mavenGoalWhenPackageJsonContainsDependencies_onlyFrameworkHandledDependencyIsTouched()
461460
throws Exception {
462-
JsonObject json = TestUtils.getInitialPackageJson();
463-
JsonObject dependencies = Json.createObject();
461+
ObjectNode json = TestUtils.getInitialPackageJson();
462+
ObjectNode dependencies = JacksonUtils.createObjectNode();
464463
dependencies.put("proj4", "2.9.0");
465464
dependencies.put("line-awesome", "1.3.0");
466465
// Make proj4 framework handled
467-
json.getObject("vaadin").getObject("dependencies").put("proj4",
466+
((ObjectNode) json.get("vaadin").get("dependencies")).put("proj4",
468467
"2.9.0");
469-
json.put("dependencies", dependencies);
470-
FileUtils.fileWrite(packageJson, "UTF-8", json.toJson());
468+
json.set("dependencies", dependencies);
469+
FileUtils.fileWrite(packageJson, "UTF-8", json.toString());
471470

472471
mojo.execute();
473-
JsonObject packageJsonObject = getPackageJson(packageJson);
474-
dependencies = packageJsonObject.getObject("dependencies");
472+
ObjectNode packageObjectNode = getPackageJson(packageJson);
473+
dependencies = (ObjectNode) packageObjectNode.get("dependencies");
475474

476475
assertContainsPackage(dependencies, "@vaadin/button",
477476
"@vaadin/vaadin-element-mixin");
478477

479478
Assert.assertFalse("proj4 should have been removed",
480-
dependencies.hasKey("proj4"));
479+
dependencies.has("proj4"));
481480
Assert.assertTrue("line-awesome should remain",
482-
dependencies.hasKey("line-awesome"));
481+
dependencies.has("line-awesome"));
483482
}
484483

485484
@Test
@@ -498,7 +497,7 @@ public void existingTokenFile_parametersShouldBeRemoved()
498497
ReflectionUtils.setVariableValueInObject(mojo,
499498
"resourceOutputDirectory", resourceOutputDirectory);
500499

501-
JsonObject initialBuildInfo = Json.createObject();
500+
ObjectNode initialBuildInfo = JacksonUtils.createObjectNode();
502501
initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false);
503502
initialBuildInfo.put(Constants.NPM_TOKEN, "npm");
504503
initialBuildInfo.put(Constants.FRONTEND_TOKEN, "src/main/frontend");
@@ -512,13 +511,13 @@ public void existingTokenFile_parametersShouldBeRemoved()
512511

513512
org.apache.commons.io.FileUtils.forceMkdir(tokenFile.getParentFile());
514513
org.apache.commons.io.FileUtils.write(tokenFile,
515-
JsonUtil.stringify(initialBuildInfo, 2) + "\n", "UTF-8");
514+
initialBuildInfo.toPrettyString() + "\n", "UTF-8");
516515

517516
mojo.execute();
518517

519518
String json = org.apache.commons.io.FileUtils
520519
.readFileToString(tokenFile, "UTF-8");
521-
JsonObject buildInfo = JsonUtil.parse(json);
520+
ObjectNode buildInfo = JacksonUtils.readTree(json);
522521
Assert.assertNull(
523522
"enable dev server token shouldn't be added " + "automatically",
524523
buildInfo.get(FRONTEND_HOTDEPLOY));
@@ -552,7 +551,7 @@ public void existingTokenFile_defaultApplicationIdentifierWritten()
552551
String expectedAppId = "app-" + StringUtil.getHash(
553552
"com.vaadin.testing:my-application", StandardCharsets.UTF_8);
554553

555-
JsonObject initialBuildInfo = Json.createObject();
554+
ObjectNode initialBuildInfo = JacksonUtils.createObjectNode();
556555
initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false);
557556
initialBuildInfo.put(Constants.NPM_TOKEN, "npm");
558557
initialBuildInfo.put(Constants.FRONTEND_TOKEN, "src/main/frontend");
@@ -566,17 +565,18 @@ public void existingTokenFile_defaultApplicationIdentifierWritten()
566565

567566
org.apache.commons.io.FileUtils.forceMkdir(tokenFile.getParentFile());
568567
org.apache.commons.io.FileUtils.write(tokenFile,
569-
JsonUtil.stringify(initialBuildInfo, 2) + "\n", "UTF-8");
568+
initialBuildInfo.toPrettyString() + "\n", "UTF-8");
570569

571570
mojo.execute();
572571
Assert.assertTrue("No token file could be found", tokenFile.exists());
573572

574573
String json = org.apache.commons.io.FileUtils
575574
.readFileToString(tokenFile, "UTF-8");
576-
JsonObject buildInfo = JsonUtil.parse(json);
575+
ObjectNode buildInfo = JacksonUtils.readTree(json);
577576
Assert.assertEquals(
578577
"Custom application identifier not written on token file",
579-
expectedAppId, buildInfo.getString(APPLICATION_IDENTIFIER));
578+
expectedAppId,
579+
buildInfo.get(APPLICATION_IDENTIFIER).textValue());
580580
}
581581

582582
@Test
@@ -587,7 +587,7 @@ public void existingTokenFile_customApplicationIdentifierWritten()
587587
ReflectionUtils.setVariableValueInObject(mojo, "applicationIdentifier",
588588
appId);
589589

590-
JsonObject initialBuildInfo = Json.createObject();
590+
ObjectNode initialBuildInfo = JacksonUtils.createObjectNode();
591591
initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false);
592592
initialBuildInfo.put(Constants.NPM_TOKEN, "npm");
593593
initialBuildInfo.put(Constants.FRONTEND_TOKEN, "src/main/frontend");
@@ -601,17 +601,17 @@ public void existingTokenFile_customApplicationIdentifierWritten()
601601

602602
org.apache.commons.io.FileUtils.forceMkdir(tokenFile.getParentFile());
603603
org.apache.commons.io.FileUtils.write(tokenFile,
604-
JsonUtil.stringify(initialBuildInfo, 2) + "\n", "UTF-8");
604+
initialBuildInfo.toPrettyString() + "\n", "UTF-8");
605605

606606
mojo.execute();
607607
Assert.assertTrue("No token file could be found", tokenFile.exists());
608608

609609
String json = org.apache.commons.io.FileUtils
610610
.readFileToString(tokenFile, "UTF-8");
611-
JsonObject buildInfo = JsonUtil.parse(json);
611+
ObjectNode buildInfo = JacksonUtils.readTree(json);
612612
Assert.assertEquals(
613613
"Custom application identifier not written on token file",
614-
appId, buildInfo.getString(APPLICATION_IDENTIFIER));
614+
appId, buildInfo.get(APPLICATION_IDENTIFIER).textValue());
615615
}
616616

617617
@Test
@@ -677,10 +677,10 @@ public void mavenGoal_generateTsFiles_when_enabled() throws Exception {
677677
Assert.assertTrue(endpointClientApi.exists());
678678
}
679679

680-
static void assertContainsPackage(JsonObject dependencies,
680+
static void assertContainsPackage(JsonNode dependencies,
681681
String... packages) {
682682
Arrays.asList(packages).forEach(dep -> Assert
683-
.assertTrue("Missing " + dep, dependencies.hasKey(dep)));
683+
.assertTrue("Missing " + dep, dependencies.has(dep)));
684684
}
685685

686686
private void assertContainsImports(boolean contains, String... imports)
@@ -784,12 +784,12 @@ static void sleep(int ms) throws InterruptedException {
784784
Thread.sleep(ms); // NOSONAR
785785
}
786786

787-
static JsonObject getPackageJson(String packageJson) throws IOException {
787+
static ObjectNode getPackageJson(String packageJson) throws IOException {
788788
if (FileUtils.fileExists(packageJson)) {
789-
return Json.parse(FileUtils.fileRead(packageJson));
789+
return JacksonUtils.readTree(FileUtils.fileRead(packageJson));
790790

791791
} else {
792-
return Json.createObject();
792+
return JacksonUtils.createObjectNode();
793793
}
794794
}
795795

0 commit comments

Comments
 (0)