Skip to content

Commit 6eb1a89

Browse files
committed
Add authentication helper methods and test cases
1 parent 4d25d26 commit 6eb1a89

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

modules/swagger-codegen/src/main/resources/Java/configuration.mustache

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,48 @@ public class Configuration {
2424
public static Authentication getAuthentication(String authName) {
2525
return AUTH.get(authName);
2626
}
27+
28+
/** Set username for the first HTTP basic authentication. */
29+
public static void setUsername(String username) {
30+
for (Authentication auth : AUTH.values()) {
31+
if (auth instanceof HttpBasicAuth) {
32+
((HttpBasicAuth) auth).setUsername(username);
33+
return;
34+
}
35+
}
36+
throw new RuntimeException("No HTTP basic authentication configured!");
37+
}
38+
39+
/** Set password for the first HTTP basic authentication. */
40+
public static void setPassword(String password) {
41+
for (Authentication auth : AUTH.values()) {
42+
if (auth instanceof HttpBasicAuth) {
43+
((HttpBasicAuth) auth).setPassword(password);
44+
return;
45+
}
46+
}
47+
throw new RuntimeException("No HTTP basic authentication configured!");
48+
}
49+
50+
/** Set API key value for the first API key authentication. */
51+
public static void setApiKey(String apiKey) {
52+
for (Authentication auth : AUTH.values()) {
53+
if (auth instanceof ApiKeyAuth) {
54+
((ApiKeyAuth) auth).setApiKey(apiKey);
55+
return;
56+
}
57+
}
58+
throw new RuntimeException("No API key authentication configured!");
59+
}
60+
61+
/** Set API key prefix for the first API key authentication. */
62+
public static void setApiKeyPrefix(String apiKeyPrefix) {
63+
for (Authentication auth : AUTH.values()) {
64+
if (auth instanceof ApiKeyAuth) {
65+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
66+
return;
67+
}
68+
}
69+
throw new RuntimeException("No API key authentication configured!");
70+
}
2771
}

samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,48 @@ public class Configuration {
2828
public static Authentication getAuthentication(String authName) {
2929
return AUTH.get(authName);
3030
}
31+
32+
/** Set username for the first HTTP basic authentication. */
33+
public static void setUsername(String username) {
34+
for (Authentication auth : AUTH.values()) {
35+
if (auth instanceof HttpBasicAuth) {
36+
((HttpBasicAuth) auth).setUsername(username);
37+
return;
38+
}
39+
}
40+
throw new RuntimeException("No HTTP basic authentication configured!");
41+
}
42+
43+
/** Set password for the first HTTP basic authentication. */
44+
public static void setPassword(String password) {
45+
for (Authentication auth : AUTH.values()) {
46+
if (auth instanceof HttpBasicAuth) {
47+
((HttpBasicAuth) auth).setPassword(password);
48+
return;
49+
}
50+
}
51+
throw new RuntimeException("No HTTP basic authentication configured!");
52+
}
53+
54+
/** Set API key value for the first API key authentication. */
55+
public static void setApiKey(String apiKey) {
56+
for (Authentication auth : AUTH.values()) {
57+
if (auth instanceof ApiKeyAuth) {
58+
((ApiKeyAuth) auth).setApiKey(apiKey);
59+
return;
60+
}
61+
}
62+
throw new RuntimeException("No API key authentication configured!");
63+
}
64+
65+
/** Set API key prefix for the first API key authentication. */
66+
public static void setApiKeyPrefix(String apiKeyPrefix) {
67+
for (Authentication auth : AUTH.values()) {
68+
if (auth instanceof ApiKeyAuth) {
69+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
70+
return;
71+
}
72+
}
73+
throw new RuntimeException("No API key authentication configured!");
74+
}
3175
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.swagger.client;
2+
3+
import io.swagger.client.auth.*;
4+
5+
import static org.junit.Assert.*;
6+
import org.junit.*;
7+
8+
public class ConfigurationTest {
9+
@Test
10+
public void testGetAuthentication() {
11+
Authentication auth = Configuration.getAuthentication("api_key");
12+
assertNotNull(auth);
13+
assertTrue(auth instanceof ApiKeyAuth);
14+
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth;
15+
assertEquals("header", apiKeyAuth.getLocation());
16+
assertEquals("api_key", apiKeyAuth.getParamName());
17+
18+
auth = Configuration.getAuthentication("petstore_auth");
19+
assertTrue(auth instanceof OAuth);
20+
21+
assertNull(Configuration.getAuthentication("unknown"));
22+
}
23+
24+
@Test
25+
public void testSetUsername() {
26+
try {
27+
Configuration.setUsername("my-username");
28+
fail("should throw RuntimeException");
29+
} catch (RuntimeException e) {
30+
}
31+
}
32+
33+
@Test
34+
public void testSetPassword() {
35+
try {
36+
Configuration.setPassword("my-password");
37+
fail("should throw RuntimeException");
38+
} catch (RuntimeException e) {
39+
}
40+
}
41+
42+
@Test
43+
public void testSetApiKeyAndPrefix() {
44+
ApiKeyAuth auth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
45+
auth.setApiKey(null);
46+
auth.setApiKeyPrefix(null);
47+
48+
Configuration.setApiKey("my-api-key");
49+
Configuration.setApiKeyPrefix("Token");
50+
assertEquals("my-api-key", auth.getApiKey());
51+
assertEquals("Token", auth.getApiKeyPrefix());
52+
53+
auth.setApiKey(null);
54+
auth.setApiKeyPrefix(null);
55+
}
56+
}

0 commit comments

Comments
 (0)