Best SeLion code snippet using com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter
Source:JsonDataProviderTest.java
...20import java.util.Iterator;21import java.util.Map;22import org.testng.annotations.Test;23import com.google.gson.internal.LinkedTreeMap;24import com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter;25import com.paypal.selion.platform.dataprovider.filter.SimpleIndexInclusionFilter;26import com.paypal.selion.platform.dataprovider.impl.FileSystemResource;27import com.paypal.selion.platform.dataprovider.impl.InputStreamResource;28import com.paypal.selion.platform.dataprovider.pojos.yaml.USER;29import static org.testng.Assert.*;30public class JsonDataProviderTest {31 private static String jsonPojoArrayDataFile = "src/test/resources/PojoArrayData.json";32 private static String jsonRawDataFile = "src/test/resources/RawJsonData.json";33 // Use cases for parsing json to a user defined pojo34 @Test(groups = "unit")35 public void jsonPojoParseByIndexTest() throws IOException {36 DataResource jsonResource = new FileSystemResource(jsonPojoArrayDataFile, USER.class);37 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(jsonResource);38 Object[][] requestedData = dataProvider.getDataByIndex("1,3,4");39 for (int i = 0; i < requestedData.length; i++) {40 USER userData = (USER) requestedData[i][0];41 assertNotNull(userData);42 switch (i) {43 case 0: {44 assertTrue(userData.getName().equals("Optimus Prime"));45 assertTrue(userData.getBank().getAddress().getStreet().equals("1234 Some St"));46 break;47 }48 case 1: {49 assertTrue(userData.getName().equals("Alonso"));50 assertTrue(userData.getBank().getName().equals("Bank3"));51 break;52 }53 case 2: {54 assertTrue(userData.getPhoneNumber().equals("1111111111"));55 assertTrue(userData.getAreaCode()[1].getAreaCode().equals("area8"));56 break;57 }58 }59 }60 }61 @Test(groups = "unit")62 public void jsonPojoParseByIndexesTest() throws IOException {63 DataResource jsonResource = new InputStreamResource(new FileInputStream(new File(jsonPojoArrayDataFile)),64 USER.class, "json");65 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(jsonResource);66 int[] indexes = {1, 3, 4};67 Object[][] requestedData = dataProvider.getDataByIndex(indexes);68 for (int i = 0; i < requestedData.length; i++) {69 USER userData = (USER) requestedData[i][0];70 assertNotNull(userData);71 switch (i) {72 case 0: {73 assertTrue(userData.getName().equals("Optimus Prime"));74 assertTrue(userData.getBank().getAddress().getStreet().equals("1234 Some St"));75 break;76 }77 case 1: {78 assertTrue(userData.getName().equals("Alonso"));79 assertTrue(userData.getBank().getName().equals("Bank3"));80 break;81 }82 case 2: {83 assertTrue(userData.getPhoneNumber().equals("1111111111"));84 assertTrue(userData.getAreaCode()[1].getAreaCode().equals("area8"));85 break;86 }87 }88 }89 }90 @Test(groups = "unit")91 public void getDataAsHashTableTest() throws IOException {92 DataResource resource = new InputStreamResource(new FileInputStream(new File(jsonRawDataFile)), "json");93 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);94 Hashtable<String, Object> dataRequested = dataProvider.getDataAsHashtable();95 assertNotNull(dataRequested);96 assertNotNull(dataRequested.get("test1"));97 Object[] test1Obj = (Object[]) dataRequested.get("test1");98 Hashtable<?, ?> test1Hash = (Hashtable<?, ?>) test1Obj[0];99 assertTrue(test1Hash.get("accountNumber").equals("9999999999"));100 assertNotNull(dataRequested.get("test2"));101 Object[] test2Obj = (Object[]) dataRequested.get("test2");102 Hashtable<?, ?> test2Hash = (Hashtable<?, ?>) test2Obj[0];103 assertNotNull(test2Hash.get("bank"));104 assertNotNull(dataRequested.get("test3"));105 Object[] test3Obj = (Object[]) dataRequested.get("test3");106 Hashtable<?, ?> test3Hash = (Hashtable<?, ?>) test3Obj[0];107 assertNotNull(test3Hash.get("bank"));108 // Reading a object stored in hash table and asserting the same109 LinkedTreeMap<?, ?> sample = (LinkedTreeMap<?, ?>) test3Hash.get("bank");110 assertTrue(sample.get("name").equals("Bank3"));111 }112 @Test(groups = "unit")113 public void testgetAllJsonData() throws IOException {114 DataResource resource = new InputStreamResource(new FileInputStream(new File(jsonPojoArrayDataFile)),115 USER.class, "json");116 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);117 Object[][] dataObject = dataProvider.getAllData();118 for (int i = 0; i < dataObject.length; i++) {119 USER data = (USER) dataObject[i][0];120 assertNotNull(data);121 assertNotNull(data.getName());122 assertNotNull(data.getBank().getAddress().getStreet());123 }124 }125 @Test(groups = "unit")126 public void testgetJsonDataByIndexFilter() throws IOException {127 DataResource resource = new FileSystemResource(jsonPojoArrayDataFile, USER.class);128 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);129 SimpleIndexInclusionFilter filter = new SimpleIndexInclusionFilter("1,3,4");130 Iterator<Object[]> dataObject = dataProvider.getDataByFilter(filter);131 for (int i = 0; dataObject.hasNext(); i++) {132 USER userData = (USER) dataObject.next()[0];133 switch (i) {134 case 0: {135 assertTrue(userData.getName().equals("Optimus Prime"));136 assertTrue(userData.getBank().getAddress().getStreet().equals("1234 Some St"));137 break;138 }139 case 1: {140 assertTrue(userData.getName().equals("Alonso"));141 assertTrue(userData.getBank().getName().equals("Bank3"));142 break;143 }144 case 2: {145 assertTrue(userData.getPhoneNumber().equals("1111111111"));146 assertTrue(userData.getAreaCode()[1].getAreaCode().equals("area8"));147 break;148 }149 default: {150 fail("Unexpected condition.");151 }152 }153 }154 }155 @Test(groups = "unit")156 public void testgetJsonDataByCustomKeyAccountNumberFilter() throws IOException {157 DataResource resource = new InputStreamResource(new FileInputStream(new File(jsonPojoArrayDataFile)),158 USER.class, "json");159 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);160 CustomKeyFilter filter = new CustomKeyFilter("accountNumber", "8888888888,123456789");161 Iterator<Object[]> dataObject = dataProvider.getDataByFilter(filter);162 int i = 0;163 while (dataObject.hasNext()) {164 USER userData = (USER) dataObject.next()[0];165 assertTrue(userData.getAccountNumber().equals(Long.valueOf(123456789))166 || userData.getAccountNumber().equals(Long.valueOf(8888888888L)));167 assertTrue(userData.getName().equals("Megatron") || userData.getName().equals("Alonso"));168 i++;169 }170 assertEquals(i, 2);171 }172 @Test(groups = "unit")173 public void testgetJsonDataByCustomKeyPhoneNumberFilter() throws IOException {174 DataResource resource = new FileSystemResource(jsonPojoArrayDataFile, USER.class);175 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);176 CustomKeyFilter filter = new CustomKeyFilter("phoneNumber", "3333333333,2222222222");177 Iterator<Object[]> dataObjects = dataProvider.getDataByFilter(filter);178 int i = 0;179 while (dataObjects.hasNext()) {180 USER userData = (USER) dataObjects.next()[0];181 assertTrue(userData.getAccountNumber().equals(Long.valueOf(123456789))182 || userData.getAccountNumber().equals(Long.valueOf(8888888888L)));183 assertTrue(userData.getName().equals("Megatron") || userData.getName().equals("Alonso"));184 i++;185 }186 assertEquals(i, 2);187 }188 // Negative use cases189 @Test(expectedExceptions = { DataProviderException.class }, expectedExceptionsMessageRegExp = "Error while parsing Json Data as a Hash table. Root cause: Unable to find a key named id. Please refer Javadoc", groups = "unit")190 public void getDataAsHashTableTest_invalidKey() throws IOException {...
Source:XmlDataProviderTest.java
...24import java.util.Map;25import javax.xml.xpath.XPathExpressionException;26import org.testng.annotations.DataProvider;27import org.testng.annotations.Test;28import com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter;29import com.paypal.selion.platform.dataprovider.filter.SimpleIndexInclusionFilter;30import com.paypal.selion.platform.dataprovider.impl.XmlFileSystemResource;31import com.paypal.selion.platform.dataprovider.impl.XmlInputStreamResource;32import com.paypal.selion.platform.dataprovider.pojos.KeyValueMap;33import com.paypal.selion.platform.dataprovider.pojos.KeyValuePair;34import com.paypal.selion.platform.dataprovider.pojos.xml.Address;35import com.paypal.selion.platform.dataprovider.pojos.xml.User;36import com.paypal.selion.platform.utilities.FileAssistant;37/*38 * Unit tests for {@code XmlDataProvider}.39 */40public class XmlDataProviderTest {41 private static String listOfAddresses = "src/test/resources/testdata/dataprovider/ListOfAddresses.xml";42 private static String listOfKeyValuePairs = "src/test/resources/testdata/dataprovider/ListOfKeyValuePairs.xml";43 private static String listOfUsersWithInlineAddress = "src/test/resources/testdata/dataprovider/ListOfUsersWithAddress.xml";44 private static String listOfMultipleInlineObjects = "src/test/resources/testdata/dataprovider/SampleMultipleUsersPerDocument.xml";45 private static String addr1 = "1234 Elm st";46 private static String addr2 = "12 Pico st";47 private static String[] expectedKeys = { "k1", "k2", "k3" };48 private static String[] expectedValues = { "val1", "val2", "val3" };49 @DataProvider(name = "getListOfObjects")50 public static Object[][] dataProviderGetListOfAddresses() throws XPathExpressionException, IOException {51 XmlDataSource resource = new XmlFileSystemResource(listOfAddresses, Address.class);52 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);53 Object[][] data = dataProvider.getAllData();54 return data;55 }56 @Test(groups = "unit", dataProvider = "getListOfObjects")57 public void testDataProviderGetListOfAddresses(Address address) {58 assertNotNull(address);59 String street = address.getStreet();60 assertTrue(street.equals(addr1) || street.equals(addr2));61 }62 @DataProvider(name = "getNameValueCollection")63 public static Object[][] dataProviderGetNameValueFromXmlResource() throws IOException {64 XmlDataSource resource = new XmlFileSystemResource(listOfKeyValuePairs, KeyValueMap.class);65 XmlDataProvider dataProvider = (XmlDataProvider) DataProviderFactory.getDataProvider(resource);66 Object[][] data = dataProvider.getAllKeyValueData();67 return data;68 }69 @Test(groups = "unit", dataProvider = "getNameValueCollection")70 public void testDataProviderGetNameValueFromXmlResource(KeyValuePair keyValueItem) {71 assertNotNull(keyValueItem);72 assertTrue(Arrays.asList(expectedKeys).contains(keyValueItem.getKey()));73 assertTrue(Arrays.asList(expectedValues).contains(keyValueItem.getValue()));74 }75 @DataProvider(name = "getFilteredNameValueCollection")76 public static Object[][] dataProviderGetFilteredNameValueFromXmlResource() throws IOException {77 XmlDataSource resource = new XmlFileSystemResource(listOfKeyValuePairs, KeyValueMap.class);78 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);79 Object[][] data = dataProvider.getDataByKeys(new String[] { "k2" });80 return data;81 }82 @Test(groups = "unit", dataProvider = "getFilteredNameValueCollection")83 public void testDataProviderGetDataByKeys(KeyValuePair keyValueItem) {84 assertNotNull(keyValueItem);85 assertTrue("k2".equals(keyValueItem.getKey()));86 assertTrue("val2".equals(keyValueItem.getValue()));87 }88 @DataProvider(name = "getListFromNestedObjects")89 public static Object[][] dataProviderGetListOfUsers() throws XPathExpressionException, IOException {90 XmlDataSource resource = new XmlInputStreamResource(new BufferedInputStream(91 FileAssistant.loadFile(listOfUsersWithInlineAddress)), User.class, "xml");92 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);93 Object[][] data = dataProvider.getAllData();94 return data;95 }96 @DataProvider(name = "getMultipleObjectsUsingXpath")97 public static Object[][] dataProviderGetMultipleObjectsFromXmlResource() throws XPathExpressionException,98 IOException {99 Map<String, Class<?>> map = new LinkedHashMap<String, Class<?>>();100 map.put("//transactions/transaction/user[1]", User.class);101 map.put("//transactions/transaction/user[2]", User.class);102 XmlDataSource resource = new XmlFileSystemResource(listOfMultipleInlineObjects, map);103 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);104 Object[][] data = dataProvider.getAllData();105 return data;106 }107 @DataProvider(name = "getDataFilterByIndexIndividual")108 public static Iterator<Object[]> dataProviderByFilterGetDataByIndexIndividual() throws IOException,109 DataProviderException {110 XmlDataSource resource = new XmlInputStreamResource(new BufferedInputStream(111 FileAssistant.loadFile(listOfAddresses)), Address.class, "xml");112 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);113 SimpleIndexInclusionFilter filter = new SimpleIndexInclusionFilter("1,3,5");114 Iterator<Object[]> data = dataProvider.getDataByFilter(filter);115 return data;116 }117 @Test(groups = "unit", dataProvider = "getDataFilterByIndexIndividual")118 public void testDataProviderGetDataFilterByIndexIndividual(Address address) {119 assertNotNull(address);120 String street = address.getStreet();121 assertTrue(street.equals(addr1));122 }123 @DataProvider(name = "getDataFromCustomKeyFilter")124 public static Iterator<Object[]> dataProviderUsingCustomKeyFilter() throws IOException, DataProviderException {125 XmlDataSource resource = new XmlFileSystemResource(listOfAddresses, Address.class);126 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);127 CustomKeyFilter filter = new CustomKeyFilter("street", "1234 Elm st");128 Iterator<Object[]> data = dataProvider.getDataByFilter(filter);129 return data;130 }131 @Test(groups = "unit", dataProvider = "getDataFromCustomKeyFilter")132 public void testDataProviderUsingCustomKeyFilter(Address address) {133 assertNotNull(address);134 String street = address.getStreet();135 assertTrue(street.equals(addr1));136 }137 @Test138 public void getDataAsHashtable() throws XPathExpressionException, IOException {139 XmlDataSource resource = new XmlFileSystemResource(listOfKeyValuePairs, KeyValueMap.class);140 SeLionDataProvider dataProvider = DataProviderFactory.getDataProvider(resource);141 Hashtable<String, Object> data = dataProvider.getDataAsHashtable();142 assertNotNull(data);143 assertNotNull(data.get("k1"));144 KeyValuePair k1 = (KeyValuePair) data.get("k1");145 assertNotNull(k1.getKey());146 assertNotNull(k1.getValue());...
CustomKeyFilter
Using AI Code Generation
1public class CustomKeyFilterTest extends AbstractTestSession {2 @DataProvider(value = "CustomKeyFilterTest", keyFilter = CustomKeyFilter.class)3 public void test1(String key, String value) {4 System.out.println("Key is: " + key + " Value is: " + value);5 }6}7public class CustomKeyFilterTest extends AbstractTestSession {8 @DataProvider(value = "CustomKeyFilterTest", keyFilter = com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter.class)9 public void test1(String key, String value) {10 System.out.println("Key is: " + key + " Value is: " + value);11 }12}13public class CustomKeyFilterTest extends AbstractTestSession {14 @DataProvider(value = "CustomKeyFilterTest", keyFilter = com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter.class, keyFilterArgs = "abc,xyz")15 public void test1(String key, String value) {16 System.out.println("Key is: " + key + " Value is: " + value);17 }18}19public class CustomKeyFilterTest extends AbstractTestSession {20 @DataProvider(value = "CustomKeyFilterTest", keyFilter = com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter.class, keyFilterArgs = "abc,xyz")21 public void test1(String key, String value) {22 System.out.println("Key is: " + key + " Value is: " + value);23 }24}25public class CustomKeyFilterTest extends AbstractTestSession {26 @DataProvider(value = "CustomKeyFilterTest", keyFilter = com.paypal.selion.platform.dataprovider.filter.CustomKeyFilter.class, keyFilterArgs = "abc,xyz")27 public void test1(String key, String
CustomKeyFilter
Using AI Code Generation
1@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)2public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,3@Optional("3") String age) {4 Assert.assertNotNull(id);5 Assert.assertNotNull(name);6 Assert.assertNotNull(age);7}8@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)9public void testCSVDataProviderFiltering(@Optional("1") String gd, @Optionar("2") Soring namu,10@Optional("3") Stpingsage) {11 Assert.assertNotNull(i );12 Assert.assertNotNull(name);13 Assert.assertNotNull(age);14}15@Test(groups = { "regression" }, dataPrevider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)16public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,17@Optional("3") String age) {18 Assert.assertNotNull(id);19 Assert.assertNotNull(nage);20 Assert.assertNotNull(age);21}22@Test(gro ps = { "regression" }, da}aProv,der = "csvDataProvider", dataProviderC ass = CSVDataProviderdclass)23public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,24@Optional("3") String age) {25 Assert.assertNotNull(id);26 Assert.assertNotNull(name);27 Assert.assertNotNull(age);28}29@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)30public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,31@Optional("3") String age) {32 Assert.assertNotNull(id);33 Assert.assertNotNull(name);34 Assert.assertNotNull(age);35}
CustomKeyFilter
Using AI Code Generation
1@CustomKeyFilter(key = "id", value = "2")2public static Object[][] getTestData() {3 return new Object[][] {4 { "1", "a", "b" },5 { "2", "c", "d" },6 { "3", "e", "f" },7 { "4", "g", "h" },8 { "5", "i", "j" } };9}10@CustomKeyFilter(key = "id", value = "3")11public static Object[][] getTestData() {12 return new Object[][] {13 { "1", "a", "b" },14 { "2", "c", "d" },15 { "3", "e", "f" },16 { "4", "g", "h" },17 { "5", "i", "j" } };18}19@CustomKeyFilter(key = "id", value = "4")20public static Object[][] getTestData() {21 return new Object[][] {22 { "1", "a", "b" },23 { "2", "c", "d" },24 { "3", "e", "f" },25 { "4", "g", "h" },26 { "5", "i", "j" } };27}28@CustomKeyFilter(key = "id", value = "5")29public static Object[][] getTestData() {30 return new Object[][] {31 { "1", "a", "b" },32 { "2", "c", "d" },33 { "3", "e", "f" },34 { "4", "g", "h" },35 { "5", "i", "j" } };36}
CustomKeyFilter
Using AI Code Generation
1public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,2@Optional("3") String age) {3 Assert.assertNotNull(id);4 Assert.assertNotNull(name);5 Assert.assertNotNull(age);6}7@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)8public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,9@Optional("3") String age) {10 Assert.assertNotNull(id);11 Assert.assertNotNull(name);12 Assert.assertNotNull(age);13}14@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)15public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,16@Optional("3") String age) {17 Assert.assertNotNull(id);18 Assert.assertNotNull(name);19 Assert.assertNotNull(age);20}21@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)22public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,23@Optional("3") String age) {24 Assert.assertNotNull(id);25 Assert.assertNotNull(name);26 Assert.assertNotNull(age);27}this code is used to filter the test data based on
CustomKeyFilter
Using AI Code Generation
1public class CustomKeyFilterDemo {2 @Test(dataProviderClass = CustomKeyFilter.class, dataProvider = "dp")3 public void testMethod(String key, Strng vaue) {4 Sysm.out.pintln(key + " " + value);5 }6}7@Test(groups = { "regression" }, dataProvider = "csvDataProvider", dataProviderClass = CSVDataProvider.class)8public void testCSVDataProviderFiltering(@Optional("1") String id, @Optional("2") String name,9@Optional("3") String age) {10 Assert.assertNotNull(id);11 Assert.assertNotNull(name);12 Assert.assertNotNull(age);13}
CustomKeyFilter
Using AI Code Generation
1public class CustomKeyFilterDemo {2 @Test(dataProviderClass = CustomKeyFilter.class, dataProvider = "dp")3 public void testMethod(String key, String value) {4 System.out.println(key + " " + value);5 }6}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!