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

Commit

Permalink
Merge pull request #34 from sbtqa/feature/custom-array-delimiter
Browse files Browse the repository at this point in the history
Add custom value to array delimiter
  • Loading branch information
clicman authored May 24, 2019
2 parents 6fe4a31 + d6ee167 commit 5347833
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.mongodb.BasicDBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.jalokim.propertiestojson.resolvers.primitives.BooleanJsonTypeResolver;
import pl.jalokim.propertiestojson.resolvers.primitives.ObjectFromTextJsonTypeResolver;
import pl.jalokim.propertiestojson.resolvers.primitives.PrimitiveArrayJsonTypeResolver;
import pl.jalokim.propertiestojson.resolvers.primitives.StringJsonTypeResolver;
import pl.jalokim.propertiestojson.util.PropertiesToJsonConverter;
import ru.sbtqa.tag.datajack.TestDataProvider;
import ru.sbtqa.tag.datajack.exceptions.CollectionNotFoundException;
Expand All @@ -26,6 +30,7 @@ public class PropertiesDataProvider extends AbstractDataProvider {
private static final String DEFAULT_EXTENSION = "properties";
private static final String REF_TPL = "$ref";
private final String extension;
private String arrayDelimiter = ",";
private String testDataFolder;

/**
Expand All @@ -50,12 +55,13 @@ public PropertiesDataProvider(String testDataFolder, String collectionName) thro
*
* @param testDataFolder path to data folder
* @param collectionName properties file name
* @param extension custom file extension
* @param extension custom file extension
* @param arrayDelimiter custom value array delimiter
* @throws DataException if file not found in testDataFolder
*/
public PropertiesDataProvider(String testDataFolder, String collectionName, String extension) throws DataException {
public PropertiesDataProvider(String testDataFolder, String collectionName, String extension, String arrayDelimiter) throws DataException {
this.extension = extension;

this.arrayDelimiter = arrayDelimiter;
String json = readFile(testDataFolder, collectionName);

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

private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String extension) {
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String extension, String arrayDelimiter) {
this.extension = extension;
this.testDataFolder = testDataFolder;
this.basicObject = obj;
this.collectionName = collectionName;
this.arrayDelimiter=arrayDelimiter;
}

private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String way, String extension) {
private PropertiesDataProvider(String testDataFolder, BasicDBObject obj, String collectionName, String way, String extension, String arrayDelimiter) {
this.extension = extension;
this.testDataFolder = testDataFolder;
this.basicObject = obj;
this.way = way;
this.collectionName = collectionName;
this.arrayDelimiter=arrayDelimiter;
}

/**
* {@inheritDoc}
*/
@Override
protected PropertiesDataProvider createInstance(String collectionName) throws DataException {
return new PropertiesDataProvider(testDataFolder, collectionName, extension);
return new PropertiesDataProvider(testDataFolder, collectionName, extension, arrayDelimiter);
}

/**
* {@inheritDoc}
*/
@Override
protected PropertiesDataProvider createInstance(BasicDBObject obj, String collectionName, String way) {
return new PropertiesDataProvider(testDataFolder, obj, collectionName, way, extension);
return new PropertiesDataProvider(testDataFolder, obj, collectionName, way, extension, arrayDelimiter);
}

/**
* {@inheritDoc}
*/
@Override
protected PropertiesDataProvider createInstance(BasicDBObject obj, String collectionName) {
return new PropertiesDataProvider(testDataFolder, obj, collectionName, extension);
return new PropertiesDataProvider(testDataFolder, obj, collectionName, extension, arrayDelimiter);
}

/**
Expand Down Expand Up @@ -129,7 +137,7 @@ public TestDataProvider getReference() throws DataException {
}
String refValue = this.basicObject.getString(REF_TPL);
String referencedCollection = refValue.contains(":") ? refValue.split(":")[0] : this.collectionName;
String collectionPrefix = refValue.startsWith("/") ? "" :this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
String collectionPrefix = refValue.startsWith("/") ? "" : this.collectionName.substring(0, this.collectionName.lastIndexOf("/") + 1);
this.path = refValue.contains(":") ? refValue.split(":")[1] : refValue;
AbstractDataProvider reference = (AbstractDataProvider) this.fromCollection(collectionPrefix + referencedCollection);
reference.setRootObject(this.rootObject, collectionPrefix + referencedCollection + "." + this.path);
Expand All @@ -155,7 +163,12 @@ private String readFile(String testDataFolder, String collectionName) throws Col
try {
File targetFile = new File(testDataFolder + separator + collectionName + "." + this.extension);
Properties properties = getProperties(targetFile);
json = new PropertiesToJsonConverter().parseToJson(properties);
json = new PropertiesToJsonConverter(
new PrimitiveArrayJsonTypeResolver(arrayDelimiter),
new ObjectFromTextJsonTypeResolver(),
new BooleanJsonTypeResolver(),
new StringJsonTypeResolver()
).parseToJson(properties);

} catch (DataException ex) {
throw new CollectionNotFoundException(String.format("File %s.%s not found in %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setUp() {
@Test
public void differentExtensionTest() throws DataException {
String collectionName = "Config";
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "conf");
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "conf", ",");

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

@Test
public void notArrayTest() throws DataException {
String collectionName = "DataBlocks";
TestDataProvider dataProvider = new PropertiesDataProvider(this.propertiesDataPath, collectionName, "properties", "~");

assertEquals("a,b,c,2",
dataProvider.get("Common.notArray").getValue());
}

@Test
public void deepArrayTest() throws DataException {
String collectionName = "Tests";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Common.password2.$ref = DataBlocks:Params Group 1.password
Common.password2.comment = Пароль пользователя
Common.cyclic.$ref = DataBlocks:Common.cyclic
Common.cyclic.comment = Cyclic
Common.notArray = a,b,c,2


Params\ Group\ 1.login.$ref = Common.password
Expand Down

0 comments on commit 5347833

Please sign in to comment.