Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 5347833

Browse files
authored
Merge pull request #34 from sbtqa/feature/custom-array-delimiter
Add custom value to array delimiter
2 parents 6fe4a31 + d6ee167 commit 5347833

3 files changed

Lines changed: 34 additions & 11 deletions

File tree

providers/properties-provider/src/main/java/ru/sbtqa/tag/datajack/providers/properties/PropertiesDataProvider.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import com.mongodb.BasicDBObject;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import pl.jalokim.propertiestojson.resolvers.primitives.BooleanJsonTypeResolver;
7+
import pl.jalokim.propertiestojson.resolvers.primitives.ObjectFromTextJsonTypeResolver;
8+
import pl.jalokim.propertiestojson.resolvers.primitives.PrimitiveArrayJsonTypeResolver;
9+
import pl.jalokim.propertiestojson.resolvers.primitives.StringJsonTypeResolver;
610
import pl.jalokim.propertiestojson.util.PropertiesToJsonConverter;
711
import ru.sbtqa.tag.datajack.TestDataProvider;
812
import ru.sbtqa.tag.datajack.exceptions.CollectionNotFoundException;
@@ -26,6 +30,7 @@ public class PropertiesDataProvider extends AbstractDataProvider {
2630
private static final String DEFAULT_EXTENSION = "properties";
2731
private static final String REF_TPL = "$ref";
2832
private final String extension;
33+
private String arrayDelimiter = ",";
2934
private String testDataFolder;
3035

3136
/**
@@ -50,12 +55,13 @@ public PropertiesDataProvider(String testDataFolder, String collectionName) thro
5055
*
5156
* @param testDataFolder path to data folder
5257
* @param collectionName properties file name
53-
* @param extension custom file extension
58+
* @param extension custom file extension
59+
* @param arrayDelimiter custom value array delimiter
5460
* @throws DataException if file not found in testDataFolder
5561
*/
56-
public PropertiesDataProvider(String testDataFolder, String collectionName, String extension) throws DataException {
62+
public PropertiesDataProvider(String testDataFolder, String collectionName, String extension, String arrayDelimiter) throws DataException {
5763
this.extension = extension;
58-
64+
this.arrayDelimiter = arrayDelimiter;
5965
String json = readFile(testDataFolder, collectionName);
6066

6167
BasicDBObject parsed = parse(json);
@@ -64,43 +70,45 @@ public PropertiesDataProvider(String testDataFolder, String collectionName, Stri
6470
this.collectionName = collectionName;
6571
}
6672

67-
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String extension) {
73+
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String extension, String arrayDelimiter) {
6874
this.extension = extension;
6975
this.testDataFolder = testDataFolder;
7076
this.basicObject = obj;
7177
this.collectionName = collectionName;
78+
this.arrayDelimiter=arrayDelimiter;
7279
}
7380

74-
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String way, String extension) {
81+
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String way, String extension, String arrayDelimiter) {
7582
this.extension = extension;
7683
this.testDataFolder = testDataFolder;
7784
this.basicObject = obj;
7885
this.way = way;
7986
this.collectionName = collectionName;
87+
this.arrayDelimiter=arrayDelimiter;
8088
}
8189

8290
/**
8391
* {@inheritDoc}
8492
*/
8593
@Override
8694
protected PropertiesDataProvider createInstance(String collectionName) throws DataException {
87-
return new PropertiesDataProvider(testDataFolder, collectionName, extension);
95+
return new PropertiesDataProvider(testDataFolder, collectionName, extension, arrayDelimiter);
8896
}
8997

9098
/**
9199
* {@inheritDoc}
92100
*/
93101
@Override
94102
protected PropertiesDataProvider createInstance(BasicDBObject obj, String collectionName, String way) {
95-
return new PropertiesDataProvider(testDataFolder, obj, collectionName, way, extension);
103+
return new PropertiesDataProvider(testDataFolder, obj, collectionName, way, extension, arrayDelimiter);
96104
}
97105

98106
/**
99107
* {@inheritDoc}
100108
*/
101109
@Override
102110
protected PropertiesDataProvider createInstance(BasicDBObject obj, String collectionName) {
103-
return new PropertiesDataProvider(testDataFolder, obj, collectionName, extension);
111+
return new PropertiesDataProvider(testDataFolder, obj, collectionName, extension, arrayDelimiter);
104112
}
105113

106114
/**
@@ -129,7 +137,7 @@ public TestDataProvider getReference() throws DataException {
129137
}
130138
String refValue = this.basicObject.getString(REF_TPL);
131139
String referencedCollection = refValue.contains(":") ? refValue.split(":")[0] : this.collectionName;
132-
String collectionPrefix = refValue.startsWith("/") ? "" :this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
140+
String collectionPrefix = refValue.startsWith("/") ? "" : this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
133141
this.path = refValue.contains(":") ? refValue.split(":")[1] : refValue;
134142
AbstractDataProvider reference = (AbstractDataProvider) this.fromCollection(collectionPrefix + referencedCollection);
135143
reference.setRootObject(this.rootObject, collectionPrefix + referencedCollection + "." + this.path);
@@ -155,7 +163,12 @@ private String readFile(String testDataFolder, String collectionName) throws Col
155163
try {
156164
File targetFile = new File(testDataFolder + separator + collectionName + "." + this.extension);
157165
Properties properties = getProperties(targetFile);
158-
json = new PropertiesToJsonConverter().parseToJson(properties);
166+
json = new PropertiesToJsonConverter(
167+
new PrimitiveArrayJsonTypeResolver(arrayDelimiter),
168+
new ObjectFromTextJsonTypeResolver(),
169+
new BooleanJsonTypeResolver(),
170+
new StringJsonTypeResolver()
171+
).parseToJson(properties);
159172

160173
} catch (DataException ex) {
161174
throw new CollectionNotFoundException(String.format("File %s.%s not found in %s",

providers/properties-provider/src/test/java/ru/sbtqa/tag/datajack/providers/properties/PropertiesDataTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void setUp() {
3838
@Test
3939
public void differentExtensionTest() throws DataException {
4040
String collectionName = "Config";
41-
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "conf");
41+
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "conf", ",");
4242

4343
assertEquals("123qwe",
4444
dataProvider.get("Common.password2").getValue());
@@ -62,6 +62,15 @@ public void arrayTest() throws DataException {
6262
dataProvider.get("array[1].b").getValue());
6363
}
6464

65+
@Test
66+
public void notArrayTest() throws DataException {
67+
String collectionName = "DataBlocks";
68+
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "properties", "~");
69+
70+
assertEquals("a,b,c,2",
71+
dataProvider.get("Common.notArray").getValue());
72+
}
73+
6574
@Test
6675
public void deepArrayTest() throws DataException {
6776
String collectionName = "Tests";

providers/properties-provider/src/test/resources/properties/DataBlocks.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Common.password2.$ref = DataBlocks:Params Group 1.password
1313
Common.password2.comment = Пароль пользователя
1414
Common.cyclic.$ref = DataBlocks:Common.cyclic
1515
Common.cyclic.comment = Cyclic
16+
Common.notArray = a,b,c,2
1617

1718

1819
Params\ Group\ 1.login.$ref = Common.password

0 commit comments

Comments
 (0)