Skip to content

Commit 1195245

Browse files
author
rathnapandi
committed
- change password
1 parent 0cc2f22 commit 1195245

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

modules/apis/src/test/java/com/axway/apim/test/CoreInitializationTestIT.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import com.axway.apim.EndpointConfig;
44
import com.jayway.jsonpath.DocumentContext;
55
import com.jayway.jsonpath.JsonPath;
6-
import org.apache.commons.io.IOUtils;
76
import org.apache.hc.client5.http.classic.HttpClient;
87
import org.apache.hc.client5.http.classic.methods.HttpGet;
8+
import org.apache.hc.client5.http.classic.methods.HttpPost;
99
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
10+
import org.apache.hc.core5.http.ContentType;
11+
import org.apache.hc.core5.http.HttpEntity;
1012
import org.apache.hc.core5.http.ParseException;
1113
import org.apache.hc.core5.http.io.entity.EntityUtils;
14+
import org.apache.hc.core5.http.io.entity.StringEntity;
15+
import org.apache.http.client.utils.URIBuilder;
1216
import org.citrusframework.actions.AbstractTestAction;
1317
import org.citrusframework.context.TestContext;
1418
import org.citrusframework.dsl.runner.TestRunner;
@@ -17,13 +21,14 @@
1721
import org.citrusframework.variable.GlobalVariables;
1822
import org.springframework.beans.factory.annotation.Autowired;
1923
import org.springframework.beans.factory.annotation.Value;
20-
import org.springframework.core.io.ClassPathResource;
2124
import org.springframework.http.HttpHeaders;
2225
import org.springframework.http.HttpStatus;
2326
import org.springframework.http.MediaType;
2427
import org.springframework.test.context.ContextConfiguration;
2528

2629
import java.io.IOException;
30+
import java.net.URI;
31+
import java.net.URISyntaxException;
2732
import java.net.URLEncoder;
2833
import java.nio.charset.StandardCharsets;
2934
import java.util.Base64;
@@ -53,21 +58,21 @@ public class CoreInitializationTestIT extends TestRunnerBeforeSuiteSupport {
5358
@Autowired
5459
GlobalVariables globalVariables;
5560

61+
private final static String DEFAULT_PASSWORD = "changeme";
62+
5663

5764
@Override
5865
public void beforeSuite(TestRunner testRunner) {
59-
String format = username + ":" + password;
66+
67+
String format = username + ":" + DEFAULT_PASSWORD;
6068
String authorizationHeaderValue = "Basic " + Base64.getEncoder().encodeToString(format.getBytes());
6169
String url = "https://" + host + ":" + port + "/api/portal/v1.4";
6270

6371
try {
64-
testRunner.echo("Turn off changePasswordOnFirstLogin and passwordExpiryEnabled validation to run integration tests");
65-
String request = IOUtils.toString(new ClassPathResource("/com/axway/apim/test/files/config/apimanager-test-config.json").getInputStream(), StandardCharsets.UTF_8);
66-
67-
testRunner.http(action -> action.client(apiManager).send().put("/config").header("Content-Type", "application/json")
68-
.payload(request));
72+
testRunner.echo("Change password of user for initial setup");
73+
postRequest(url + "/currentuser/changepassword", authorizationHeaderValue);
6974

70-
String orgName = URLEncoder.encode((String) globalVariables.getVariables().get("orgName"), "UTF-8");
75+
String orgName = URLEncoder.encode((String) globalVariables.getVariables().get("orgName"), StandardCharsets.UTF_8);
7176
String response = getRequest(url + "/organizations?field=name&op=eq&value=" + orgName, authorizationHeaderValue);
7277
DocumentContext documentContext = JsonPath.parse(response);
7378
if (!response.equals("[]")) {
@@ -93,7 +98,7 @@ public void beforeSuite(TestRunner testRunner) {
9398
}
9499

95100
testRunner.echo("Creating second organization");
96-
String orgName2 = URLEncoder.encode((String) globalVariables.getVariables().get("orgName2"), "UTF-8");
101+
String orgName2 = URLEncoder.encode((String) globalVariables.getVariables().get("orgName2"), StandardCharsets.UTF_8);
97102
response = getRequest(url + "/organizations?field=name&op=eq&value=" + orgName2, authorizationHeaderValue);
98103
if (!response.equals("[]")) {
99104
testRunner.echo("Organization ${orgName2} Already exists");
@@ -119,7 +124,7 @@ public void beforeSuite(TestRunner testRunner) {
119124
}
120125

121126
testRunner.echo("Creating third organization");
122-
String orgName3 = URLEncoder.encode((String) globalVariables.getVariables().get("orgName3"), "UTF-8");
127+
String orgName3 = URLEncoder.encode((String) globalVariables.getVariables().get("orgName3"), StandardCharsets.UTF_8);
123128
response = getRequest(url + "/organizations?field=name&op=eq&value=" + orgName3, authorizationHeaderValue);
124129
if (!response.equals("[]")) {
125130
testRunner.echo("Organization ${orgName3} Already exists");
@@ -219,4 +224,23 @@ public String getRequest(String url, String authorizationHeaderValue) {
219224
throw new RuntimeException(e);
220225
}
221226
}
227+
228+
public void postRequest(String url, String authorizationHeaderValue) throws URISyntaxException {
229+
URI uri = new URIBuilder(url).build();
230+
HttpEntity entity = new StringEntity("newPassword=" + password + "&oldPassword="+DEFAULT_PASSWORD, ContentType.APPLICATION_FORM_URLENCODED);
231+
HttpPost post = new HttpPost(uri);
232+
post.setEntity(entity);
233+
post.setHeader(HttpHeaders.AUTHORIZATION, authorizationHeaderValue);
234+
235+
try (CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(post)) {
236+
int statusCode = response.getCode();
237+
if (statusCode != 204) {
238+
throw new RuntimeException("Error changing password of user. Response-Code: " + EntityUtils.toString(response.getEntity()));
239+
}
240+
241+
242+
} catch (IOException | ParseException e) {
243+
throw new RuntimeException(e);
244+
}
245+
}
222246
}

0 commit comments

Comments
 (0)