Skip to content

Commit 383c3bd

Browse files
GH-264 loosening the workspace validation to ease its use
1 parent bb9ea0e commit 383c3bd

3 files changed

Lines changed: 35 additions & 35 deletions

File tree

core/src/main/java/com/adobe/aio/workspace/Workspace.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public static Builder builder() {
5858

5959
public void validateAll() {
6060
validateWorkspaceContext();
61+
if (StringUtils.isEmpty(apiKey)) {
62+
throw new IllegalStateException("Your `Workspace` is missing an apiKey");
63+
}
6164
if (!isAuthOAuth() && !isAuthJWT()) {
6265
throw new IllegalStateException("Missing auth configuration, set either jwt or oauth...");
6366
}
@@ -76,23 +79,12 @@ public void validateWorkspaceContext() throws IllegalStateException {
7679
if (StringUtils.isEmpty(this.getConsumerOrgId())) {
7780
throw new IllegalStateException("Your `Workspace` is missing a consumerOrgId");
7881
}
79-
if (StringUtils.isEmpty(apiKey)) {
80-
throw new IllegalStateException("Your `Workspace` is missing an apiKey");
81-
}
8282
if (StringUtils.isEmpty(this.getProjectId())) {
8383
throw new IllegalStateException("Your `Workspace` is missing a projectId");
8484
}
8585
if (StringUtils.isEmpty(this.getWorkspaceId())) {
8686
throw new IllegalStateException("Your `Workspace` is missing a workspaceId");
8787
}
88-
// note that the credentialId is optional
89-
// but it might be handy to have it in your `Workspace` POJO,
90-
// to avoid confusion when you have multiple credentials,
91-
// and to eventually in some Adobe API calls
92-
93-
if (authContext == null) {
94-
throw new IllegalStateException("Missing auth configuration ...");
95-
}
9688
}
9789

9890
public String getProjectUrl() {

core/src/test/java/com/adobe/aio/workspace/WorkspaceTest.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import java.security.KeyPair;
1616
import java.security.KeyPairGenerator;
1717
import java.security.PrivateKey;
18+
import java.util.HashSet;
19+
import java.util.Set;
1820

1921
import com.adobe.aio.auth.Context;
22+
import com.adobe.aio.auth.OAuthContext;
2023
import com.adobe.aio.util.Constants;
2124
import org.junit.jupiter.api.BeforeAll;
2225
import org.junit.jupiter.api.Test;
@@ -38,22 +41,16 @@ public static void beforeClass() throws Exception {
3841

3942
@Test
4043
public void successFullBuilder() throws IOException {
41-
42-
class MockContext implements Context {
43-
@Override
44-
public void validate() {
45-
46-
}
47-
}
48-
44+
Set<String> scopes = new HashSet<>();
45+
scopes.add("scope1");
4946
Workspace actual = Workspace.builder()
5047
.imsUrl(Constants.PROD_IMS_URL)
5148
.imsOrgId(Workspace.IMS_ORG_ID + TEST_VALUE)
5249
.apiKey(Workspace.API_KEY + TEST_VALUE)
5350
.consumerOrgId(Workspace.CONSUMER_ORG_ID + TEST_VALUE)
5451
.projectId(Workspace.PROJECT_ID + TEST_VALUE)
5552
.workspaceId(Workspace.WORKSPACE_ID + TEST_VALUE)
56-
.authContext(new MockContext())
53+
.authContext(new OAuthContext("someClientSecret", scopes))
5754
.build();
5855

5956
assertEquals(Workspace.IMS_ORG_ID + TEST_VALUE, actual.getImsOrgId());
@@ -63,6 +60,21 @@ public void validate() {
6360
assertEquals(Workspace.WORKSPACE_ID + TEST_VALUE, actual.getWorkspaceId());
6461
assertEquals(Constants.PROD_IMS_URL, actual.getImsUrl());
6562
actual.validateWorkspaceContext();
63+
actual.validateAll();
64+
}
65+
66+
@Test
67+
public void missingApiKey() {
68+
Workspace actual = Workspace.builder()
69+
.imsUrl(Constants.PROD_IMS_URL)
70+
.imsOrgId(Workspace.IMS_ORG_ID + TEST_VALUE)
71+
.consumerOrgId(Workspace.CONSUMER_ORG_ID + TEST_VALUE)
72+
.projectId(Workspace.PROJECT_ID + TEST_VALUE)
73+
.workspaceId(Workspace.WORKSPACE_ID + TEST_VALUE)
74+
.build();
75+
actual.validateWorkspaceContext();
76+
Exception ex = assertThrows(IllegalStateException.class, actual::validateAll);
77+
assertEquals("Your `Workspace` is missing an apiKey", ex.getMessage());
6678
}
6779

6880
@Test
@@ -81,16 +93,6 @@ public void missingConsumerOrgId() {
8193
assertEquals("Your `Workspace` is missing a consumerOrgId", ex.getMessage());
8294
}
8395

84-
@Test
85-
public void missingApiKey() {
86-
Workspace actual = Workspace.builder()
87-
.imsOrgId(Workspace.IMS_ORG_ID + TEST_VALUE)
88-
.consumerOrgId(Workspace.CONSUMER_ORG_ID + TEST_VALUE)
89-
.build();
90-
Exception ex = assertThrows(IllegalStateException.class, actual::validateWorkspaceContext);
91-
assertEquals("Your `Workspace` is missing an apiKey", ex.getMessage());
92-
}
93-
9496
@Test
9597
public void missingProjectId() {
9698
Workspace actual = Workspace.builder()

ims/src/main/java/com/adobe/aio/util/WorkspaceUtil.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,25 @@ public static Workspace.Builder getWorkspaceBuilder(Map<String, String> configMa
7272
.projectId(configMap.get(PROJECT_ID))
7373
.workspaceId(configMap.get(WORKSPACE_ID))
7474
.credentialId(configMap.get(CREDENTIAL_ID));
75-
builder.authContext(getAuthContext(configMap));
75+
getAuthContext(configMap).ifPresent(builder::authContext);
7676
return builder;
7777
}
7878

7979
public static boolean isOAuthConfig(Map<String, String> configMap) {
8080
return configMap.containsKey(SCOPES);
8181
}
8282

83-
public static Context getAuthContext(Map<String, String> configMap) {
83+
public static boolean isJwtConfig(Map<String, String> configMap) {
84+
return configMap.containsKey(META_SCOPES);
85+
}
86+
87+
public static Optional<Context> getAuthContext(Map<String, String> configMap) {
8488
if (isOAuthConfig(configMap)) {
85-
return getOAuthContextBuilder(configMap).build();
86-
} else {
87-
return getJwtContextBuilder(configMap).build();
89+
return Optional.of(getOAuthContextBuilder(configMap).build());
90+
} else if (isJwtConfig(configMap)) {
91+
return Optional.of(getJwtContextBuilder(configMap).build());
92+
} else {
93+
return Optional.empty();
8894
}
8995
}
9096

0 commit comments

Comments
 (0)