11package edu .harvard .iq .dataverse .api ;
22
3- import io .restassured .RestAssured ;
4- import io .restassured .path .json .JsonPath ;
5- import io .restassured .response .Response ;
63import edu .harvard .iq .dataverse .DataFile ;
74import edu .harvard .iq .dataverse .authorization .providers .builtin .BuiltinAuthenticationProvider ;
85import edu .harvard .iq .dataverse .authorization .providers .oauth2 .impl .GitHubOAuth2AP ;
96import edu .harvard .iq .dataverse .authorization .providers .oauth2 .impl .OrcidOAuth2AP ;
107import edu .harvard .iq .dataverse .settings .SettingsServiceBean ;
11-
12- import java .io .IOException ;
13- import java .nio .file .Files ;
14- import java .nio .file .Paths ;
15- import java .util .ArrayList ;
16- import java .util .HashMap ;
17- import java .util .List ;
18-
8+ import io .restassured .RestAssured ;
9+ import io .restassured .path .json .JsonPath ;
10+ import io .restassured .response .Response ;
1911import jakarta .json .Json ;
2012import jakarta .json .JsonArray ;
13+ import jakarta .json .JsonObject ;
14+ import org .junit .jupiter .api .AfterAll ;
15+ import org .junit .jupiter .api .Assumptions ;
16+ import org .junit .jupiter .api .BeforeAll ;
2117import org .junit .jupiter .api .Disabled ;
18+ import org .junit .jupiter .api .Nested ;
2219import org .junit .jupiter .api .Test ;
23- import org .junit .jupiter .api .BeforeAll ;
2420import org .junit .jupiter .params .ParameterizedTest ;
2521import org .junit .jupiter .params .provider .ValueSource ;
2622
27-
28-
23+ import java .io .IOException ;
24+ import java .nio .file .Files ;
25+ import java .nio .file .Paths ;
26+ import java .util .ArrayList ;
27+ import java .util .HashMap ;
28+ import java .util .List ;
2929import java .util .Map ;
3030import java .util .UUID ;
3131import java .util .logging .Logger ;
3232
33- import static jakarta .ws .rs .core .Response .Status .*;
34- import static org .hamcrest .CoreMatchers .*;
33+ import static io .restassured .RestAssured .given ;
34+ import static jakarta .ws .rs .core .Response .Status .BAD_REQUEST ;
35+ import static jakarta .ws .rs .core .Response .Status .CREATED ;
36+ import static jakarta .ws .rs .core .Response .Status .FORBIDDEN ;
37+ import static jakarta .ws .rs .core .Response .Status .INTERNAL_SERVER_ERROR ;
38+ import static jakarta .ws .rs .core .Response .Status .NOT_FOUND ;
39+ import static jakarta .ws .rs .core .Response .Status .OK ;
40+ import static jakarta .ws .rs .core .Response .Status .UNAUTHORIZED ;
41+ import static org .hamcrest .CoreMatchers .containsString ;
42+ import static org .hamcrest .CoreMatchers .equalTo ;
43+ import static org .hamcrest .CoreMatchers .notNullValue ;
3544import static org .junit .jupiter .api .Assertions .assertEquals ;
45+ import static org .junit .jupiter .api .Assertions .assertFalse ;
3646import static org .junit .jupiter .api .Assertions .assertTrue ;
3747
3848public class AdminIT {
@@ -45,7 +55,150 @@ public class AdminIT {
4555 public static void setUp () {
4656 RestAssured .baseURI = UtilIT .getRestAssuredBaseUri ();
4757 }
48-
58+
59+ @ Nested
60+ class SettingsAPI {
61+
62+ static final SettingsServiceBean .Key harmlessSetting = SettingsServiceBean .Key .InstallationName ;
63+ static final String harmlessValue = "Test Instance Name" ;
64+ static final String language = "fr" ;
65+ static final String harmlessL10nValue = "Nom de l'instance de test" ;
66+
67+ @ AfterAll
68+ static void destroy () {
69+ // No leftover settings after breaking tests!
70+ UtilIT .deleteSetting (harmlessSetting );
71+ UtilIT .deleteSetting (harmlessSetting , language );
72+ }
73+
74+ @ Test
75+ void testSettingsRoundTrip () {
76+ Assumptions .assumeTrue (UtilIT .getSetting (harmlessSetting ).statusCode () == NOT_FOUND .getStatusCode (), "Harmless setting should not exist yet." );
77+ Assumptions .assumeTrue (UtilIT .getSetting (harmlessSetting , language ).statusCode () == NOT_FOUND .getStatusCode (), "Harmless localized setting should not exist yet." );
78+
79+ // Step 0: Add a localized setting so we can make sure the put all can cope with that, too.
80+ UtilIT .setSetting (harmlessSetting , harmlessL10nValue , language );
81+
82+ // Step 1: Get current settings state
83+ Response getResponse = given ()
84+ .when ()
85+ .get ("/api/admin/settings" );
86+
87+ getResponse .then ()
88+ .assertThat ()
89+ .statusCode (OK .getStatusCode ())
90+ .contentType ("application/json" )
91+ .body ("status" , equalTo ("OK" ))
92+ .body ("data.'" +harmlessSetting +"/lang/" +language +"'" , equalTo (harmlessL10nValue ));
93+
94+ // Store original settings as JsonObject for later restoration
95+ JsonObject originalSettings = Json .createReader (getResponse .body ().asInputStream ())
96+ .readObject ()
97+ .getJsonObject ("data" );
98+
99+ // Step 2: Set our harmless test setting using UtilIT
100+ Response setResponse = UtilIT .setSetting (harmlessSetting .toString (), harmlessValue );
101+ setResponse .then ()
102+ .assertThat ()
103+ .statusCode (OK .getStatusCode ());
104+
105+ // Step 3: Verify the harmless setting was set
106+ Response verifySetResponse = UtilIT .getSetting (harmlessSetting );
107+
108+ verifySetResponse .then ()
109+ .assertThat ()
110+ .statusCode (OK .getStatusCode ())
111+ .body ("data.message" , equalTo (harmlessValue ));
112+
113+ // Step 4: Put back the original settings (this is what we're testing)
114+ Response putResponse = given ()
115+ //.header("X-Dataverse-key", "")
116+ .header ("Content-Type" , "application/json" )
117+ .body (originalSettings .toString ())
118+ .when ()
119+ .put ("/api/admin/settings" );
120+
121+ putResponse .then ()
122+ .assertThat ()
123+ .statusCode (OK .getStatusCode ())
124+ .body ("status" , equalTo ("OK" ))
125+ .body ("message.message" , containsString ("successfully updated" ));
126+
127+ // Step 5: Verify the harmless setting is gone (restored to original state)
128+ Response verifyRestoredResponse = given ()
129+ //.header("X-Dataverse-key", "")
130+ .when ()
131+ .get ("/api/admin/settings" + harmlessSetting .toString ());
132+
133+ verifyRestoredResponse .then ()
134+ .assertThat ()
135+ .statusCode (NOT_FOUND .getStatusCode ()); // Should not exist anymore
136+
137+ // Step 6: Verify overall settings state matches original
138+ Response finalGetResponse = given ()
139+ //.header("X-Dataverse-key", "")
140+ .when ()
141+ .get ("/api/admin/settings" );
142+
143+ finalGetResponse .then ()
144+ .assertThat ()
145+ .statusCode (OK .getStatusCode ());
146+
147+ // Store original settings as JsonObject for later restoration
148+ JsonObject finalSettings = Json .createReader (getResponse .body ().asInputStream ())
149+ .readObject ()
150+ .getJsonObject ("data" );
151+
152+ // Verify the settings are back to original state (our test setting should be absent)
153+ assertFalse (finalSettings .containsKey (harmlessSetting .toString ()), "Harmless setting should not exist in restored settings" );
154+
155+ // Cleanup: delete the localized setting
156+ UtilIT .deleteSetting (harmlessSetting , language );
157+ }
158+
159+ @ Test
160+ void testGetAllSettingsWithLocalization () {
161+ int statusCode = UtilIT .getSetting (harmlessSetting , language ).statusCode ();
162+ Assumptions .assumeTrue (statusCode == NOT_FOUND .getStatusCode (), "Harmless localized setting should not exist yet. Status Code: " + statusCode );
163+
164+ // Given
165+ UtilIT .setSetting (harmlessSetting , harmlessL10nValue , language );
166+
167+ // When
168+ Response getResponse = given ()
169+ .when ()
170+ .get ("/api/admin/settings" );
171+
172+ // Then
173+ getResponse .then ()
174+ .assertThat ()
175+ .statusCode (OK .getStatusCode ())
176+ .contentType ("application/json" )
177+ .body ("status" , equalTo ("OK" ))
178+ .body ("data.'" +harmlessSetting +"/lang/" +language +"'" , equalTo (harmlessL10nValue ));
179+
180+ // Cleanup
181+ UtilIT .deleteSetting (harmlessSetting , language );
182+ }
183+
184+ @ Test
185+ void testPutAllSettingsWithEmptyJson () {
186+ // Test error handling for empty JSON
187+ Response response = given ()
188+ //.header("X-Dataverse-key", UtilIT.getSuperuserApiToken())
189+ .header ("Content-Type" , "application/json" )
190+ .body ("{}" )
191+ .when ()
192+ .put ("/api/admin/settings" );
193+
194+ response .then ()
195+ .assertThat ()
196+ .statusCode (BAD_REQUEST .getStatusCode ())
197+ .body ("message" , containsString ("Empty or invalid JSON object" ));
198+ }
199+ }
200+
201+
49202 @ Test
50203 public void testListAuthenticatedUsers () throws Exception {
51204 Response anon = UtilIT .listAuthenticatedUsers (testNonSuperuserApiToken );
0 commit comments