diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/Util.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/Util.groovy index 8bf6443..1cdb2ce 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/Util.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/Util.groovy @@ -31,10 +31,24 @@ class Util { fileName.replaceFirst(PATTERN_FILE_EXT, '') } + /** + * Gets a developer name of full name + * @param fullName is a tag of custom field + * @return developerName of custom field + */ public static String getDeveloperName(String fullName){ return fullName.substring(fullName.indexOf('.') + 1, fullName.length() - 7) } + /** + * Gets a developerName of member + * @param member is member tag of package xml file + * @return developerName of custom field + */ + public static String getDeveloperNameByMember(String member) { + return member.substring(member.indexOf('.') + 1, member.length() - 3) + } + /** * Verifies if the property exist and if it is not empty string * @param name the property typeName @@ -164,7 +178,7 @@ class Util { public static ArrayList getInvalidFolders(ArrayList foldersName) { ArrayList invalidFolders = new ArrayList() foldersName.each { String folderName -> - if (!MetadataComponents.validFolder(folderName)) { + if (!MetadataComponents.validFolder(folderName) || folderName.contains('.')) { invalidFolders.push(folderName) } } @@ -180,8 +194,10 @@ class Util { ArrayList emptyFolders = new ArrayList() foldersName.each { String folderName -> File file = new File(Paths.get(projectPath, folderName).toString()) - if (file.exists() && file.list().length == 0) { - emptyFolders.push(folderName) + if (file.isDirectory()) { + if (file.exists() && file.list().length == 0 ) { + emptyFolders.push(folderName) + } } } return emptyFolders diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/salesforce/MetadataComponents.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/salesforce/MetadataComponents.groovy index 174b332..62dfec4 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/salesforce/MetadataComponents.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/utils/salesforce/MetadataComponents.groovy @@ -112,6 +112,11 @@ public enum MetadataComponents { return COMPONENT.get(name.toUpperCase()); } + /** + * Gets a MetadataComponent by folder + * @param folder is a component folder + * @return a MetadataComponent + */ public static MetadataComponents getComponentByFolder(String folder) { MetadataComponents metadataComponent @@ -124,6 +129,28 @@ public enum MetadataComponents { return metadataComponent } + /** + * Gets a MetadataComponent by name + * @param name is component name + * @return a MetadataComponent object + */ + public static MetadataComponents getComponentByName(String name) { + + MetadataComponents metadataComponent + for (MetadataComponents component : values()) { + if (component.getTypeName() == name) { + metadataComponent = component + break + } + } + return metadataComponent + } + + /** + * Gets a extension of component by folder + * @param folder is a component folder + * @return a extension of component + */ public static String getExtensionByFolder(String folder) { String extensionByFolder @@ -136,6 +163,11 @@ public enum MetadataComponents { return extensionByFolder } + /** + * Gets a extension of component by name + * @param name is component name + * @return a extension o component + */ public static String getExtensionByName(String name) { String extensionByName @@ -148,6 +180,11 @@ public enum MetadataComponents { return extensionByName } + /** + * Gets a directory of component by name + * @param name is a component name + * @return a directory of component + */ public static String getDirectoryByName(String name) { String directory @@ -160,6 +197,11 @@ public enum MetadataComponents { return directory } + /** + * Validates a component extension + * @param extension is a component extension + * @return true if extension is valid + */ public static boolean validExtension(String extension) { for (MetadataComponents input : values()) { if (input.getExtension() == extension) { @@ -169,6 +211,11 @@ public enum MetadataComponents { return false } + /** + * Validates a component folder + * @param folderName is a component folder + * @return true if folder is valid + */ public static boolean validFolder(String folderName) { for (MetadataComponents input : values()) { if (input.name() == folderName.toUpperCase()) { diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/HttpAPIClient.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/HttpAPIClient.groovy index 0879859..d1bfdb1 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/HttpAPIClient.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/HttpAPIClient.groovy @@ -47,7 +47,6 @@ class HttpAPIClient { * @return the result from server in JSON format */ public String executeQuery(String soql) { - String resultQuery = "" HTTPBuilder http = new HTTPBuilder(getEndPoint(host)) http.request( GET, JSON ) { diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilder.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilder.groovy index 339a88e..013cce3 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilder.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilder.groovy @@ -5,6 +5,7 @@ package org.fundacionjala.gradle.plugins.enforce.wsc.rest +import com.sforce.soap.metadata.PackageTypeMembers import groovy.util.logging.Log import org.fundacionjala.gradle.plugins.enforce.utils.Constants import org.fundacionjala.gradle.plugins.enforce.utils.Util @@ -25,6 +26,7 @@ class QueryBuilder { public static final ArrayList defaultComponents = ['ApexClass', 'ApexComponent', 'ApexPage', 'ApexTrigger', 'StaticResource', 'Profile', 'EmailTemplate', 'CustomField', 'CompactLayout', 'RecordType','ValidationRule'] + public static final ArrayList defaultSubComponents = ['CustomField', 'CompactLayout', 'RecordType','ValidationRule'] /** * Gets queries of components from package xml file * @param packagePath is type String @@ -35,9 +37,18 @@ class QueryBuilder { throw new Exception("${THERE_IS_NOT_PACKAGE}${packagePath}") } ArrayList queries = new ArrayList() - getComponents(new FileReader(packagePath)).each { component -> - if (isDefaultComponent(component)) { - queries.add("${'SELECT Name FROM'} ${component}") + getComponents(new FileReader(packagePath)).each { typeMembers -> + if(defaultSubComponents.contains(typeMembers.name)) { + typeMembers.members.each { member -> + if (member != '*') { + queries.add("""${SELECT_FULL_NAME} ${typeMembers.name} ${WHERE_FULL_NAME} '${ + Util.getDeveloperNameByMember(member) + }'""") + } + } + } else + if (isDefaultComponent(typeMembers.name)) { + queries.add("${SELECT_NAME} ${typeMembers.name}") } } return queries @@ -90,14 +101,10 @@ class QueryBuilder { * @param reader is type Reader * @return arrayList with sales force's components */ - public ArrayList getComponents(Reader reader) { + public ArrayList getComponents(Reader reader) { PackageBuilder packageBuilder = new PackageBuilder() packageBuilder.read(reader) - ArrayList components = new ArrayList() - packageBuilder.metaPackage.types.each { type -> - components.add(type.name) - } - return components + return packageBuilder.metaPackage.types } /** diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/utils/UtilTest.groovy b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/utils/UtilTest.groovy index c2a6188..179f4bf 100644 --- a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/utils/UtilTest.groovy +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/utils/UtilTest.groovy @@ -141,6 +141,13 @@ class UtilTest extends Specification { developerName == 'AuthTokenAPI' } + def "Test should get developerName from a member" () { + when: + def developerName = Util.getDeveloperNameByMember('TwilioConfig__c.AuthTokenAPI__c') + then: + developerName == 'AuthTokenAPI' + } + def cleanupSpec() { new File(Paths.get(resourcesPath, 'triggers').toString()).deleteDir() } diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/resources/packageWithFields.xml b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/resources/packageWithFields.xml new file mode 100644 index 0000000..25cffac --- /dev/null +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/resources/packageWithFields.xml @@ -0,0 +1,18 @@ + + + * + ApexClass + + + Component1__c.component + Component2__c.component + ApexComponent + + + Object5__c.Field11__c + Object5__c.Field22__c + Object5__c.Field33__c + CustomField + + 32.0 + diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilderTest.groovy b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilderTest.groovy index 12b92de..3f1c62a 100644 --- a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilderTest.groovy +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/wsc/rest/QueryBuilderTest.groovy @@ -42,8 +42,30 @@ class QueryBuilderTest extends Specification { def packagePath = Paths.get(wscPath, 'resources', 'packageTest.xml').toString() when: def componentsObtained = queryBuilder.getComponents(new FileReader(packagePath)) + def componentsName = new ArrayList() + componentsObtained.each { component -> + componentsName.add(component.name) + } + componentsName = componentsName.sort() then: - componentsObtained.sort() == ['ApexClass', 'ApexComponent', 'ApexPage', 'ApexTrigger', 'CustomObject', 'StaticResource'].sort() + componentsName.get(0) == 'ApexClass' + componentsName.get(1) == 'ApexComponent' + componentsName.get(2) == 'ApexPage' + componentsName.get(3) == 'ApexTrigger' + componentsName.get(4) == 'CustomObject' + componentsName.get(5) == 'StaticResource' + } + + def "Test should return an array with queries that have fields" () { + given: + def packagePath = Paths.get(wscPath, 'resources', 'packageWithFields.xml').toString() + when: + def queries = queryBuilder.createQueryFromPackage(packagePath) + then: + queries.sort() == ['SELECT Name FROM ApexClass', 'SELECT Name FROM ApexComponent', + "${'SELECT FullName FROM CustomField WHERE DeveloperName = '}${"'Field11'"}", + "${'SELECT FullName FROM CustomField WHERE DeveloperName = '}${"'Field22'"}", + "${'SELECT FullName FROM CustomField WHERE DeveloperName = '}${"'Field33'"}",].sort() } def "Test should return an array with queries" () {