diff --git a/config/customer-groups.yaml b/config/customer-groups.yaml index e23f203..f06e9b5 100644 --- a/config/customer-groups.yaml +++ b/config/customer-groups.yaml @@ -1,10 +1,16 @@ customer-groups: # number of generated customer groups - elementCount: 15 - # range of customers per customer group + elementCount: 100 + + # range of customers per customer group - will be ignored if rules are present (see below) minCustomers: 5 maxCustomers: 50 # generate random custom attributes generatedAttributes: elementCount: 5 + + rules: + includeConditions: + - attributePath: session.sourceCodeGroup + operator: is-equal diff --git a/config/sourcecodes.yaml b/config/sourcecodes.yaml index b04fc9a..919d1d8 100644 --- a/config/sourcecodes.yaml +++ b/config/sourcecodes.yaml @@ -1,9 +1,9 @@ sourcecodes: # number of generated source code groups - elementCount: 10 + elementCount: 100 # range of codes per source code group minCodes: 1 - maxCodes: 10 + maxCodes: 1 # generate random custom attributes generatedAttributes: diff --git a/src/main/java/com/salesforce/comdagen/config/CustomerGroupConfiguration.kt b/src/main/java/com/salesforce/comdagen/config/CustomerGroupConfiguration.kt index bec8435..ce819da 100644 --- a/src/main/java/com/salesforce/comdagen/config/CustomerGroupConfiguration.kt +++ b/src/main/java/com/salesforce/comdagen/config/CustomerGroupConfiguration.kt @@ -8,6 +8,7 @@ package com.salesforce.comdagen.config import com.fasterxml.jackson.annotation.JsonRootName +import com.salesforce.comdagen.Configuration import com.salesforce.comdagen.ExtendableObjectConfig import com.salesforce.comdagen.RenderConfig @@ -17,17 +18,55 @@ data class CustomerGroupConfiguration( val maxCustomers: Int = 100, + override val customAttributes: Map? = null, override val generatedAttributes: GeneratedAttributeConfig? = null, override val elementCount: Int = 5, override val initialSeed: Long, + + val rules: CustomerGroupConditions? = null, + override val outputFilePattern: String = "generated.xml", override val outputDir: String = "", override val templateName: String = "customer-groups.ftlx" + ) : RenderConfig, ExtendableObjectConfig { init { require(maxCustomers >= minCustomers, { "maxCustomers needs to be greater equal minCustomers" }) } -} \ No newline at end of file + +} + +class CustomerGroupConditions(override val initialSeed: Long) : Configuration { + val includeConditions: List = listOf() + val excludeConditions: List = listOf() + override val elementCount: Int + get() = 2 + + override fun equals(other: Any?): Boolean{ + if (this === other) return true + if (other?.javaClass != javaClass) return false + + other as CustomerGroupConditions + + if (includeConditions != other.includeConditions) return false + if (excludeConditions != other.excludeConditions) return false + + return true + } + + + override fun hashCode(): Int { + return includeConditions.hashCode() + 31 * excludeConditions.hashCode() + } + +} +class CustomerGroupCondition( + val attributePath: String, + val operator: String) { + + val value: String? = null + +} diff --git a/src/main/java/com/salesforce/comdagen/generator/CustomerGroupGenerator.kt b/src/main/java/com/salesforce/comdagen/generator/CustomerGroupGenerator.kt index 78e920c..2d8d4a1 100644 --- a/src/main/java/com/salesforce/comdagen/generator/CustomerGroupGenerator.kt +++ b/src/main/java/com/salesforce/comdagen/generator/CustomerGroupGenerator.kt @@ -13,17 +13,21 @@ import com.salesforce.comdagen.config.CustomerGroupConfiguration import com.salesforce.comdagen.model.AttributeDefinition import com.salesforce.comdagen.model.CustomerGroup import com.salesforce.comdagen.model.GroupAssignment +import com.salesforce.comdagen.model.GroupCondition import java.util.* data class CustomerGroupGenerator( override val configuration: CustomerGroupConfiguration, - private val customerConfig: CustomerConfiguration + private val customerConfig: CustomerConfiguration, + private val sourceCodes: List ) : Generator { - override val creatorFunc = { _: Int, seed: Long -> CustomerGroup(seed, metadata["CustomerGroup"].orEmpty()) } + override val creatorFunc = { idx: Int, seed: Long -> CustomerGroup(idx, seed, metadata["CustomerGroup"].orEmpty(), includeConditions) } val assignments: Sequence get() { + if(configuration.rules != null) + return emptySequence() val groups = objects val rng = Random(configuration.initialSeed) return groups.flatMap { group -> @@ -35,6 +39,13 @@ data class CustomerGroupGenerator( (1..customerCount).asSequence().map { GroupAssignment(group.id, getRandomCustomer(rng.nextInt())) } } } + private val includeConditions: (Int) -> List? = { idx -> + configuration.rules?.includeConditions?.map { condition -> GroupCondition(condition.attributePath ,condition.operator, sourceCodes[idx]) } + } + val excludeConditions: List? + get() { + TODO() + } override val metadata: Map> = mapOf( "CustomerGroup" to configuration.attributeDefinitions("CustomerGroup") diff --git a/src/main/java/com/salesforce/comdagen/model/CustomerGroup.kt b/src/main/java/com/salesforce/comdagen/model/CustomerGroup.kt index 9ddcfec..779a52a 100644 --- a/src/main/java/com/salesforce/comdagen/model/CustomerGroup.kt +++ b/src/main/java/com/salesforce/comdagen/model/CustomerGroup.kt @@ -10,13 +10,15 @@ package com.salesforce.comdagen.model import com.salesforce.comdagen.RandomData import java.util.* -class CustomerGroup(private val seed: Long, private val attributeDefinitions: Set) { +class CustomerGroup(private val idx: Int, private val seed: Long, private val attributeDefinitions: Set, includeConditions: (Int) -> List?) { val id: String get() = "comdagen-${Math.abs(seed)}" val description: String get() = RandomData.getRandomSentence(seed + "customerGroupDescription".hashCode()) + val conditions: List? = includeConditions(idx.toInt()) + val customAttributes: List get() { val rng = Random(seed + "customAttributes".hashCode()) @@ -24,4 +26,5 @@ class CustomerGroup(private val seed: Long, private val attributeDefinitions: Se } } +data class GroupCondition(val attributePath: String, val operator: String, val value: String) data class GroupAssignment(val groupId: String, val customerId: Int) diff --git a/src/main/java/com/salesforce/comdagen/model/Site.kt b/src/main/java/com/salesforce/comdagen/model/Site.kt index 0548ec9..48741d3 100644 --- a/src/main/java/com/salesforce/comdagen/model/Site.kt +++ b/src/main/java/com/salesforce/comdagen/model/Site.kt @@ -142,9 +142,15 @@ class Site( else null + val sourceCodeGenerator: SourceCodeGenerator? = + if (sourceCodeConfig != null) + SourceCodeGenerator(sourceCodeConfig) + else + null + val customerGroupGenerator: CustomerGroupGenerator? = - if (customerGroupConfig != null && customerConfig != null) - CustomerGroupGenerator(customerGroupConfig, customerConfig) + if (customerGroupConfig != null && customerConfig != null && sourceCodeConfig != null) + CustomerGroupGenerator(customerGroupConfig, customerConfig, sourceCodeGenerator?.objects?.toList()?.map { it.id } ?: kotlin.collections.emptyList()) else null @@ -154,12 +160,6 @@ class Site( else null - val sourceCodeGenerator: SourceCodeGenerator? = - if (sourceCodeConfig != null) - SourceCodeGenerator(sourceCodeConfig) - else - null - val promotionGenerator: PromotionGenerator? = if (promotionConfig != null && catalogGenerator != null) PromotionGenerator(promotionConfig, catalogGenerator.objects.first(), customerGroups = customerGroupGenerator?.objects?.map { it.id }?.toList() ?: emptyList(), diff --git a/src/test/java/com/salesforce/comdagen/CustomerGroupTest.kt b/src/test/java/com/salesforce/comdagen/CustomerGroupTest.kt index 4f4fa4f..f6934b2 100644 --- a/src/test/java/com/salesforce/comdagen/CustomerGroupTest.kt +++ b/src/test/java/com/salesforce/comdagen/CustomerGroupTest.kt @@ -1,11 +1,9 @@ package com.salesforce.comdagen -import com.salesforce.comdagen.config.AttributeConfig -import com.salesforce.comdagen.config.CustomerConfiguration -import com.salesforce.comdagen.config.CustomerGroupConfiguration -import com.salesforce.comdagen.config.GeneratedAttributeConfig +import com.salesforce.comdagen.config.* import com.salesforce.comdagen.generator.CustomerGroupGenerator import org.junit.Test +import java.util.* import kotlin.test.assertEquals import kotlin.test.assertNotEquals import kotlin.test.assertTrue @@ -21,8 +19,9 @@ class CustomerGroupTest { val elementCount = 30 val customerConfig = CustomerConfiguration(initialSeed = seed) val customerGroupConfig = CustomerGroupConfiguration(elementCount = elementCount, initialSeed = seed) + val sourceCodeConfig = SourceCodeConfiguration(initialSeed = seed) val customerGroupGenerator = - CustomerGroupGenerator(configuration = customerGroupConfig, customerConfig = customerConfig) + CustomerGroupGenerator(configuration = customerGroupConfig, customerConfig = customerConfig, sourceCodes = Collections.emptyList()) assertEquals(elementCount, customerGroupGenerator.objects.count()) } @@ -37,8 +36,9 @@ class CustomerGroupTest { minCustomers = minCustomers, maxCustomers = maxCustomers, elementCount = elementCount, initialSeed = seed ) + val sourceCodeConfig = SourceCodeConfiguration(initialSeed = seed) val customerGroupGenerator = - CustomerGroupGenerator(configuration = customerGroupConfig, customerConfig = customerConfig) + CustomerGroupGenerator(configuration = customerGroupConfig, customerConfig = customerConfig, sourceCodes = Collections.emptyList()) customerGroupGenerator.objects.forEach { group -> val assignmentCount: Int = customerGroupGenerator.assignments.count { it.groupId == group.id } @@ -63,9 +63,11 @@ class CustomerGroupTest { val customAttribute: Map = mapOf(name to attributeConfig) val customerGroupConfig = CustomerGroupConfiguration(customAttributes = customAttribute, initialSeed = seed) + val sourceCodeConfig = SourceCodeConfiguration(initialSeed = seed) val customerGroupGenerator = CustomerGroupGenerator( configuration = customerGroupConfig, - customerConfig = CustomerConfiguration(initialSeed = seed) + customerConfig = CustomerConfiguration(initialSeed = seed), + sourceCodes = Collections.emptyList() ) assertEquals(customAttribute.values.size, customerGroupGenerator.metadata.values.sumBy { it.size }) @@ -85,9 +87,11 @@ class CustomerGroupTest { val elementCount = 15 val customerGroupConfig = CustomerGroupConfiguration(generatedAttributes = GeneratedAttributeConfig(elementCount), initialSeed = seed) + val sourceCodeConfiguration = SourceCodeConfiguration(initialSeed = seed) val customerGroupGenerator = CustomerGroupGenerator( configuration = customerGroupConfig, - customerConfig = CustomerConfiguration(initialSeed = seed) + customerConfig = CustomerConfiguration(initialSeed = seed), + sourceCodes = Collections.emptyList() ) customerGroupGenerator.objects.forEach { group -> diff --git a/src/test/java/com/salesforce/comdagen/PromotionTest.kt b/src/test/java/com/salesforce/comdagen/PromotionTest.kt index b4a7bf3..16005f2 100644 --- a/src/test/java/com/salesforce/comdagen/PromotionTest.kt +++ b/src/test/java/com/salesforce/comdagen/PromotionTest.kt @@ -7,6 +7,7 @@ import com.salesforce.comdagen.model.OrderPromotion import com.salesforce.comdagen.model.ProductPromotion import com.salesforce.comdagen.model.ShippingPromotion import org.junit.Test +import java.util.* import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -431,7 +432,8 @@ class PromotionTest { val customerConfig = CustomerConfiguration(initialSeed = seed) val customerGroupConfig = CustomerGroupConfiguration(initialSeed = seed) - val customerGroupGenerator = CustomerGroupGenerator(customerGroupConfig, customerConfig) + val sourceCodeConfiguration = SourceCodeConfiguration(initialSeed = seed) + val customerGroupGenerator = CustomerGroupGenerator(customerGroupConfig, customerConfig, sourceCodes = Collections.emptyList()) val customerGroupIds = customerGroupGenerator.objects.map { it.id }.toList() .plus(listOf("Everyone", "Registered", "Unregistered")) diff --git a/src/test/java/com/salesforce/comdagen/SchemaVerificationTest.kt b/src/test/java/com/salesforce/comdagen/SchemaVerificationTest.kt index ea4cefe..10c9396 100644 --- a/src/test/java/com/salesforce/comdagen/SchemaVerificationTest.kt +++ b/src/test/java/com/salesforce/comdagen/SchemaVerificationTest.kt @@ -10,6 +10,7 @@ import org.xmlunit.validation.Languages import java.io.File import java.nio.file.Files import java.nio.file.StandardCopyOption +import java.util.* import javax.xml.transform.stream.StreamSource import javax.xml.validation.SchemaFactory import javax.xml.validation.Validator @@ -218,7 +219,7 @@ class SchemaVerificationTest { fun testCustomerGroupValid() { val customerConfig = CustomerConfiguration(initialSeed = seed) val customerGroupConfig = CustomerGroupConfiguration(initialSeed = seed) - val customerGroupGenerator = CustomerGroupGenerator(customerGroupConfig, customerConfig) + val customerGroupGenerator = CustomerGroupGenerator(customerGroupConfig, customerConfig, sourceCodes = Collections.emptyList()) val v = getValidator("/schema/customergroup.xsd") @@ -309,7 +310,7 @@ class SchemaVerificationTest { val catalog = CatalogGenerator(CatalogListConfiguration(initialSeed = seed)).objects.first() val customerGroupIds = CustomerGroupGenerator( CustomerGroupConfiguration(initialSeed = seed), - CustomerConfiguration(initialSeed = seed) + CustomerConfiguration(initialSeed = seed), sourceCodes = Collections.emptyList() ).objects.map { it.id }.toList() val shippingMethods = ShippingGenerator(ShippingConfiguration(initialSeed = seed)).objects.map { it.id }.toList() diff --git a/templates/customer-groups.ftlx b/templates/customer-groups.ftlx index 296fec4..5ad4516 100644 --- a/templates/customer-groups.ftlx +++ b/templates/customer-groups.ftlx @@ -4,6 +4,21 @@ <#list gen.objects as group> ${group.description} + <#if group.conditions?has_content> + + + + <#list group.conditions as condition> + + ${condition.attributePath} + ${condition.operator} + ${condition.value} + + + + + + <#if group.customAttributes?has_content> <#list group.customAttributes as attribute>