From 6f39d89fd769ecf91ee19e58769b60f5b8e4e91e Mon Sep 17 00:00:00 2001 From: yagudin10 Date: Wed, 3 Aug 2022 12:27:25 +0300 Subject: [PATCH] CB-2313 remove dependency from ee test suite --- .../io/cloudbeaver/utils/WebTestUtils.java | 56 +++++++++++++++++++ .../cloudbeaver/test/platform/AllTests.java | 22 -------- .../test/platform/AuthenticationTest.java | 7 ++- .../test/platform/PlatformTest.java | 13 +++-- 4 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/utils/WebTestUtils.java diff --git a/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/utils/WebTestUtils.java b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/utils/WebTestUtils.java new file mode 100644 index 0000000000..d6b7c85b6d --- /dev/null +++ b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/utils/WebTestUtils.java @@ -0,0 +1,56 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.cloudbeaver.utils; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +import java.io.File; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +public class WebTestUtils { + + public static String readScriptTemplate(String templateName, Path scriptsPath) throws Exception { + Path templatePath = new File(String.valueOf(scriptsPath), templateName).toPath(); + return Files.readString(templatePath); + } + + + public static Map doPost(String apiUrl, String input, HttpClient client) throws Exception { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(apiUrl)) + .POST(HttpRequest.BodyPublishers.ofString(input)) + .header("Content-Type", "application/json") + .build(); + + HttpResponse response = client.send(request, + HttpResponse.BodyHandlers.ofString()); + + return new GsonBuilder().create().fromJson( + response.body(), + new TypeToken>() { + }.getType() + ); + } +} diff --git a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AllTests.java b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AllTests.java index 5b85d36efc..cc878c94e8 100644 --- a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AllTests.java +++ b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AllTests.java @@ -104,28 +104,6 @@ public class AllTests { return client; } - public static Map doPost(String input, HttpClient client) throws Exception { - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(GQL_API_URL)) - .POST(HttpRequest.BodyPublishers.ofString(input)) - .header("Content-Type", "application/json") - .build(); - - HttpResponse response = client.send(request, - HttpResponse.BodyHandlers.ofString()); - - return new GsonBuilder().create().fromJson( - response.body(), - new TypeToken>() { - }.getType() - ); - } - - public static String readScriptTemplate(String templateName, Path scriptsPath) throws Exception { - Path templatePath = new File(String.valueOf(scriptsPath), templateName).toPath(); - return Files.readString(templatePath); - } - public static Path getScriptsPath() { return scriptsPath; } diff --git a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AuthenticationTest.java b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AuthenticationTest.java index b999eb121a..02ba01b282 100644 --- a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AuthenticationTest.java +++ b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AuthenticationTest.java @@ -1,5 +1,6 @@ package io.cloudbeaver.test.platform; +import io.cloudbeaver.utils.WebTestUtils; import org.jkiss.dbeaver.model.auth.SMAuthStatus; import org.jkiss.dbeaver.model.data.json.JSONUtils; import org.jkiss.utils.CommonUtils; @@ -22,9 +23,9 @@ public class AuthenticationTest { Assert.assertEquals(SMAuthStatus.SUCCESS.name(), JSONUtils.getString(authInfo, "authStatus")); } - public static Map authenticateUser(HttpClient client, Path scriptsPath) throws Exception { - String input = AllTests.readScriptTemplate(GQL_TEMPLATE_AUTH_LOGIN, scriptsPath); - Map map = AllTests.doPost(input, client); + private Map authenticateUser(HttpClient client, Path scriptsPath) throws Exception { + String input = WebTestUtils.readScriptTemplate(GQL_TEMPLATE_AUTH_LOGIN, scriptsPath); + Map map = WebTestUtils.doPost(AllTests.GQL_API_URL, input, client); Map data = JSONUtils.getObjectOrNull(map, "data"); if (data != null) { return JSONUtils.getObjectOrNull(data, "authInfo"); diff --git a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/PlatformTest.java b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/PlatformTest.java index 3f28406a7d..e6c1cbd21a 100644 --- a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/PlatformTest.java +++ b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/PlatformTest.java @@ -17,6 +17,7 @@ package io.cloudbeaver.test.platform; +import io.cloudbeaver.utils.WebTestUtils; import org.jkiss.dbeaver.model.data.json.JSONUtils; import org.jkiss.dbeaver.runtime.DBWorkbench; import org.jkiss.dbeaver.utils.GeneralUtils; @@ -64,8 +65,8 @@ public class PlatformTest { } private List> getUserConnections(HttpClient client) throws Exception { - String input = AllTests.readScriptTemplate(GQL_TEMPLATE_USER_CONNECTIONS, AllTests.getScriptsPath()); - Map map = AllTests.doPost(input, client); + String input = WebTestUtils.readScriptTemplate(GQL_TEMPLATE_USER_CONNECTIONS, AllTests.getScriptsPath()); + Map map = WebTestUtils.doPost(AllTests.GQL_API_URL, input, client); Map data = JSONUtils.getObjectOrNull(map, "data"); if (data != null) { return JSONUtils.getObjectList(data, "userConnections"); @@ -74,8 +75,8 @@ public class PlatformTest { } private Map createConnection(HttpClient client) throws Exception { - String input = AllTests.readScriptTemplate(GQL_TEMPLATE_CREATE_CONNECTION, AllTests.getScriptsPath()); - Map map = AllTests.doPost(input, client); + String input = WebTestUtils.readScriptTemplate(GQL_TEMPLATE_CREATE_CONNECTION, AllTests.getScriptsPath()); + Map map = WebTestUtils.doPost(AllTests.GQL_API_URL, input, client); Map data = JSONUtils.getObjectOrNull(map, "data"); if (data != null) { return JSONUtils.getObjectOrNull(data, "createConnection"); @@ -84,9 +85,9 @@ public class PlatformTest { } private boolean deleteConnection(HttpClient client, String connectionId) throws Exception { - String input = AllTests.readScriptTemplate(GQL_TEMPLATE_DELETE_CONNECTION, AllTests.getScriptsPath()) + String input = WebTestUtils.readScriptTemplate(GQL_TEMPLATE_DELETE_CONNECTION, AllTests.getScriptsPath()) .replace("${connectionId}", connectionId); - Map map = AllTests.doPost(input, client); + Map map = WebTestUtils.doPost(AllTests.GQL_API_URL, input, client); Map data = JSONUtils.getObjectOrNull(map, "data"); if (data != null) { return JSONUtils.getBoolean(data, "deleteConnection");