From ff5f9f12b9d018b173efc33a3823787f38d269fb Mon Sep 17 00:00:00 2001 From: styluo Date: Mon, 27 Nov 2023 11:02:47 +0800 Subject: [PATCH] feat: XEI (#40) --- .../sdk/java/dto/GioCdpEventMessage.java | 10 +++-- .../sdk/java/test/Case0PropertiesTest.java | 2 +- .../sdk/java/test/Case1MockHttpTest.java | 41 ++++++++++++++++++- .../sdk/java/test/Case3EventMessageTest.java | 11 +++-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/growing/sdk/java/dto/GioCdpEventMessage.java b/src/main/java/io/growing/sdk/java/dto/GioCdpEventMessage.java index 880d5a3..e938b26 100644 --- a/src/main/java/io/growing/sdk/java/dto/GioCdpEventMessage.java +++ b/src/main/java/io/growing/sdk/java/dto/GioCdpEventMessage.java @@ -19,6 +19,8 @@ public class GioCdpEventMessage extends GioCDPMessage implements Ser private static final long serialVersionUID = -2414503426226355459L; + public static final String XEI_USER_KEY = "$notuser"; + private final EventV3Dto event; private GioCdpEventMessage(EventV3Dto.Builder builder) { @@ -49,9 +51,11 @@ public boolean isIllegal() { return true; } - if (StringUtils.isBlank(event.getUserId()) && StringUtils.isBlank(event.getDeviceId())) { - GioLogger.error("GioCdpEventMessage: userId and anonymousId are empty"); - return true; + if (!XEI_USER_KEY.equals(event.getUserKey())) { + if (StringUtils.isBlank(event.getUserId()) && StringUtils.isBlank(event.getDeviceId())) { + GioLogger.error("GioCdpEventMessage: userId and anonymousId are empty"); + return true; + } } return false; diff --git a/src/test/java/io/growing/sdk/java/test/Case0PropertiesTest.java b/src/test/java/io/growing/sdk/java/test/Case0PropertiesTest.java index 4035fc9..4123aaf 100644 --- a/src/test/java/io/growing/sdk/java/test/Case0PropertiesTest.java +++ b/src/test/java/io/growing/sdk/java/test/Case0PropertiesTest.java @@ -33,7 +33,7 @@ public void checkProperties() { Properties properties = new Properties(); - properties.setProperty("api.host", "http://localhost:8080"); + properties.setProperty("api.host", "https://www.growingio.com"); properties.setProperty("project.id", "123456654321"); properties.setProperty("msg.store.strategy", "abortPolicy"); properties.setProperty("run.mode", "production"); diff --git a/src/test/java/io/growing/sdk/java/test/Case1MockHttpTest.java b/src/test/java/io/growing/sdk/java/test/Case1MockHttpTest.java index 4a22e47..d83da9d 100644 --- a/src/test/java/io/growing/sdk/java/test/Case1MockHttpTest.java +++ b/src/test/java/io/growing/sdk/java/test/Case1MockHttpTest.java @@ -36,7 +36,7 @@ public static void before() { setStaticField(RunMode.class, "currentMode", null); Properties properties = new Properties(); properties.setProperty("run.mode", "production"); - properties.setProperty("api.host", "http://117.50.105.254:8080"); + properties.setProperty("api.host", "https://www.growingio.com"); GrowingAPI.initConfig(properties); sender = new GrowingAPI.Builder().setDataSourceId(DATASOURCE_ID).setProjectKey(PROJECT_KEY).build(); factory = new StubStreamHandlerFactory(); @@ -68,6 +68,45 @@ public void afterTest() { } } + @Test + public void sendXEIEvent() throws InterruptedException { + final CountDownLatch countDownLatch = new CountDownLatch(1); + + factory.setStubHttpURLConnectionListener(new StubStreamHandlerFactory.StubHttpURLConnectionListener() { + @Override + public void onSend(URL url, byte[] msg) { + try { + EventV3List eventList = EventV3List.parseFrom(msg); + EventV3Dto customEvent = eventList.getValues(0); + Assert.assertEquals(DATASOURCE_ID, customEvent.getDataSourceId()); + Assert.assertEquals(PROJECT_KEY, customEvent.getProjectKey()); + + Assert.assertEquals("CUSTOM", customEvent.getEventType().name()); + Assert.assertEquals("payOrder", customEvent.getEventName()); + + Assert.assertEquals(GioCdpEventMessage.XEI_USER_KEY, customEvent.getUserKey()); + Map attributes = customEvent.getAttributesMap(); + Assert.assertEquals("0001", attributes.get("prod_id")); + Assert.assertEquals("15.52", attributes.get("money")); + + } catch (Exception e) { + mException = e; + } + + countDownLatch.countDown(); + } + }); + + sender.send(new GioCdpEventMessage.Builder() + .eventKey("payOrder") + .loginUserKey(GioCdpEventMessage.XEI_USER_KEY) + .addEventVariable("prod_id", "0001") + .addEventVariable("money", "15.52") + .build()); + + countDownLatch.await(); + } + @Test public void sendCustomEvent() throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1); diff --git a/src/test/java/io/growing/sdk/java/test/Case3EventMessageTest.java b/src/test/java/io/growing/sdk/java/test/Case3EventMessageTest.java index ae869f9..097d26e 100644 --- a/src/test/java/io/growing/sdk/java/test/Case3EventMessageTest.java +++ b/src/test/java/io/growing/sdk/java/test/Case3EventMessageTest.java @@ -1,9 +1,6 @@ package io.growing.sdk.java.test; -import io.growing.sdk.java.dto.GioCdpEventMessage; -import io.growing.sdk.java.dto.GioCdpItemMessage; -import io.growing.sdk.java.dto.GioCdpUserMappingMessage; -import io.growing.sdk.java.dto.GioCdpUserMessage; +import io.growing.sdk.java.dto.*; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,6 +23,12 @@ public void checkCustomMessage() { .build(); Assert.assertFalse(eventMessage.isIllegal()); + eventMessage = new GioCdpEventMessage.Builder() + .eventKey("event_name") + .loginUserKey(GioCdpEventMessage.XEI_USER_KEY) + .build(); + Assert.assertFalse(eventMessage.isIllegal()); + eventMessage = new GioCdpEventMessage.Builder() .eventKey("event_name") .build();