11package io .getstream .client ;
22
3- import io .getstream .core .exceptions .StreamAPIException ;
43import io .getstream .core .exceptions .StreamException ;
54import io .getstream .core .models .Activity ;
6- import io .getstream .core .models .AuditLog ;
75import org .junit .Before ;
86import org .junit .Test ;
9- import java .util .Date ;
10- import java .util .List ;
11- import java .util .concurrent .CompletionException ;
12-
137import 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 */
2013public 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+ }
0 commit comments