Skip to content

Commit abc1fa1

Browse files
authored
test: improve test code coverage (#162)
1 parent 7ef92f5 commit abc1fa1

26 files changed

+662
-133
lines changed

src/main/java/com/ibm/cloud/sdk/core/service/BaseService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2021.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -36,6 +36,7 @@
3636
import com.ibm.cloud.sdk.core.service.exception.ForbiddenException;
3737
import com.ibm.cloud.sdk.core.service.exception.InternalServerErrorException;
3838
import com.ibm.cloud.sdk.core.service.exception.InvalidServiceResponseException;
39+
import com.ibm.cloud.sdk.core.service.exception.NotAcceptableException;
3940
import com.ibm.cloud.sdk.core.service.exception.NotFoundException;
4041
import com.ibm.cloud.sdk.core.service.exception.RequestTooLargeException;
4142
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
@@ -439,6 +440,14 @@ public void setEndPoint(final String endPoint) {
439440
this.setServiceUrl(endPoint);
440441
}
441442

443+
/**
444+
* Returns the set of default headers current set on this BaseService instance.
445+
* @return the Headers object containing the default headers.
446+
*/
447+
public Headers getDefaultHeaders() {
448+
return defaultHeaders;
449+
}
450+
442451
/**
443452
* Set the default headers to be used on every HTTP request.
444453
*
@@ -526,7 +535,7 @@ protected <T> T processServiceCall(final ResponseConverter<T> converter, final R
526535
case HttpStatus.NOT_FOUND: // HTTP 404
527536
throw new NotFoundException(response);
528537
case HttpStatus.NOT_ACCEPTABLE: // HTTP 406
529-
throw new ForbiddenException(response);
538+
throw new NotAcceptableException(response);
530539
case HttpStatus.CONFLICT: // HTTP 409
531540
throw new ConflictException(response);
532541
case HttpStatus.REQUEST_TOO_LONG: // HTTP 413
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2022.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.ibm.cloud.sdk.core.service.exception;
15+
16+
import com.ibm.cloud.sdk.core.http.HttpStatus;
17+
18+
import okhttp3.Response;
19+
20+
/**
21+
* 406 Not Acceptable (HTTP/1.0 - RFC 1945).
22+
*/
23+
public class NotAcceptableException extends ServiceResponseException {
24+
25+
/**
26+
* The Constant serialVersionUID.
27+
*/
28+
private static final long serialVersionUID = 1L;
29+
30+
/**
31+
* Instantiates a new Forbidden Exception.
32+
*
33+
* @param response the HTTP response
34+
*/
35+
public NotAcceptableException(Response response) {
36+
super(HttpStatus.NOT_ACCEPTABLE, response);
37+
if (this.getMessage() == null) {
38+
this.setMessage("Not Acceptable: Service cannot produce a response.");
39+
}
40+
}
41+
}

src/main/java/com/ibm/cloud/sdk/core/util/CredentialUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2019.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -143,6 +143,12 @@ public static Map<String, List<VcapService>> getVcapServicesObj() {
143143

144144
// Retrieve the environment variable's value.
145145
String vcapValue = EnvironmentUtils.getenv(VCAP_SERVICES);
146+
147+
// If no env var, then look for a system property (helps in testing).
148+
if (StringUtils.isEmpty(vcapValue)) {
149+
vcapValue = System.getProperty(VCAP_SERVICES);
150+
}
151+
146152
if (StringUtils.isNotEmpty(vcapValue)) {
147153
Gson gson = GsonSingleton.getGson();
148154
// Parse it into a map of VcapService lists keyed by service name.

src/test/java/com/ibm/cloud/sdk/core/security/VpcInstanceAuthenticatorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2021.
2+
* (C) Copyright IBM Corp. 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -111,6 +111,9 @@ public void testBuilderDefaultConfig() {
111111
assertNull(authenticator.getURL());
112112
assertNull(authenticator.getIamProfileCrn());
113113
assertNull(authenticator.getIamProfileId());
114+
115+
VpcInstanceAuthenticator auth2 = authenticator.newBuilder().build();
116+
assertNotNull(auth2);
114117
}
115118

116119
@Test

src/test/java/com/ibm/cloud/sdk/core/test/TestUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2019.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -43,6 +43,9 @@ public final class TestUtils {
4343
/** The Constant PUT. */
4444
public static final String PUT = "PUT";
4545

46+
/** The Constant PATCH. */
47+
public static final String PATCH = "PATCH";
48+
4649
/**
4750
* Private constructor.
4851
*/

src/test/java/com/ibm/cloud/sdk/core/test/http/HttpClientSingletonTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2021.
2+
* (C) Copyright IBM Corp. 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -13,21 +13,23 @@
1313

1414
package com.ibm.cloud.sdk.core.test.http;
1515

16-
import com.ibm.cloud.sdk.core.http.HttpClientSingleton;
17-
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
18-
import okhttp3.ConnectionSpec;
19-
import okhttp3.OkHttpClient;
20-
import okhttp3.TlsVersion;
21-
import org.testng.annotations.Ignore;
22-
import org.testng.annotations.Test;
16+
import static org.testng.Assert.assertTrue;
2317

2418
import java.io.IOException;
25-
import java.util.Arrays;
2619
import java.util.ArrayList;
20+
import java.util.Arrays;
2721
import java.util.List;
22+
2823
import javax.net.ssl.SSLSocket;
2924

30-
import static org.testng.Assert.assertTrue;
25+
import org.testng.annotations.Test;
26+
27+
import com.ibm.cloud.sdk.core.http.HttpClientSingleton;
28+
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
29+
30+
import okhttp3.ConnectionSpec;
31+
import okhttp3.OkHttpClient;
32+
import okhttp3.TlsVersion;
3133

3234
/**
3335
* Unit tests for the HttpClientSingleton object.

src/test/java/com/ibm/cloud/sdk/core/test/http/HttpConfigTest.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2019.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -13,26 +13,34 @@
1313

1414
package com.ibm.cloud.sdk.core.test.http;
1515

16-
import okhttp3.Authenticator;
17-
import okhttp3.Request;
18-
import okhttp3.Response;
19-
import okhttp3.Route;
20-
import org.testng.annotations.Test;
21-
22-
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
16+
import static org.testng.Assert.assertEquals;
17+
import static org.testng.Assert.assertNotNull;
18+
import static org.testng.Assert.assertNull;
19+
import static org.testng.Assert.assertTrue;
2320

24-
import javax.annotation.Nullable;
2521
import java.io.IOException;
2622
import java.net.InetSocketAddress;
2723
import java.net.Proxy;
2824

29-
import static org.testng.Assert.assertEquals;
30-
import static org.testng.Assert.assertNull;
31-
import static org.testng.Assert.assertTrue;
25+
import javax.annotation.Nullable;
26+
27+
import org.testng.annotations.Test;
28+
29+
import com.ibm.cloud.sdk.core.http.HttpClientSingleton;
30+
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
31+
import com.ibm.cloud.sdk.core.util.HttpLogging;
32+
33+
import okhttp3.Authenticator;
34+
import okhttp3.OkHttpClient;
35+
import okhttp3.Request;
36+
import okhttp3.Response;
37+
import okhttp3.Route;
38+
import okhttp3.logging.HttpLoggingInterceptor;
3239

3340
/**
3441
* Unit tests for the HttpConfigOptions object.
3542
*/
43+
@SuppressWarnings("deprecation")
3644
public class HttpConfigTest {
3745

3846
@Test
@@ -58,5 +66,23 @@ public Request authenticate(@Nullable Route route, Response response) throws IOE
5866
assertNull(configOptions.getGzipCompression());
5967
assertEquals(proxy, configOptions.getProxy());
6068
assertEquals(HttpConfigOptions.LoggingLevel.HEADERS, configOptions.getLoggingLevel());
69+
70+
OkHttpClient client = HttpClientSingleton.getInstance().configureClient(configOptions);
71+
assertNotNull(client);
72+
73+
// Call configureClient to cover some additional statements.
74+
client = HttpClientSingleton.getInstance().configureClient(configOptions);
75+
assertNotNull(client);
76+
77+
client = HttpClientSingleton.getInstance().configureClient(null);
78+
assertNotNull(client);
79+
}
80+
81+
82+
@Test
83+
public void testHttpLogging() {
84+
HttpLoggingInterceptor interceptor = HttpLogging.getLoggingInterceptor();
85+
assertNotNull(interceptor);
6186
}
87+
6288
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2022.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.ibm.cloud.sdk.core.test.security;
15+
16+
import static org.testng.Assert.assertEquals;
17+
import static org.testng.Assert.assertNull;
18+
import static org.testng.Assert.assertTrue;
19+
20+
import org.testng.annotations.Test;
21+
22+
import com.ibm.cloud.sdk.core.security.AuthenticatorBase;
23+
24+
public class AuthenticatorBaseTest {
25+
26+
@Test
27+
public void testConstructBasicAuthHeader() {
28+
assertTrue(AuthenticatorBase.constructBasicAuthHeader("user", "password").startsWith("Basic "));
29+
assertNull(AuthenticatorBase.constructBasicAuthHeader(null, null));
30+
assertNull(AuthenticatorBase.constructBasicAuthHeader("", ""));
31+
}
32+
33+
@Test
34+
public void testConstructBearerAuthHeader() {
35+
assertEquals("Bearer token", AuthenticatorBase.constructBearerTokenAuthHeader("token"));
36+
assertNull( AuthenticatorBase.constructBearerTokenAuthHeader(null));
37+
assertNull( AuthenticatorBase.constructBearerTokenAuthHeader(""));
38+
}
39+
}

src/test/java/com/ibm/cloud/sdk/core/test/security/BasicAuthenticatorTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2021.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -36,7 +36,10 @@ public void testSuccess() {
3636
String username = "good-username";
3737
String password = "good-password";
3838

39-
BasicAuthenticator auth = new BasicAuthenticator(username, password);
39+
BasicAuthenticator auth = new BasicAuthenticator.Builder()
40+
.username(username)
41+
.password(password)
42+
.build();
4043
assertEquals(Authenticator.AUTHTYPE_BASIC, auth.authenticationType());
4144
assertEquals(username, auth.getUsername());
4245
assertEquals(password, auth.getPassword());
@@ -64,6 +67,9 @@ public void testBuilderSuccess() {
6467
assertNotNull(auth);
6568
assertEquals(auth.getUsername(), "good-user");
6669
assertEquals(auth.getPassword(), "good-password");
70+
71+
BasicAuthenticator auth2 = auth.newBuilder().build();
72+
assertNotNull(auth2);
6773
}
6874

6975
@Test(expectedExceptions = IllegalArgumentException.class)

src/test/java/com/ibm/cloud/sdk/core/test/security/BearerTokenAuthenticatorTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2015, 2021.
2+
* (C) Copyright IBM Corp. 2015, 2022.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -96,20 +96,31 @@ public void testConfigEmptyAccessToken() {
9696
BearerTokenAuthenticator.fromConfiguration(props);
9797
}
9898

99+
@Test
99100
public void testCtorCorrectConfig() {
100101
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator("my-access-token");
101102
assertEquals(Authenticator.AUTHTYPE_BEARER_TOKEN, authenticator.authenticationType());
102103
assertEquals("my-access-token", authenticator.getBearerToken());
103104
}
104105

105-
public void testConfigCorrectConfig() {
106+
@Test
107+
public void testConfigCorrectConfig1() {
106108
Map<String, String> props = new HashMap<>();
107109
props.put(Authenticator.PROPNAME_BEARER_TOKEN, "my-access-token");
108110
BearerTokenAuthenticator authenticator = BearerTokenAuthenticator.fromConfiguration(props);
109111
assertEquals(Authenticator.AUTHTYPE_BEARER_TOKEN, authenticator.authenticationType());
110112
assertEquals("my-access-token", authenticator.getBearerToken());
111113
}
112114

115+
@Test
116+
public void testConfigCorrectConfig2() {
117+
Map<String, String> props = new HashMap<>();
118+
props.put(Authenticator.PROPNAME_BEARER_TOKEN, "my-access-token");
119+
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(props);
120+
assertEquals(Authenticator.AUTHTYPE_BEARER_TOKEN, authenticator.authenticationType());
121+
assertEquals("my-access-token", authenticator.getBearerToken());
122+
}
123+
113124
@Test
114125
public void testSingleAuthHeader() {
115126
Map<String, String> props = new HashMap<>();

0 commit comments

Comments
 (0)