Skip to content

Commit 6a55e1c

Browse files
Radek Mensikliry
andcommitted
1064 get user by login
Co-authored-by: Libor Ryšavý <liry@users.noreply.github.com>
1 parent 2aeea4a commit 6a55e1c

11 files changed

Lines changed: 271 additions & 3 deletions

File tree

gooddata-java-model/src/main/java/com/gooddata/sdk/model/account/Account.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
*/
66
package com.gooddata.sdk.model.account;
77

8-
import com.fasterxml.jackson.annotation.*;
8+
import com.fasterxml.jackson.annotation.JsonCreator;
9+
import com.fasterxml.jackson.annotation.JsonIgnore;
10+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
11+
import com.fasterxml.jackson.annotation.JsonInclude;
12+
import com.fasterxml.jackson.annotation.JsonProperty;
13+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
14+
import com.fasterxml.jackson.annotation.JsonTypeName;
15+
import com.fasterxml.jackson.annotation.JsonView;
916
import com.gooddata.sdk.common.util.GoodDataToStringBuilder;
1017
import com.gooddata.sdk.model.util.UriHelper;
1118

@@ -22,6 +29,7 @@ public class Account {
2229

2330
public static final String URI = "/gdc/account/profile/{id}";
2431
public static final String ACCOUNTS_URI = "/gdc/account/domains/{organization_name}/users";
32+
public static final String ACCOUNT_BY_EMAIL_URI = ACCOUNTS_URI + "?login={email}";
2533

2634
public static final String LOGIN_URI = "/gdc/account/login/{id}";
2735

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.account;
7+
8+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
9+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
10+
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
11+
import com.fasterxml.jackson.annotation.JsonTypeName;
12+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
13+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
14+
import com.gooddata.sdk.common.collections.Page;
15+
import com.gooddata.sdk.common.collections.Paging;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
/**
21+
* List of accounts. Deserialization only.
22+
*/
23+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = Id.NAME)
24+
@JsonTypeName(Accounts.ROOT_NODE)
25+
@JsonIgnoreProperties(ignoreUnknown = true)
26+
@JsonDeserialize(using = AccountsDeserializer.class)
27+
public class Accounts extends Page<Account> {
28+
29+
static final String ROOT_NODE = "accountSettings";
30+
31+
32+
Accounts(final List<Account> items, final Paging paging, final Map<String, String> links) {
33+
super(items, paging, links);
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.account;
7+
8+
import com.gooddata.sdk.common.collections.PageDeserializer;
9+
import com.gooddata.sdk.common.collections.Paging;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
class AccountsDeserializer extends PageDeserializer<Accounts, Account> {
15+
16+
protected AccountsDeserializer() {
17+
super(Account.class);
18+
}
19+
20+
@Override
21+
protected Accounts createPage(final List<Account> items, final Paging paging, final Map<String, String> links) {
22+
return new Accounts(items, paging, links);
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.sdk.model.account;
7+
8+
import org.hamcrest.Matchers;
9+
import org.testng.annotations.Test;
10+
11+
import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
12+
import static org.hamcrest.CoreMatchers.is;
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.contains;
15+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
16+
17+
public class AccountsTest {
18+
19+
private static final String EMAIL = "example@company.com";
20+
private static final String FIRST_NAME = "Blah";
21+
private static final String LAST_NAME = "Muhehe";
22+
private static final String IP = "1.2.3.4/32";
23+
24+
private final Accounts accounts = readObjectFromResource("/account/accounts.json", Accounts.class);
25+
26+
27+
@Test
28+
public void testDeserialization() throws Exception {
29+
assertThat(accounts, Matchers.notNullValue());
30+
assertThat(accounts.getPageItems(), hasSize(1));
31+
final Account account = accounts.getPageItems().get(0);
32+
assertThat(account.getFirstName(), is(FIRST_NAME));
33+
assertThat(account.getLastName(), is(LAST_NAME));
34+
assertThat(account.getId(), is("ID"));
35+
assertThat(account.getUri(), is("/gdc/account/profile/ID"));
36+
assertThat(account.getProjectsUri(), is("/gdc/account/profile/ID/projects"));
37+
assertThat(account.getIpWhitelist(), contains(IP));
38+
assertThat(account.getEmail(), is(EMAIL));
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"accountSettings": {
3+
"items": [
4+
{
5+
"accountSetting": {
6+
"companyName": null,
7+
"country": null,
8+
"created": "2014-02-24 18:51:53",
9+
"firstName": "Blah",
10+
"lastName": "Muhehe",
11+
"login": "example@company.com",
12+
"phoneNumber": null,
13+
"position": null,
14+
"timezone": null,
15+
"updated": "2014-02-24 18:51:53",
16+
"email": "example@company.com",
17+
"language": "language",
18+
"ipWhitelist": ["1.2.3.4/32"],
19+
"effectiveIpWhitelist": null,
20+
"links": {
21+
"projects": "/gdc/account/profile/ID/projects",
22+
"self": "/gdc/account/profile/ID"
23+
}
24+
}
25+
}
26+
],
27+
"paging": {
28+
"offset": 0,
29+
"count": 1
30+
}
31+
32+
}
33+
34+
}

gooddata-java/src/main/java/com/gooddata/sdk/service/account/AccountNotFoundException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public AccountNotFoundException(String uri, GoodDataRestException cause) {
2020
this.accountUri = uri;
2121
}
2222

23+
public AccountNotFoundException(final String message,
24+
final String accountUri) {
25+
super(message + " failed on " + accountUri);
26+
this.accountUri = accountUri;
27+
}
28+
2329
public String getAccountUri() {
2430
return accountUri;
2531
}

gooddata-java/src/main/java/com/gooddata/sdk/service/account/AccountService.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import com.gooddata.sdk.common.GoodDataException;
99
import com.gooddata.sdk.common.GoodDataRestException;
1010
import com.gooddata.sdk.model.account.Account;
11+
import com.gooddata.sdk.model.account.Accounts;
1112
import com.gooddata.sdk.model.account.SeparatorSettings;
1213
import com.gooddata.sdk.model.gdc.UriResponse;
1314
import com.gooddata.sdk.service.AbstractService;
1415
import com.gooddata.sdk.service.GoodDataSettings;
16+
1517
import org.springframework.http.HttpStatus;
1618
import org.springframework.http.converter.json.MappingJacksonValue;
1719
import org.springframework.web.client.RestClientException;
@@ -29,6 +31,7 @@ public class AccountService extends AbstractService {
2931

3032
public static final UriTemplate ACCOUNT_TEMPLATE = new UriTemplate(Account.URI);
3133
public static final UriTemplate ACCOUNTS_TEMPLATE = new UriTemplate(Account.ACCOUNTS_URI);
34+
public static final UriTemplate ACCOUNT_BY_LOGIN_TEMPLATE = new UriTemplate(Account.ACCOUNT_BY_EMAIL_URI);
3235
public static final UriTemplate LOGIN_TEMPLATE = new UriTemplate(Account.LOGIN_URI);
3336
public static final UriTemplate SEPARATORS_TEMPLATE = new UriTemplate(SeparatorSettings.URI);
3437

@@ -130,6 +133,31 @@ public Account getAccountById(final String id) {
130133
}
131134
}
132135

136+
/**
137+
* Get account by given login.
138+
* Only domain admin is allowed to search users by login.
139+
* @param email used as login
140+
* @param organizationName (domain) in which account is present
141+
* @return account found by given login
142+
* @throws AccountNotFoundException when given account wasn't found
143+
* @throws GoodDataException when different error occurs
144+
*/
145+
public Account getAccountByLogin(final String email, final String organizationName) {
146+
notNull(email, "email");
147+
notNull(organizationName, "organizationName");
148+
try {
149+
final Accounts accounts = restTemplate.getForObject(
150+
Account.ACCOUNT_BY_EMAIL_URI, Accounts.class, organizationName, email);
151+
if (accounts != null && !accounts.getPageItems().isEmpty()) {
152+
return accounts.getPageItems().get(0);
153+
}
154+
throw new AccountNotFoundException("User was not found by email " +
155+
email + " in organization " + organizationName, Account.ACCOUNT_BY_EMAIL_URI);
156+
} catch (RestClientException e) {
157+
throw new GoodDataException("Unable to get account", e);
158+
}
159+
}
160+
133161
/**
134162
* Get account for given account id
135163
* @param uri to search for

gooddata-java/src/test/java/com/gooddata/sdk/service/account/AccountServiceAT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public void getAccount() {
6060
assertThat(foundAccount.getLogin(), is(account.getLogin()));
6161
}
6262

63+
@Test(groups = "isolated_domain", dependsOnMethods = "createAccount")
64+
public void getAccountByLogin() {
65+
final Account foundAccount = accountService.getAccountByLogin(LOGIN, getProperty("domain"));
66+
67+
assertThat(foundAccount, is(notNullValue()));
68+
assertThat(foundAccount.getId(), is(notNullValue()));
69+
assertThat(foundAccount.getId(), is(account.getId()));
70+
assertThat(foundAccount.getLogin(), is(LOGIN));
71+
assertThat(foundAccount.getLogin(), is(account.getLogin()));
72+
}
73+
6374
@Test(groups = "isolated_domain", dependsOnMethods = "getAccount")
6475
public void getSeparatorSettings() {
6576
final SeparatorSettings separators = accountService.getSeparatorSettings(account);

gooddata-java/src/test/java/com/gooddata/sdk/service/account/AccountServiceIT.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77

88
import com.gooddata.sdk.common.GoodDataException;
99
import com.gooddata.sdk.model.account.Account;
10+
import com.gooddata.sdk.model.account.Accounts;
1011
import com.gooddata.sdk.model.account.SeparatorSettings;
1112
import com.gooddata.sdk.service.AbstractGoodDataIT;
13+
1214
import org.testng.annotations.BeforeClass;
1315
import org.testng.annotations.Test;
1416

1517
import java.util.Collections;
1618
import java.util.List;
1719

18-
import static com.gooddata.sdk.common.util.ResourceUtils.*;
20+
import static com.gooddata.sdk.common.util.ResourceUtils.readFromResource;
21+
import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
22+
import static com.gooddata.sdk.common.util.ResourceUtils.readStringFromResource;
1923
import static com.gooddata.sdk.model.account.Account.AuthenticationMode.SSO;
2024
import static net.jadler.Jadler.onRequest;
2125
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
@@ -25,16 +29,24 @@
2529

2630
public class AccountServiceIT extends AbstractGoodDataIT {
2731

32+
private static final String DOMAIN = "default";
2833
private static final String CREATE_ACCOUNT = "/account/create-account.json";
2934
public static final String ACCOUNT = "/account/account.json";
35+
private static final String ACCOUNT_BY_EMAIL = "/account/account-by-email.json";
36+
private static final String ACCOUNT_BY_EMAIL_EMPTY_RESPONSE = "/account/account-by-email-empty-response.json";
3037
private static final String ACCOUNT_UPDATE = "/account/update-account.json";
3138
private static final String SEPARATORS = "/account/separators.json";
3239
private static final String ACCOUNT_ID = "ID";
3340
private static final String ACCOUNT_URI = AccountService.ACCOUNT_TEMPLATE.expand(ACCOUNT_ID).toString();
41+
private static final String LOGIN_EMAIL = "example@company.com";
42+
private static final String ACCOUNT_BY_EMAIL_URI = AccountService.ACCOUNTS_TEMPLATE
43+
.expand(DOMAIN).toString();
44+
45+
3446
private static final String SEPARATORS_URI = AccountService.SEPARATORS_TEMPLATE.expand(ACCOUNT_ID).toString();
3547
public static final String CURRENT_ACCOUNT_URI = AccountService.ACCOUNT_TEMPLATE.expand(Account.CURRENT_ID).toString();
3648
private static final String LOGOUT_CURRENT = AccountService.LOGIN_TEMPLATE.expand(ACCOUNT_ID).toString();
37-
private static final String DOMAIN = "default";
49+
3850

3951
private static Account account;
4052
private static Account createAccount;
@@ -187,6 +199,33 @@ public void shouldGetAccount() {
187199
assertThat(created.getFirstName(), is("Blah"));
188200
}
189201

202+
@Test
203+
public void shouldGetAccountByEmail() {
204+
onRequest()
205+
.havingMethodEqualTo("GET")
206+
.havingPathEqualTo(ACCOUNT_BY_EMAIL_URI)
207+
.havingQueryStringEqualTo("login=" + LOGIN_EMAIL)
208+
.respond()
209+
.withBody(readFromResource(ACCOUNT_BY_EMAIL))
210+
.withStatus(200);
211+
212+
final Account loaded = gd.getAccountService().getAccountByLogin(LOGIN_EMAIL, DOMAIN);
213+
assertThat(loaded.getFirstName(), is("John"));
214+
}
215+
216+
@Test(expectedExceptions = AccountNotFoundException.class)
217+
public void shouldGetEmptyPageWhenAccountByEmailDoesNotExist() {
218+
onRequest()
219+
.havingMethodEqualTo("GET")
220+
.havingPathEqualTo(ACCOUNT_BY_EMAIL_URI)
221+
.havingQueryStringEqualTo("login=wrong@email.com")
222+
.respond()
223+
.withBody(readFromResource(ACCOUNT_BY_EMAIL_EMPTY_RESPONSE))
224+
.withStatus(200);
225+
226+
gd.getAccountService().getAccountByLogin("wrong@email.com", DOMAIN);
227+
}
228+
190229
@Test(expectedExceptions = AccountNotFoundException.class)
191230
public void shouldFailToFindAccount() {
192231
onRequest()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"accountSettings": {
3+
"paging": {
4+
"offset": 0,
5+
"count": 0
6+
},
7+
"items": []
8+
}
9+
}

0 commit comments

Comments
 (0)