Skip to content

Commit

Permalink
test: add tests for handling separate versions in NodeUpdater (#14079)
Browse files Browse the repository at this point in the history
Tests are added for changes done in PR
#14014

(cherry picked from commit 7140d32)
  • Loading branch information
taefi committed Jun 28, 2022
1 parent abb34bf commit 913e166
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import com.vaadin.flow.server.frontend.scanner.FrontendDependencies;

Expand Down Expand Up @@ -313,6 +317,96 @@ public void generateVersionsJson_versionsGeneratedFromPackageJson_containsBothDe
versionsJson.toJson());
}

@Test
public void testGetPlatformPinnedDependencies_vaadinCoreVersionIsNotPresent_outputIsEmptyJson()
throws IOException {
Logger logger = Mockito.spy(Logger.class);
try (MockedStatic<LoggerFactory> loggerFactoryMocked = Mockito
.mockStatic(LoggerFactory.class)) {
loggerFactoryMocked
.when(() -> LoggerFactory.getLogger(nodeUpdater.getClass()))
.thenReturn(logger);

Mockito.when(
finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(null);
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(null);

JsonObject pinnedVersions = nodeUpdater
.getPlatformPinnedDependencies();
Assert.assertEquals(0, pinnedVersions.keys().length);

Mockito.verify(logger, Mockito.times(1)).info(
"Couldn't find {} file to pin dependency versions for core components."
+ " Transitive dependencies won't be pinned for npm/pnpm.",
Constants.VAADIN_CORE_VERSIONS_JSON);
}
}

@Test
public void testGetPlatformPinnedDependencies_onlyVaadinCoreVersionIsPresent_outputContainsOnlyCoreVersions()
throws IOException {
File coreVersionsFile = File.createTempFile("vaadin-core-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinCoreJson = getMockVaadinCoreVersionsJson();
Assert.assertTrue(mockedVaadinCoreJson.hasKey("core"));
Assert.assertTrue(
mockedVaadinCoreJson.getObject("core").hasKey("button"));
Assert.assertFalse(mockedVaadinCoreJson.hasKey("vaadin"));

FileUtils.write(coreVersionsFile, mockedVaadinCoreJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(coreVersionsFile.toURI().toURL());
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(null);

JsonObject pinnedVersions = nodeUpdater.getPlatformPinnedDependencies();

Assert.assertTrue(pinnedVersions.hasKey("@vaadin/button"));
Assert.assertFalse(pinnedVersions.hasKey("@vaadin/grid-pro"));
Assert.assertFalse(pinnedVersions.hasKey("@vaadin/vaadin-grid-pro"));
}

@Test
public void testGetPlatformPinnedDependencies_VaadinAndVaadinCoreVersionsArePresent_outputContainsBothCoreAndCommercialVersions()
throws IOException {
File coreVersionsFile = File.createTempFile("vaadin-core-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinCoreJson = getMockVaadinCoreVersionsJson();
Assert.assertTrue(mockedVaadinCoreJson.hasKey("core"));
Assert.assertTrue(
mockedVaadinCoreJson.getObject("core").hasKey("button"));
Assert.assertFalse(mockedVaadinCoreJson.hasKey("vaadin"));

FileUtils.write(coreVersionsFile, mockedVaadinCoreJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(coreVersionsFile.toURI().toURL());

File vaadinVersionsFile = File.createTempFile("vaadin-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinJson = getMockVaadinVersionsJson();
Assert.assertFalse(mockedVaadinJson.hasKey("core"));
Assert.assertTrue(mockedVaadinJson.hasKey("vaadin"));
Assert.assertTrue(
mockedVaadinJson.getObject("vaadin").hasKey("grid-pro"));
Assert.assertTrue(
mockedVaadinJson.getObject("vaadin").hasKey("vaadin-grid-pro"));

FileUtils.write(vaadinVersionsFile, mockedVaadinJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(vaadinVersionsFile.toURI().toURL());

JsonObject pinnedVersions = nodeUpdater.getPlatformPinnedDependencies();

Assert.assertTrue(pinnedVersions.hasKey("@vaadin/button"));
Assert.assertTrue(pinnedVersions.hasKey("@vaadin/grid-pro"));
Assert.assertTrue(pinnedVersions.hasKey("@vaadin/vaadin-grid-pro"));
}

private String getPolymerVersion(JsonObject object) {
JsonObject deps = object.get("dependencies");
String version = deps.getString("@polymer/polymer");
Expand All @@ -334,4 +428,91 @@ private void resolveResource_unhappyPath(String resourceFolder) {
Assert.assertEquals("foo", nodeUpdater.resolveResource("foo"));
Assert.assertEquals("foo", nodeUpdater.resolveResource("foo"));
}

private JsonObject getMockVaadinCoreVersionsJson() {
// @formatter:off
return Json.parse(
"{\n" +
" \"bundles\": {\n" +
" \"vaadin\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/bundles\"\n" +
" }\n" +
" },\n" +
" \"core\": {\n" +
" \"accordion\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/accordion\"\n" +
" },\n" +
" \"app-layout\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/app-layout\"\n" +
" },\n" +
" \"avatar\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/avatar\"\n" +
" },\n" +
" \"avatar-group\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/avatar-group\"\n" +
" },\n" +
" \"button\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/button\"\n" +
" },\n" +
" \"checkbox\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/checkbox\"\n" +
" }" +
" },\n" +
" \"platform\": \"23.2.0\"\n" +
"}"
);
// @formatter:on
}

private JsonObject getMockVaadinVersionsJson() {
// @formatter:off
return Json.parse(
"{\n" +
" \"vaadin\": {\n" +
" \"board\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/board\"\n" +
" },\n" +
" \"charts\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/charts\"\n" +
" },\n" +
" \"grid-pro\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/grid-pro\"\n" +
" },\n" +
" \"vaadin-board\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-board\",\n" +
" \"pro\": true\n" +
" },\n" +
" \"vaadin-charts\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-charts\",\n" +
" \"pro\": true\n" +
" },\n" +
" \"vaadin-grid-pro\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-grid-pro\",\n" +
" \"pro\": true\n" +
" },\n" +
" },\n" +
" \"platform\": \"23.2.0\"\n" +
"}"
);
// @formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import elemental.json.Json;
import elemental.json.JsonObject;
Expand Down Expand Up @@ -69,16 +74,64 @@ public void missingVaadinDevDependencies_allDependenciesSholdBeUserHandled()
NodeUpdater.DEV_DEPENDENCIES);
}

@Test
public void testGetFilteredVersions_whenErrorHappens_versionOriginParameterIsUsedInErrorLogs()
throws IOException {
String pkgJson = IOUtils.toString(
Objects.requireNonNull(
getClass().getClassLoader().getResourceAsStream(
"versions/no_vaadin_package.json")),
StandardCharsets.UTF_8);
JsonObject packageJson = Json.parse(pkgJson);
VersionsJsonFilter filter = new VersionsJsonFilter(packageJson,
NodeUpdater.DEPENDENCIES);
String versionOrigin = "dummy-origin.json";

Logger logger = Mockito.spy(Logger.class);
try (MockedStatic<LoggerFactory> loggerFactoryMocked = Mockito
.mockStatic(LoggerFactory.class)) {
loggerFactoryMocked
.when(() -> LoggerFactory.getLogger(FrontendVersion.class))
.thenReturn(logger);

JsonObject sourceJsonMocked = getMockedJsonObject();

Mockito.when(sourceJsonMocked.getString(Mockito.anyString()))
.thenThrow(new ClassCastException());
filter.getFilteredVersions(sourceJsonMocked, versionOrigin);
Mockito.verify(logger, Mockito.times(1)).warn(
"Ignoring error while parsing frontend dependency version for package '{}' in '{}'",
"test", versionOrigin);

sourceJsonMocked = getMockedJsonObject();

String nfeMessage = "NFE MSG";
Mockito.when(sourceJsonMocked.getString(Mockito.anyString()))
.thenThrow(new NumberFormatException(nfeMessage));
filter.getFilteredVersions(sourceJsonMocked, versionOrigin);
Mockito.verify(logger, Mockito.times(1)).warn(
"Ignoring error while parsing frontend dependency version in {}: {}",
versionOrigin, nfeMessage);
}
}

private JsonObject getMockedJsonObject() {
JsonObject jsonObject = Mockito.mock(JsonObject.class);
Mockito.when(jsonObject.keys()).thenReturn(new String[] { "test" });
Mockito.when(jsonObject.hasKey("test")).thenReturn(true);
return jsonObject;
}

private void assertMissingVaadinDependencies_allDependenciesSholdBeUserHandled(
String depKey) throws IOException {
String versions = IOUtils
.toString(
getClass().getClassLoader()
.getResourceAsStream("versions/versions.json"),
StandardCharsets.UTF_8);
String versions = IOUtils.toString(
Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream("versions/versions.json")),
StandardCharsets.UTF_8);
String pkgJson = IOUtils.toString(
getClass().getClassLoader()
.getResourceAsStream("versions/no_vaadin_package.json"),
Objects.requireNonNull(
getClass().getClassLoader().getResourceAsStream(
"versions/no_vaadin_package.json")),
StandardCharsets.UTF_8);

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
Expand All @@ -95,16 +148,14 @@ private void assertMissingVaadinDependencies_allDependenciesSholdBeUserHandled(

private void assertFilterPlatformVersions_multipleUserChanged_correctlyIgnored(
String depKey) throws IOException {
String versions = IOUtils
.toString(
getClass().getClassLoader().getResourceAsStream(
"versions/user_versions.json"),
StandardCharsets.UTF_8);
String pkgJson = IOUtils
.toString(
getClass().getClassLoader().getResourceAsStream(
"versions/user_package.json"),
StandardCharsets.UTF_8);
String versions = IOUtils.toString(
Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream("versions/user_versions.json")),
StandardCharsets.UTF_8);
String pkgJson = IOUtils.toString(
Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream("versions/user_package.json")),
StandardCharsets.UTF_8);

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
depKey);
Expand Down Expand Up @@ -143,16 +194,14 @@ private void assertFilterPlatformVersions_multipleUserChanged_correctlyIgnored(

private void assertFilterPlatformVersions(String depKey)
throws IOException {
String versions = IOUtils
.toString(
getClass().getClassLoader()
.getResourceAsStream("versions/versions.json"),
StandardCharsets.UTF_8);
String pkgJson = IOUtils
.toString(
getClass().getClassLoader()
.getResourceAsStream("versions/package.json"),
StandardCharsets.UTF_8);
String versions = IOUtils.toString(
Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream("versions/versions.json")),
StandardCharsets.UTF_8);
String pkgJson = IOUtils.toString(
Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream("versions/package.json")),
StandardCharsets.UTF_8);

VersionsJsonFilter filter = new VersionsJsonFilter(Json.parse(pkgJson),
depKey);
Expand Down

0 comments on commit 913e166

Please sign in to comment.