Skip to content
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

Add persist dependency to the toml in the bal build instead of persist add #346

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
package io.ballerina.persist.tools;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -38,11 +42,25 @@
public class BuildCodeGeneratorTest {
public static final Path TARGET_DIR = Paths.get(System.getProperty("user.dir"), "build");
public static final Path TEST_DISTRIBUTION_PATH = TARGET_DIR.resolve("ballerina-distribution");
private String persistSqlVersion;

@BeforeClass
public void findLatestPersistVersion() {
Path versionPropertiesFile = Paths.get("../", "persist-cli", "src", "main", "resources",
"version.properties").toAbsolutePath();
try (InputStream inputStream = Files.newInputStream(versionPropertiesFile)) {
Properties properties = new Properties();
properties.load(inputStream);
persistSqlVersion = properties.get("persistSqlVersion").toString();
} catch (IOException e) {
// ignore
}
}

@Test(enabled = true)
public void testBuildWithMysql() throws IOException, InterruptedException {
String log = "Persist client and entity types generated successfully in " +
"the persist_build_1 directory.";
updateOutputBallerinaToml("tool_test_build_1");
String log = "Persist client and entity types generated successfully in the persist_build_1 directory.";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_1");
assertContainLogs(log, project);
}
Expand Down Expand Up @@ -82,6 +100,35 @@ public void testBuildWithoutEntities() throws IOException, InterruptedException
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithExistingDependency() throws IOException, InterruptedException {
updateOutputBallerinaToml("tool_test_build_7");
String log = "ERROR: the 'Ballerina.toml' file is already updated with the Persist client native dependency" +
" but the version is different from the current version. Please remove the existing dependency and " +
"try again.";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_7");
assertContainLogs(log, project);
}

private void updateOutputBallerinaToml(String fileName) {
String tomlFileName = "Ballerina.toml";
Path filePath = Paths.get("src", "test", "resources", "test-src", "input", fileName, tomlFileName);
if (filePath.endsWith(tomlFileName)) {
try {
String content = Files.readString(filePath);
String dataStore = "persist.sql";
String version = persistSqlVersion;
content = content.replaceAll(
"artifactId\\s=\\s\"" + dataStore + "-native\"\nversion\\s=\\s\\\"\\d+(\\.\\d+)+" +
"(-SNAPSHOT)?\\\"", "artifactId = \"" + dataStore +
"-native\"\nversion = \"" + version + "\"");
Files.writeString(filePath, content);
} catch (IOException e) {
// ignore
}
}
}

private String collectLogOutput(Path project) throws IOException, InterruptedException {
List<String> buildArgs = new LinkedList<>();
Process process = executeBuild(TEST_DISTRIBUTION_PATH.toString(), project, buildArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@

import io.ballerina.persist.cmd.Add;
import jdk.jfr.Description;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import picocli.CommandLine;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import static io.ballerina.persist.tools.utils.GeneratedSourcesTestUtils.Command.ADD;
import static io.ballerina.persist.tools.utils.GeneratedSourcesTestUtils.assertGeneratedSources;
Expand All @@ -42,31 +37,12 @@
*/
public class ToolingAddTest {

private String persistSqlVersion;
private String persistInMemoryVersion;
private String persistGoogleSheetsVersion;
private static final PrintStream errStream = System.err;
public static final String GENERATED_SOURCES_DIRECTORY = Paths.get("build", "generated-sources").toString();

@BeforeClass
public void findLatestPersistVersion() {
Path versionPropertiesFile = Paths.get("../", "persist-cli", "src", "main", "resources",
"version.properties").toAbsolutePath();
try (InputStream inputStream = Files.newInputStream(versionPropertiesFile)) {
Properties properties = new Properties();
properties.load(inputStream);
persistSqlVersion = properties.get("persistSqlVersion").toString();
persistInMemoryVersion = properties.get("persistInMemoryVersion").toString();
persistGoogleSheetsVersion = properties.get("persistGoogleSheetsVersion").toString();
} catch (IOException e) {
// ignore
}
}

@Test(enabled = true)
@Description("When the project is not initiated")
public void testAdd() {
updateOutputBallerinaToml("tool_test_add_1");
executeCommand("tool_test_add_1");
assertGeneratedSources("tool_test_add_1");
}
Expand All @@ -81,7 +57,6 @@ public void testAddUpdateConfigWithNewDbConfigurations() {
@Description("When there is a database config files inside the directories and there are missing database " +
"configurations")
public void testsInitUpdateConfigWithPartialyInitiatedFiles() {
updateOutputBallerinaToml("tool_test_add_3");
executeCommand("tool_test_add_3");
assertGeneratedSources("tool_test_add_3");
}
Expand All @@ -95,7 +70,6 @@ public void testsInitOutsideBalProject() {
@Test(enabled = true)
@Description("When the configs are already updated")
public void testsInitUpdateConfigWithUpdatedDbConfigurations() {
updateOutputBallerinaToml("tool_test_add_5");
executeCommand("tool_test_add_5");
assertGeneratedSources("tool_test_add_5");
}
Expand All @@ -110,15 +84,13 @@ public void testInitAlreadyInitializedProject() {
@Test(enabled = true)
@Description("Running init on a already initialized project with database configurations missing")
public void testInitAlreadyInitializedProjectWithOutPersistConfiguration() {
updateOutputBallerinaToml("tool_test_add_7");
executeCommand("tool_test_add_7");
assertGeneratedSources("tool_test_add_7");
}

@Test(enabled = true)
@Description("Running init on a project with manually created definition file")
public void testInitWithManuallyCreatedDefinitionFile() {
updateOutputBallerinaToml("tool_test_add_9");
executeCommand("tool_test_add_9");
assertGeneratedSources("tool_test_add_9");
}
Expand Down Expand Up @@ -162,7 +134,6 @@ public void testAddArgs() throws ClassNotFoundException, NoSuchMethodException,
@Test
public void testInitWithModuleArg() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
InstantiationException, IllegalAccessException {
updateOutputBallerinaToml("tool_test_add_12");
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).
newInstance(Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_12").toAbsolutePath().
Expand All @@ -175,7 +146,6 @@ public void testInitWithModuleArg() throws ClassNotFoundException, NoSuchMethodE
@Test(enabled = true)
public void testInitWithMssql() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
InstantiationException, IllegalAccessException {
updateOutputBallerinaToml("tool_test_add_13");
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).
newInstance(Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_13").toAbsolutePath().
Expand All @@ -188,7 +158,6 @@ public void testInitWithMssql() throws ClassNotFoundException, NoSuchMethodExcep
@Test
public void testInitWithPostgresql() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
updateOutputBallerinaToml("tool_test_add_14");
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).
newInstance(Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_14").toAbsolutePath().
Expand All @@ -198,33 +167,6 @@ public void testInitWithPostgresql() throws ClassNotFoundException, NoSuchMethod
assertGeneratedSources("tool_test_add_14");
}

private void updateOutputBallerinaToml(String fileName) {
String tomlFileName = "Ballerina.toml";
Path filePath = Paths.get("src", "test", "resources", "test-src", "output", fileName, tomlFileName);
if (filePath.endsWith(tomlFileName)) {
try {
String content = Files.readString(filePath);
String dataStore = "persist.inmemory";
String version = persistInMemoryVersion;
if (content.contains("datastore = \"mysql\"") || content.contains("datastore = \"mssql\"") ||
content.contains("datastore = \"postgresql\"")) {
dataStore = "persist.sql";
version = persistSqlVersion;
} else if (content.contains("datastore = \"googlesheets\"")) {
dataStore = "persist.googlesheets";
version = persistGoogleSheetsVersion;
}
content = content.replaceAll(
"artifactId\\s=\\s\"" + dataStore + "-native\"\nversion\\s=\\s\\\"\\d+(\\.\\d+)+" +
"(-SNAPSHOT)?\\\"", "artifactId = \"" + dataStore +
"-native\"\nversion = \"" + version + "\"");
Files.writeString(filePath, content);
} catch (IOException e) {
// ignore
}
}
}

private void executeCommand(String subDir) {
Class<?> persistClass;
Path sourcePath = Paths.get(GENERATED_SOURCES_DIRECTORY, subDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ filePath = "persist/model.bal"
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.2.1"
version = "1.3.0-SNAPSHOT"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
org = "foo"
name = "persist_build_7"
version = "0.1.0"
distribution = "2201.3.0"

[build-options]
observabilityIncluded = true

[[tool.persist]]
id = "generate-db-client"
targetModule = "persist_build_7"
options.datastore = "mysql"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.2.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you 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.

import ballerina/io;

public function main() {
io:println("hello");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you 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.

import ballerina/time;
import ballerina/persist as _;
import ballerina/constraint;

// Defines the entity type with the entity identity

type Employee record {|
readonly string empNo;
string firstName;
string lastName;
time:Date birthDate;
string gender;
time:Date hireDate;

Department department;
Workspace? workspace;
|};

type Workspace record {|
readonly string workspaceId;
string workspaceType;

Building location;
Employee employee;
|};

type Building record {|
readonly string buildingCode;
string city;
string state;
string country;
string postalCode;

Workspace[] workspaces;
|};

type Department record {|
@constraint:String {
maxLength: 10
}
readonly string deptNo;
string deptName;

Employee[] employees;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ value = 2

array = [ [1, 0], [0, 1] ]

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "0.2.1-SNAPSHOT"

[[tool.persist]]
id = "generate-db-client"
targetModule = "persist_add_1"
options.datastore = "mysql"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.3.0-SNAPSHOT"

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ targetModule = "persist_add_12.test"
options.datastore = "inmemory"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.inmemory-native"
version = "1.2.0"

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ targetModule = "persist_add_13"
options.datastore = "mssql"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.3.0-SNAPSHOT"

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ targetModule = "persist_add_14"
options.datastore = "postgresql"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.3.0-SNAPSHOT"

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,3 @@ targetModule = "persist_add_2"
options.datastore = "inmemory"
filePath = "persist/model.bal"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.inmemory-native"
version = "1.2.0"

Loading
Loading