Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 0ca0525

Browse files
committed
Removed System mock
1 parent 9851295 commit 0ca0525

4 files changed

Lines changed: 29 additions & 40 deletions

File tree

cloudant-client/src/test/java/com/cloudant/tests/CloudFoundryServiceTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.cloudant.tests;
1616

17+
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
1718
import static com.cloudant.tests.util.MockWebServerResources.IAM_API_KEY;
1819
import static com.cloudant.tests.util.MockWebServerResources.IAM_TOKEN;
1920
import static com.cloudant.tests.util.MockWebServerResources.OK_IAM_COOKIE;
@@ -23,12 +24,11 @@
2324
import com.cloudant.client.api.ClientBuilder;
2425
import com.cloudant.client.api.CloudantClient;
2526
import com.cloudant.tests.extensions.MockWebServerExtension;
26-
import com.cloudant.tests.util.IamSystemPropertyMock;
2727
import com.cloudant.tests.util.MockWebServerResources;
2828
import com.google.gson.GsonBuilder;
2929

30+
import org.junit.jupiter.api.AfterEach;
3031
import org.junit.jupiter.api.Assertions;
31-
import org.junit.jupiter.api.BeforeAll;
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.Test;
3434
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -46,8 +46,6 @@
4646

4747
public class CloudFoundryServiceTest {
4848

49-
public static IamSystemPropertyMock iamSystemPropertyMock;
50-
5149
private static String TEST_HOST = "https://cloudant.example";
5250
private static String TEST_USER = "user";
5351
private static String TEST_PASSWORD = "pass";
@@ -63,25 +61,23 @@ public class CloudFoundryServiceTest {
6361
public MockWebServer server;
6462
public MockWebServer mockIamServer;
6563

66-
/**
67-
* Before running this test class setup the property mock.
68-
*/
69-
@BeforeAll
70-
public static void setupIamSystemPropertyMock() {
71-
iamSystemPropertyMock = new IamSystemPropertyMock();
72-
}
73-
7464
@BeforeEach
7565
public void beforeEach() {
7666
server = mockWebServerExt.get();
7767
server.useHttps(MockWebServerResources.getSSLSocketFactory(), false);
7868
mockServerHostPort = String.format("%s:%s/", server.getHostName(), server.getPort());
7969
//setup mock IAM server
8070
mockIamServer = mockIamServerExt.get();
81-
iamSystemPropertyMock.setMockIamTokenEndpointUrl(mockIamServer.url(iamTokenEndpoint)
71+
// Override the default IAM token server with our test mock server
72+
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
8273
.toString());
8374
}
8475

76+
@AfterEach
77+
public void clearIAMMock() {
78+
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
79+
}
80+
8581
private static class VCAPGenerator {
8682

8783
private Map<String, Object> vcap;

cloudant-client/src/test/java/com/cloudant/tests/HttpIamTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.cloudant.tests;
1616

17+
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
1718
import static com.cloudant.tests.HttpTest.takeN;
1819
import static com.cloudant.tests.util.MockWebServerResources.EXPECTED_OK_COOKIE;
1920
import static com.cloudant.tests.util.MockWebServerResources.EXPECTED_OK_COOKIE_2;
@@ -40,10 +41,9 @@
4041
import com.cloudant.http.Http;
4142
import com.cloudant.http.interceptors.Replay429Interceptor;
4243
import com.cloudant.tests.extensions.MockWebServerExtension;
43-
import com.cloudant.tests.util.IamSystemPropertyMock;
4444
import com.cloudant.tests.util.MockWebServerResources;
4545

46-
import org.junit.jupiter.api.BeforeAll;
46+
import org.junit.jupiter.api.AfterEach;
4747
import org.junit.jupiter.api.BeforeEach;
4848
import org.junit.jupiter.api.Test;
4949
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -60,8 +60,6 @@
6060

6161
public class HttpIamTest {
6262

63-
public static IamSystemPropertyMock iamSystemPropertyMock;
64-
6563
@RegisterExtension
6664
public MockWebServerExtension mockWebServerExt = new MockWebServerExtension();
6765

@@ -71,25 +69,23 @@ public class HttpIamTest {
7169
public MockWebServer mockWebServer;
7270
public MockWebServer mockIamServer;
7371

74-
/**
75-
* Before running this test class setup the property mock.
76-
*/
77-
@BeforeAll
78-
public static void setupIamSystemPropertyMock() {
79-
iamSystemPropertyMock = new IamSystemPropertyMock();
80-
}
81-
8272
/**
8373
* Before each test set the value of the endpoint in the property mock
8474
*/
8575
@BeforeEach
8676
public void setIAMMockEndpoint() {
8777
mockWebServer = mockWebServerExt.get();
8878
mockIamServer = mockIamServerExt.get();
89-
iamSystemPropertyMock.setMockIamTokenEndpointUrl(mockIamServer.url(iamTokenEndpoint)
79+
// Override the default IAM token server with our test mock server
80+
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
9081
.toString());
9182
}
9283

84+
@AfterEach
85+
public void clearIAMMock() {
86+
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
87+
}
88+
9389
/**
9490
* Test IAM token and cookie flow:
9591
* - GET a resource on the cloudant server

cloudant-client/src/test/java/com/cloudant/tests/SessionInterceptorExpiryTests.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.cloudant.tests;
1616

17+
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
18+
import static com.cloudant.tests.util.MockWebServerResources.iamTokenEndpoint;
1719
import static org.junit.jupiter.api.Assertions.assertEquals;
1820
import static org.junit.jupiter.api.Assertions.assertNotNull;
1921
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -30,10 +32,9 @@
3032
import com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory;
3133
import com.cloudant.tests.extensions.MockWebServerExtension;
3234
import com.cloudant.tests.util.HttpFactoryParameterizedTest;
33-
import com.cloudant.tests.util.IamSystemPropertyMock;
3435
import com.cloudant.tests.util.MockWebServerResources;
3536

36-
import org.junit.jupiter.api.BeforeAll;
37+
import org.junit.jupiter.api.AfterEach;
3738
import org.junit.jupiter.api.BeforeEach;
3839
import org.junit.jupiter.api.TestTemplate;
3940
import org.junit.jupiter.api.extension.ExtendWith;
@@ -115,8 +116,6 @@ public Object resolveParameter(ParameterContext parameterContext,
115116
}
116117
}
117118

118-
public static IamSystemPropertyMock iamSystemPropertyMock;
119-
120119
@RegisterExtension
121120
public MockWebServerExtension mockWebServerExt = new MockWebServerExtension();
122121
public MockWebServer mockWebServer;
@@ -127,14 +126,6 @@ public Object resolveParameter(ParameterContext parameterContext,
127126
private HttpConnectionRequestInterceptor rqInterceptor;
128127
private HttpConnectionResponseInterceptor rpInterceptor;
129128

130-
/**
131-
* Before running this test class setup the property mock.
132-
*/
133-
@BeforeAll
134-
public static void setupIamSystemPropertyMock() {
135-
iamSystemPropertyMock = new IamSystemPropertyMock();
136-
}
137-
138129
@BeforeEach
139130
public void setupSessionInterceptor(boolean okUsable, String sessionPath) {
140131
this.mockWebServer = mockWebServerExt.get();
@@ -148,15 +139,20 @@ public void setupSessionInterceptor(boolean okUsable, String sessionPath) {
148139
rpInterceptor = ci;
149140
} else if (sessionPath.equals("/_iam_session")) {
150141
// Set the endpoint value before each test
151-
iamSystemPropertyMock.setMockIamTokenEndpointUrl(mockIamServer.url("/identity/token")
142+
// Override the default IAM token server with our test mock server
143+
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
152144
.toString());
153145
IamCookieInterceptor ici = new IamCookieInterceptor("apikey", baseUrl);
154146
rqInterceptor = ici;
155147
rpInterceptor = ici;
156148
} else {
157149
fail("Invalid sessionPath " + sessionPath);
158150
}
151+
}
159152

153+
@AfterEach
154+
public void clearIAMMock() {
155+
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
160156
}
161157

162158
private void queueResponses(boolean okUsable,

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/IamCookieInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
public class IamCookieInterceptor extends CookieInterceptorBase {
3131

32+
public static final String IAM_TOKEN_SERVER_URL_PROPERTY_KEY = "com.cloudant.client.iamserver";
3233
public final URL iamServerUrl;
3334
private final byte[] tokenRequestPayload;
3435

@@ -37,7 +38,7 @@ public IamCookieInterceptor(String apiKey, String baseUrl) {
3738

3839
// Read iamServer from system property, or set default
3940
try {
40-
this.iamServerUrl = new URL(System.getProperty("com.cloudant.client.iamserver",
41+
this.iamServerUrl = new URL(System.getProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY,
4142
"https://iam.cloud.ibm.com/identity/token"));
4243
} catch (MalformedURLException mue) {
4344
throw new RuntimeException("IAM server property was not a valid URL", mue);

0 commit comments

Comments
 (0)