Skip to content

Commit c1c8bef

Browse files
authored
Merge pull request #12 from railsware/feature/accounts
Implemented Accounts API
2 parents 7f0a72d + 382392b commit c1c8bef

File tree

8 files changed

+133
-1
lines changed

8 files changed

+133
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.mailtrap.api;
2+
3+
import io.mailtrap.Constants;
4+
import io.mailtrap.api.abstractions.Accounts;
5+
import io.mailtrap.api.abstractions.classes.ApiResource;
6+
import io.mailtrap.config.MailtrapConfig;
7+
import io.mailtrap.http.RequestData;
8+
import io.mailtrap.model.response.AccountsResponse;
9+
10+
import java.util.List;
11+
12+
public class AccountsImpl extends ApiResource implements Accounts {
13+
14+
public AccountsImpl(MailtrapConfig config) {
15+
super(config);
16+
this.apiHost = Constants.GENERAL_HOST;
17+
}
18+
19+
@Override
20+
public List<AccountsResponse> getAllAccounts() {
21+
return httpClient.getList(
22+
String.format(apiHost + "/api/accounts"),
23+
new RequestData(),
24+
AccountsResponse.class
25+
);
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.mailtrap.api.abstractions;
2+
3+
import io.mailtrap.model.response.AccountsResponse;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Interface representing the Mailtrap Testing API for interaction with accounts
9+
*/
10+
public interface Accounts {
11+
12+
/**
13+
* Get a list of your Mailtrap accounts.
14+
*
15+
* @return the list of accounts to which the API token has access
16+
*/
17+
List<AccountsResponse> getAllAccounts();
18+
19+
}

src/main/java/io/mailtrap/client/layers/MailtrapGeneralApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.mailtrap.client.layers;
22

33
import io.mailtrap.api.abstractions.AccountAccesses;
4+
import io.mailtrap.api.abstractions.Accounts;
45
import lombok.Getter;
56
import lombok.RequiredArgsConstructor;
67
import lombok.experimental.Accessors;
@@ -13,4 +14,5 @@
1314
@RequiredArgsConstructor
1415
public class MailtrapGeneralApi {
1516
private final AccountAccesses accountAccesses;
17+
private final Accounts accounts;
1618
}

src/main/java/io/mailtrap/factory/MailtrapClientFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public static MailtrapClient createMailtrapClient(MailtrapConfig config) {
4141

4242
private static MailtrapGeneralApi createGeneralApi(MailtrapConfig config) {
4343
var accountAccess = new AccountAccessesImpl(config);
44+
var accounts = new AccountsImpl(config);
4445

45-
return new MailtrapGeneralApi(accountAccess);
46+
return new MailtrapGeneralApi(accountAccess, accounts);
4647
}
4748

4849
private static SendingContextHolder configureSendingContext(MailtrapConfig config) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.mailtrap.model.response;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
6+
import java.util.List;
7+
8+
@Data
9+
public class AccountsResponse {
10+
11+
private long id;
12+
13+
private String name;
14+
15+
@JsonProperty("access_levels")
16+
private List<AccessLevel> accessLevels;
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.mailtrap.api;
2+
3+
import io.mailtrap.Constants;
4+
import io.mailtrap.api.abstractions.Accounts;
5+
import io.mailtrap.config.MailtrapConfig;
6+
import io.mailtrap.factory.MailtrapClientFactory;
7+
import io.mailtrap.model.response.AccessLevel;
8+
import io.mailtrap.model.response.AccountsResponse;
9+
import io.mailtrap.testutils.BaseTest;
10+
import io.mailtrap.testutils.DataMock;
11+
import io.mailtrap.testutils.TestHttpClient;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.List;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
class AccountsImplTest extends BaseTest {
21+
22+
private Accounts api;
23+
24+
@BeforeEach
25+
public void init() {
26+
TestHttpClient httpClient = new TestHttpClient(List.of(
27+
DataMock.build(Constants.GENERAL_HOST + "/api/accounts",
28+
"GET", null, "api/accounts/getAllAccountsResponse.json")
29+
));
30+
31+
MailtrapConfig testConfig = new MailtrapConfig.Builder()
32+
.httpClient(httpClient)
33+
.token("dummy_token")
34+
.build();
35+
36+
api = MailtrapClientFactory.createMailtrapClient(testConfig).generalApi().accounts();
37+
}
38+
39+
@Test
40+
void test_getAllAccounts() {
41+
List<AccountsResponse> accounts = api.getAllAccounts();
42+
43+
assertEquals(2, accounts.size());
44+
assertEquals(accountId, accounts.get(0).getId());
45+
assertTrue(accounts.get(0).getAccessLevels().contains(AccessLevel.ADMIN));
46+
assertEquals(anotherAccountId, accounts.get(1).getId());
47+
assertTrue(accounts.get(1).getAccessLevels().contains(AccessLevel.OWNER));
48+
}
49+
}

src/test/java/io/mailtrap/testutils/BaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public class BaseTest {
44
protected final Long accountId = 1L;
5+
protected final Long anotherAccountId = 2L;
56
protected final long inboxId = 2;
67
protected final Long projectId = 2L;
78
protected final Long anotherProjectId = 2L;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "James",
5+
"access_levels": [
6+
100
7+
]
8+
},
9+
{
10+
"id": 2,
11+
"name": "John",
12+
"access_levels": [
13+
1000
14+
]
15+
}
16+
]

0 commit comments

Comments
 (0)