Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute field type validation #7

Merged
merged 6 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The `@AnalyticsEvent` annotation flags a class as the one to be processed by `An
Annotations for class fields:

* `@KeyPath` – use this annotation if you want to format your `actionKey` at runtime
* `@Attribute` - the annotation `value` and field value will form a key-value pair in the `data` map that the tracker recieves
* `@Attribute` - the annotation `value` and field value will form a key-value pair in the `data` map that the tracker receives. Accepts primitives, String, Boolean and boxed numbers
* `@AttributeMap` – sometimes, it might be easier to form a map than to create a [big] number of annotated fields. These map contents will be merged with attributes.

For more info please see the `sample` project code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class BuyPetEvent implements BaseAnalyticsAction {
@Attribute("pet_birth_date")
String petBirthDate;

@Attribute("pet_gender")
String petGender = "female";

@AttributeMap
Map<String, Object> data = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class BuyPetEventKt(petEntity: PetEntity) : BaseAnalyticsAction { // kotlin clas
@Attribute("pet_birth_date")
val petBirthDate: String = DateFormat.getDateInstance().format(petEntity.birthDate.time)

@Attribute("pet_gender")
val gender: String = "female"

@AttributeMap
val data: HashMap<String, Any> = HashMap()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.techery.analytics.sample.event;

import org.junit.Test;

import java.util.Calendar;

import io.techery.analytics.sample.BaseTest;
import io.techery.analytics.sample.utils.MapMatcher;
import io.techery.analytics.sample_common.entity.PetEntity;
import io.techery.analytics.sample_common.entity.PetType;

import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

public class BuyPetEventKtTest extends BaseTest {

@Test
public void eventKotlinSentWithCorrectData() {
Calendar petBirthDate = Calendar.getInstance();
petBirthDate.set(2015, 4, 13); // formatted date will be "May 13, 2015"
PetEntity pet = new PetEntity(PetType.DOG, "Moohtar", petBirthDate);
BuyPetEventKt event = new BuyPetEventKt(pet);

analyticsPipe.send(event);
verify(tracker).trackEvent(eq("user_bought_pet:dog:mall"),
argThat(MapMatcher.builder()
.with("pet_birth_date", "May 13, 2015")
.with("pet_name", "Moohtar")
.with("pet_gender", "female")
.build()
));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.techery.analytics.sample.event;

import io.techery.analytics.sample.BaseTest;
import io.techery.analytics.sample.utils.MapAssert;
import io.techery.analytics.sample.utils.MapMatcher;
import io.techery.analytics.sample_common.entity.PetEntity;
import io.techery.analytics.sample_common.entity.PetType;
import org.junit.Test;
Expand All @@ -16,34 +18,17 @@ public class BuyPetEventTest extends BaseTest {
@Test
public void eventSentWithCorrectData() {
Calendar petBirthDate = Calendar.getInstance();
petBirthDate.set(2015, 4, 13); // formatted date will be "May 13, 2015
petBirthDate.set(2015, 4, 13); // formatted date will be "May 13, 2015"
PetEntity pet = new PetEntity(PetType.DOG, "Moohtar", petBirthDate);
BuyPetEvent event = new BuyPetEvent(pet);

analyticsPipe.send(event);
verify(tracker).trackEvent(eq("user_bought_pet:dog:mall"),
argThat(argument ->
argument.containsKey("pet_birth_date") &&
argument.get("pet_birth_date").equals("May 13, 2015") &&
argument.containsKey("pet_name") &&
argument.get("pet_name").equals("Moohtar")
));
}

@Test
public void eventKotlinSentWithCorrectData() {
Calendar petBirthDate = Calendar.getInstance();
petBirthDate.set(2015, 4, 13); // formatted date will be "May 13, 2015
PetEntity pet = new PetEntity(PetType.DOG, "Moohtar", petBirthDate);
BuyPetEventKt event = new BuyPetEventKt(pet);

analyticsPipe.send(event);
verify(tracker).trackEvent(eq("user_bought_pet:dog:mall"),
argThat(argument ->
argument.containsKey("pet_birth_date") &&
argument.get("pet_birth_date").equals("May 13, 2015") &&
argument.containsKey("pet_name") &&
argument.get("pet_name").equals("Moohtar")
argThat(MapMatcher.builder()
.with("pet_birth_date", "May 13, 2015")
.with("pet_name", "Moohtar")
.with("pet_gender", "female")
.build()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import io.techery.analytics.sample.BaseTest;
import io.techery.analytics.sample.utils.MapMatcher;

import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -18,11 +19,9 @@ public void successorEventSentWithCorrectData() {

analyticsPipe.send(event);
verify(tracker).trackEvent(eq("event_with_superclass:SuccessorEvent"),
argThat(argument ->
argument.containsKey("successor_attribute") &&
argument.get("successor_attribute").equals("SUCCESSOR") &&
argument.containsKey("attribute_from_parent") &&
argument.get("attribute_from_parent").equals("PARENT")
));
argThat(MapMatcher.builder()
.with("successor_attribute", successorAttribute)
.with("attribute_from_parent", parentAttribute)
.build()));
}
}
Loading