/coteafs-datasource

:ledger: Simple Data file to object parser supports JSON, YML, XML and properties file format.

Primary LanguageJavaApache License 2.0Apache-2.0

Open Source Love Test

Maven Central Github Releases License

Usage 🏃

Dependency

<dependency>
    <groupId>com.github.wasiqb.coteafs</groupId>
    <artifactId>datasource</artifactId>
    <version>1.2.0</version>
</dependency>

Example

Pojo Classes

Pojo class for our data file login-data.yml.

import java.util.List;

import com.github.wasiqb.coteafs.datasource.annotation.DataFile;
import lombok.Data;

@Data
@DataFile
public class LoginData {
    private List<Login> loginData;
}

@Data
public class Login {
    private String password;
    private String userName;
    private String path;
}

Data file content

Data for our Yml data file login-data.yml.

login_data:
  - user_name: WasiqB
    password: Admin
    path: ${sys:user.dir}
  - user_name: FaisalK
    password: Abcd
    path: ${sys:user.dir}

Parsing data file

Following is an example to convert data file into a TestNG data provider.

import static com.google.common.truth.Truth.assertWithMessage;
import static java.lang.System.getProperty;

import com.github.wasiqb.coteafs.datasource.data.LoginData;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataSourceYmlTest {
    @DataProvider
    public Iterator<Object[]> getLoginDataYml () {
        final LoginData loginData = DataSource.parse (LoginData.class);
        final List<Object[]> data = new ArrayList<> ();
        loginData.getLoginData ()
            .forEach (d -> data.add (new Object[] { d }));
        return data.iterator ();
    }

    @Test (dataProvider = "getLoginDataYml")
    public void testYmlDataSource (final Login login) {
        assertWithMessage ("User Name").that (login.getUserName ())
            .isNotEmpty ();
        assertWithMessage ("Password").that (login.getPassword ())
            .isNotEmpty ();
        assertWithMessage ("Path").that (login.getPath ())
            .isEqualTo (getProperty ("user.dir"));
    }
}

Parsing placeholders in file field values

You can use placeholders in JSON and YML files. Following is the table of allowed variable formats which can be used in the placeholder.

Desired value Sample Placeholder
Base64 Decoder ${base64Decoder:SGVsbG9Xb3JsZCE=}
Base64 Encoder ${base64Encoder:HelloWorld!}
Java Constant ${const:java.awt.event.KeyEvent.VK_ESCAPE}
Date ${date:yyyy-MM-dd}
DNS ${dns:address|apache.org}
Environment Variable ${env:USERNAME}
File Content ${file:UTF-8:src/test/resources/document.properties}
Java ${java:version}
Localhost ${localhost:canonical-name}
Properties File ${properties:src/test/resources/document.properties::mykey}
Resource Bundle ${resourceBundle:org.example.testResourceBundleLookup:mykey}
Script ${script:javascript:3 + 4}
System Property ${sys:user.dir}
URL Decoder ${urlDecoder:Hello%20World%21}
URL Encoder ${urlEncoder:Hello World!}
URL Content (HTTP) ${url:UTF-8:http://www.apache.org}
URL Content (HTTPS) ${url:UTF-8:https://www.apache.org}
URL Content (File) ${url:UTF-8:file:///${sys:user.dir}/src/test/resources/document.properties}
XML XPath ${xml:src/test/resources/document.xml:/root/path/to/node}

Custom value is not supported in the placeholder.

Contributors ✨

Thanks to these wonderful people (emoji key):


Wasiq Bhamla

💻 ⚠️ 🚇 📖 🤔 🚧

Mohammad Faisal Khatri

⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!