1+ package io .getstream .client ;
2+
3+ import io .getstream .core .LookupKind ;
4+ import io .getstream .core .models .Activity ;
5+ import io .getstream .core .models .FeedID ;
6+ import io .getstream .core .models .Reaction ;
7+ import org .junit .Test ;
8+
9+ import java .util .Date ;
10+ import java .util .HashMap ;
11+ import java .util .List ;
12+ import java .util .Map ;
13+ import java .util .UUID ;
14+
15+ import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertNotNull ;
17+ import static org .junit .Assert .assertTrue ;
18+
19+ public class TargetFeedsExtraDataTest {
20+ private static final String apiKey =
21+ System .getenv ("STREAM_KEY" ) != null
22+ ? System .getenv ("STREAM_KEY" )
23+ : System .getProperty ("STREAM_KEY" );
24+ private static final String secret =
25+ System .getenv ("STREAM_SECRET" ) != null
26+ ? System .getenv ("STREAM_SECRET" )
27+ : System .getProperty ("STREAM_SECRET" );
28+
29+ @ Test
30+ public void testTargetFeedsExtraData () throws Exception {
31+ // Create client
32+ Client client = Client .builder (apiKey , secret ).build ();
33+
34+ // Use unique user id to avoid conflicts in notification feed
35+ String uniqueId = UUID .randomUUID ().toString ().replace ("-" , "" );
36+ String userId = "test-user-" + uniqueId ;
37+
38+ // 1. Create a test activity
39+ String activityId = UUID .randomUUID ().toString ();
40+ Activity activity = Activity .builder ()
41+ .actor (userId )
42+ .verb ("post" )
43+ .object ("test-object" )
44+ .foreignID ("test-foreignId-" + activityId )
45+ .time (new Date ())
46+ .build ();
47+
48+ Activity postedActivity = client .flatFeed ("user" , userId ).addActivity (activity ).join ();
49+
50+ // 2. Create a comment reaction on the activity
51+ Map <String , Object > commentData = new HashMap <>();
52+ commentData .put ("text" , "This is a test comment" );
53+
54+ Reaction comment = Reaction .builder ()
55+ .kind ("comment" )
56+ .activityID (postedActivity .getID ())
57+ .extraField ("data" , commentData )
58+ .build ();
59+
60+ Reaction postedComment = client .reactions ().add (userId , comment , new FeedID [0 ]).join ();
61+
62+ // 3. Create a like reaction on the comment with targetFeedsExtraData
63+ Map <String , Object > targetFeedsExtraData = new HashMap <>();
64+ String extraDataValue = "SR:" + postedComment .getId ();
65+ targetFeedsExtraData .put ("parent_reaction" , extraDataValue );
66+
67+ FeedID [] targetFeeds = new FeedID [] {
68+ new FeedID ("notification" , userId )
69+ };
70+
71+ // The critical part of the test: Can we successfully create a reaction with targetFeedsExtraData
72+ Reaction like = client .reactions ().addChild (
73+ "actor-" + uniqueId , // Different user performs the like action
74+ "like" ,
75+ postedComment .getId (),
76+ targetFeeds ,
77+ targetFeedsExtraData
78+ ).join ();
79+
80+ // 4. Verify that the reaction was created successfully
81+ assertNotNull ("Like reaction should not be null" , like );
82+ assertEquals ("Like reaction should have kind='like'" , "like" , like .getKind ());
83+ assertEquals ("Like reaction should have parent ID" , postedComment .getId (), like .getParent ());
84+
85+ // Check if the reaction has extra data
86+ Map <String , Object > reactionExtra = like .getExtra ();
87+ assertNotNull ("Reaction should have extra data" , reactionExtra );
88+
89+ // Check for targetFeedsExtraData directly
90+ assertTrue ("Reaction should contain target_feeds_extra_data" ,
91+ reactionExtra .containsKey ("target_feeds_extra_data" ));
92+
93+ // Verify the content of targetFeedsExtraData
94+ Object extraDataObj = reactionExtra .get ("target_feeds_extra_data" );
95+ assertTrue ("target_feeds_extra_data should be a Map" , extraDataObj instanceof Map );
96+
97+ Map <String , Object > extraDataMap = (Map <String , Object >) extraDataObj ;
98+ assertTrue ("target_feeds_extra_data should contain parent_reaction" ,
99+ extraDataMap .containsKey ("parent_reaction" ));
100+ assertEquals ("parent_reaction value should match what we sent" ,
101+ extraDataValue , extraDataMap .get ("parent_reaction" ));
102+
103+ // 5. Get the reactions to verify
104+ List <Reaction > reactions = client .reactions ().filter (
105+ LookupKind .REACTION ,
106+ postedComment .getId (),
107+ "like"
108+ ).join ();
109+
110+ assertEquals ("Should have one like reaction" , 1 , reactions .size ());
111+ assertEquals ("Reaction should match the one we created" , like .getId (), reactions .get (0 ).getId ());
112+
113+ // Clean up
114+ client .reactions ().delete (like .getId ()).join ();
115+ client .reactions ().delete (postedComment .getId ()).join ();
116+ client .flatFeed ("user" , userId ).removeActivityByID (postedActivity .getID ()).join ();
117+ }
118+ }
0 commit comments