Skip to content

Commit 3de5996

Browse files
author
github-actions
committed
chore: change contructor to builder class
1 parent cffe407 commit 3de5996

File tree

4 files changed

+97
-146
lines changed

4 files changed

+97
-146
lines changed

src/main/java/io/getstream/client/AuditLogsClient.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public CompletableFuture<QueryAuditLogsResponse> queryAuditLogs(QueryAuditLogsFi
5252
throw new StreamException("Filters cannot be null for audit logs queries");
5353
}
5454

55-
filters.validate();
56-
5755
final Token token = buildAuditLogsToken(secret, TokenAction.READ);
5856

5957
RequestOption[] options = buildRequestOptions(filters, pager);

src/main/java/io/getstream/client/QueryAuditLogsFilters.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
/**
66
* Filters for querying audit logs.
77
* Either entityType+entityID pair OR userID is required by the API.
8+
*
9+
* Common entity types in Stream include:
10+
* - "activity" - for feed activities
11+
* - "reaction" - for activity reactions
12+
* - "user" - for Stream users
13+
* - "feed" - for feed configurations
814
*/
915
public class QueryAuditLogsFilters {
1016
private String entityType;
@@ -39,14 +45,34 @@ public static QueryAuditLogsFilters forUser(String userID) {
3945
/**
4046
* Creates a new filter for entity type and ID queries.
4147
*
42-
* @param entityType The type of entity (e.g., "user", "feed")
48+
* @param entityType The type of entity (e.g., "activity", "reaction", "user", "feed")
4349
* @param entityID The ID of the entity
4450
* @return a new QueryAuditLogsFilters with the entity type and ID set
4551
*/
4652
public static QueryAuditLogsFilters forEntity(String entityType, String entityID) {
4753
return builder().withEntityType(entityType).withEntityID(entityID).build();
4854
}
4955

56+
/**
57+
* Convenience method to create a filter for activity entities.
58+
*
59+
* @param activityID The ID of the activity
60+
* @return a new QueryAuditLogsFilters for the activity
61+
*/
62+
public static QueryAuditLogsFilters forActivity(String activityID) {
63+
return forEntity("activity", activityID);
64+
}
65+
66+
/**
67+
* Convenience method to create a filter for reaction entities.
68+
*
69+
* @param reactionID The ID of the reaction
70+
* @return a new QueryAuditLogsFilters for the reaction
71+
*/
72+
public static QueryAuditLogsFilters forReaction(String reactionID) {
73+
return forEntity("reaction", reactionID);
74+
}
75+
5076
public String getEntityType() {
5177
return entityType;
5278
}
@@ -62,7 +88,7 @@ public String getUserID() {
6288
/**
6389
* Set the entity type for existing filter instance.
6490
*
65-
* @param entityType The type of entity
91+
* @param entityType The type of entity (e.g., "activity", "reaction")
6692
* @return this instance for method chaining
6793
*/
6894
public QueryAuditLogsFilters setEntityType(String entityType) {
@@ -134,7 +160,7 @@ private Builder() {
134160
/**
135161
* Set the entity type.
136162
*
137-
* @param entityType The type of entity (e.g., "user", "feed")
163+
* @param entityType The type of entity (e.g., "activity", "reaction")
138164
* @return this builder for method chaining
139165
*/
140166
public Builder withEntityType(String entityType) {
Lines changed: 23 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,56 @@
11
package io.getstream.client;
22

3-
import io.getstream.core.exceptions.StreamAPIException;
43
import io.getstream.core.exceptions.StreamException;
54
import io.getstream.core.models.Activity;
6-
import io.getstream.core.models.AuditLog;
75
import org.junit.Before;
86
import org.junit.Test;
9-
import java.util.Date;
10-
import java.util.List;
11-
import java.util.concurrent.CompletionException;
12-
137
import static org.junit.Assert.*;
14-
import static org.junit.Assume.assumeTrue;
158

169
/**
1710
* Integration test for the AuditLogsClient
1811
* Uses a Stream app with audit logs enabled
1912
*/
2013
public class AuditLogsClientIntegrationTest {
2114
// Credentials for a Stream app with audit logs enabled
22-
private static final String apiKey = "cahwc7wn4qs9";
23-
private static final String secret = "x7psq92284cmn2j9wkhdjyrum9va7h6d6m5cbm9ryjgv649surzj9fdex34u6utn";
24-
15+
private static final String apiKey =
16+
System.getenv("STREAM_KEY") != null
17+
? System.getenv("STREAM_KEY")
18+
: System.getProperty("STREAM_KEY");
19+
private static final String secret =
20+
System.getenv("STREAM_SECRET") != null
21+
? System.getenv("STREAM_SECRET")
22+
: System.getProperty("STREAM_SECRET");
23+
2524
private Client client;
26-
private boolean hasValidCredentials = false;
2725

2826
@Before
2927
public void setUp() throws Exception {
30-
if (apiKey != null && !apiKey.isEmpty() && secret != null && !secret.isEmpty()) {
31-
client = Client.builder(apiKey, secret).region("oregon").build();
32-
hasValidCredentials = true;
33-
}
28+
client = Client.builder(apiKey, secret).build();
3429
}
3530

31+
3632
@Test
3733
public void testQueryAuditLogs() throws StreamException {
38-
// Skip test if credentials aren't available
39-
assumeTrue("Skipping test due to missing API credentials", hasValidCredentials);
40-
41-
// Test querying audit logs with a user filter (required by the API)
42-
// Using the builder pattern for better flexibility
43-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forUser("admin");
34+
FlatFeed userFeed = client.flatFeed("user", "1");
35+
Activity a = Activity.builder().actor("userid:1").verb("tweet").object("Tweet:1").build();
36+
a = userFeed.addActivity(a).join();
37+
38+
// Using the convenience method for activity entities
4439
QueryAuditLogsPager pager = new QueryAuditLogsPager(5); // limit to 5 results
45-
40+
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forActivity(a.getID());
4641
QueryAuditLogsResponse response = client.auditLogs().queryAuditLogs(filters, pager).join();
47-
48-
// Verify response structure
49-
assertNotNull("Response should not be null", response);
50-
assertNotNull("Audit logs list should not be null", response.getAuditLogs());
51-
assertNotNull("Duration should not be null", response.getDuration());
52-
53-
// Test that audit logs list is properly initialized (even if empty)
54-
assertTrue("Audit logs list should be accessible", response.getAuditLogs() != null);
55-
56-
// Verify that pagination properties exist
57-
// Note: they might be null if no pagination is needed
58-
// but the fields themselves should exist
59-
assertNotNull("Response object should contain next field (even if null)", response);
60-
assertNotNull("Response object should contain prev field (even if null)", response);
61-
}
62-
63-
@Test
64-
public void testQueryAuditLogsByEntityType() throws StreamException {
65-
// Skip test if credentials aren't available
66-
assumeTrue("Skipping test due to missing API credentials", hasValidCredentials);
67-
68-
// Test querying audit logs by entity type and ID
69-
// Using the static factory method for entity-based filters
70-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forEntity("user", "user-123");
71-
QueryAuditLogsPager pager = new QueryAuditLogsPager(5);
72-
73-
QueryAuditLogsResponse response = client.auditLogs().queryAuditLogs(filters, pager).join();
74-
42+
7543
// Verify response structure
7644
assertNotNull("Response should not be null", response);
7745
assertNotNull("Audit logs list should not be null", response.getAuditLogs());
78-
assertNotNull("Duration should not be null", response.getDuration());
79-
80-
// Validate that filters worked properly - if we have any logs for this entity
81-
if (!response.getAuditLogs().isEmpty()) {
82-
for (AuditLog log : response.getAuditLogs()) {
83-
if (log.getEntityType() != null && log.getEntityID() != null) {
84-
assertEquals("Entity type should match filter", "user", log.getEntityType());
85-
assertEquals("Entity ID should match filter", "user-123", log.getEntityID());
86-
}
87-
}
88-
}
89-
}
90-
91-
@Test
92-
public void testInvalidFilters() throws StreamException {
93-
// No need to check credentials since this doesn't make an API call
94-
95-
// Test that validation works for invalid filters
96-
// Using the builder with no values set
97-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.builder().build();
98-
99-
// Use a different approach since JUnit 4 doesn't have assertThrows
100-
try {
101-
filters.validate();
102-
fail("Should have thrown an exception for invalid filters");
103-
} catch (StreamException e) {
104-
// Expected exception
105-
assertEquals("Error message should match validation message",
106-
"Either entityType+entityID or userID is required for audit logs queries",
107-
e.getMessage());
108-
}
109-
}
110-
111-
@Test
112-
public void testQueryAuditLogs2() throws StreamException {
113-
// Skip test if credentials aren't available
114-
assumeTrue("Skipping test due to missing API credentials", hasValidCredentials);
115-
116-
try {
117-
FlatFeed userFeed = client.flatFeed("user", "1");
118-
Activity a = Activity.builder().actor("userid:1").verb("tweet").object("Tweet:1").build();
119-
userFeed.addActivities(a).join();
46+
assertNotEquals(0, response.getAuditLogs().size());
12047

121-
// Using the builder pattern with chainable methods for complex filters
122-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.builder()
123-
.withEntityType("activity")
124-
.withEntityID("userid:1")
125-
.build();
48+
filters = QueryAuditLogsFilters.forUser("userid:1");
49+
response = client.auditLogs().queryAuditLogs(filters, pager).join();
12650

127-
QueryAuditLogsPager pager = new QueryAuditLogsPager(5); // limit to 5 results
128-
129-
QueryAuditLogsResponse response = client.auditLogs().queryAuditLogs(filters, pager).join();
130-
131-
// Verify response structure
132-
assertNotNull("Response should not be null", response);
133-
assertNotNull("Audit logs list should not be null", response.getAuditLogs());
134-
assertNotNull("Duration should not be null", response.getDuration());
135-
} catch (Exception e) {
136-
// In case of any error with the activity creation, just log and continue
137-
System.err.println("Test encountered error: " + e.getMessage());
138-
// Don't fail the test as this is just testing the audit logs API functionality
139-
}
140-
}
141-
142-
@Test
143-
public void testMixedFilters() throws StreamException {
144-
// Skip test if credentials aren't available
145-
assumeTrue("Skipping test due to missing API credentials", hasValidCredentials);
146-
147-
// Test creating a filter with both entity and user information
148-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.builder()
149-
.withEntityType("feed")
150-
.withEntityID("user:123")
151-
.withUserID("admin")
152-
.build();
153-
154-
QueryAuditLogsPager pager = new QueryAuditLogsPager(5);
155-
156-
QueryAuditLogsResponse response = client.auditLogs().queryAuditLogs(filters, pager).join();
157-
15851
// Verify response structure
15952
assertNotNull("Response should not be null", response);
16053
assertNotNull("Audit logs list should not be null", response.getAuditLogs());
161-
assertNotNull("Duration should not be null", response.getDuration());
162-
163-
// Since we've used both entity and user filters, API will prioritize one based on its implementation
164-
// Just verify we got a valid response back
165-
assertTrue("Response should be properly constructed", response != null && response.getAuditLogs() != null);
54+
assertNotEquals(0, response.getAuditLogs().size());
16655
}
167-
}
56+
}

src/test/java/io/getstream/client/AuditLogsClientTest.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void setUp() throws Exception {
9191

9292
@Test
9393
public void testQueryAuditLogs() throws Exception {
94-
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forEntity("user", "user-123");
94+
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forEntity("activity", "activity-123");
9595
QueryAuditLogsPager pager = new QueryAuditLogsPager(10);
9696

9797
QueryAuditLogsResponse response = client.auditLogs().queryAuditLogs(filters, pager).join();
@@ -126,8 +126,8 @@ public void testQueryAuditLogs() throws Exception {
126126
String urlQuery = lastRequest.getURL().getQuery();
127127
Map<String, String> queryParams = extractQueryParams(urlQuery);
128128

129-
assertEquals("user", queryParams.get("entity_type"));
130-
assertEquals("user-123", queryParams.get("entity_id"));
129+
assertEquals("activity", queryParams.get("entity_type"));
130+
assertEquals("activity-123", queryParams.get("entity_id"));
131131
assertEquals("10", queryParams.get("limit"));
132132
}
133133

@@ -192,8 +192,8 @@ public void testQueryAuditLogsTokenGeneration() throws Exception {
192192
public void testBuilderPatternFlexibility() throws Exception {
193193
// Test the full builder pattern flexibility
194194
QueryAuditLogsFilters filters = QueryAuditLogsFilters.builder()
195-
.withEntityType("feed")
196-
.withEntityID("user:123")
195+
.withEntityType("reaction")
196+
.withEntityID("reaction-123")
197197
.withUserID("admin")
198198
.build();
199199

@@ -207,11 +207,49 @@ public void testBuilderPatternFlexibility() throws Exception {
207207
String urlQuery = lastRequest.getURL().getQuery();
208208
Map<String, String> queryParams = extractQueryParams(urlQuery);
209209

210-
assertEquals("feed", queryParams.get("entity_type"));
211-
assertEquals("user:123", queryParams.get("entity_id"));
210+
assertEquals("reaction", queryParams.get("entity_type"));
211+
assertEquals("reaction-123", queryParams.get("entity_id"));
212212
assertEquals("admin", queryParams.get("user_id"));
213213
}
214214

215+
@Test
216+
public void testForActivityConvenienceMethod() throws Exception {
217+
// Test the convenience method for activities
218+
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forActivity("activity-789");
219+
220+
client.auditLogs().queryAuditLogs(filters).join();
221+
222+
// Verify request parameters
223+
Request lastRequest = mockHTTPClient.lastRequest;
224+
assertNotNull(lastRequest);
225+
226+
// Extract query parameters from URL
227+
String urlQuery = lastRequest.getURL().getQuery();
228+
Map<String, String> queryParams = extractQueryParams(urlQuery);
229+
230+
assertEquals("activity", queryParams.get("entity_type"));
231+
assertEquals("activity-789", queryParams.get("entity_id"));
232+
}
233+
234+
@Test
235+
public void testForReactionConvenienceMethod() throws Exception {
236+
// Test the convenience method for reactions
237+
QueryAuditLogsFilters filters = QueryAuditLogsFilters.forReaction("reaction-456");
238+
239+
client.auditLogs().queryAuditLogs(filters).join();
240+
241+
// Verify request parameters
242+
Request lastRequest = mockHTTPClient.lastRequest;
243+
assertNotNull(lastRequest);
244+
245+
// Extract query parameters from URL
246+
String urlQuery = lastRequest.getURL().getQuery();
247+
Map<String, String> queryParams = extractQueryParams(urlQuery);
248+
249+
assertEquals("reaction", queryParams.get("entity_type"));
250+
assertEquals("reaction-456", queryParams.get("entity_id"));
251+
}
252+
215253
private Map<String, String> extractQueryParams(String query) {
216254
if (query == null || query.isEmpty()) {
217255
return Collections.emptyMap();

0 commit comments

Comments
 (0)