Compare commits

..

2 Commits

24 changed files with 214 additions and 102 deletions

View File

@@ -110,6 +110,21 @@ public class MongoTicketRepository implements LotteryTicketRepository {
return result.getInteger("seq");
}
/**
* @return mongo client
*/
public MongoClient getMongoClient() {
return mongoClient;
}
/**
*
* @return mongo database
*/
public MongoDatabase getMongoDatabase() {
return database;
}
/**
*
* @return tickets collection

View File

@@ -22,13 +22,12 @@
*/
package com.iluwatar.hexagonal.domain;
import com.google.common.base.Joiner;
import java.util.Collections;
import java.util.HashSet;
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.Set;
import java.util.Iterator;
/**
*
@@ -85,7 +84,15 @@ public class LotteryNumbers {
* @return numbers as comma separated string
*/
public String getNumbersAsString() {
return Joiner.on(',').join(numbers);
StringBuilder builder = new StringBuilder();
Iterator<Integer> iterator = numbers.iterator();
for (int i = 0; i < NUM_NUMBERS; i++) {
builder.append(iterator.next());
if (i < NUM_NUMBERS - 1) {
builder.append(",");
}
}
return builder.toString();
}
/**

View File

@@ -22,18 +22,17 @@
*/
package com.iluwatar.hexagonal.domain;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Lottery ticked id
*/
public class LotteryTicketId {
private static AtomicInteger numAllocated = new AtomicInteger(0);
private static volatile int numAllocated;
private final int id;
public LotteryTicketId() {
this.id = numAllocated.incrementAndGet();
this.id = numAllocated + 1;
numAllocated++;
}
public LotteryTicketId(int id) {

View File

@@ -27,10 +27,10 @@ import org.junit.jupiter.api.Test;
/**
* Unit test for simple App.
*/
class AppTest {
public class AppTest {
@Test
void testApp() {
public void testApp() {
String[] args = {};
App.main(args);
}

View File

@@ -32,19 +32,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for banking
*
*/
class InMemoryBankTest {
public class InMemoryBankTest {
private final WireTransfers bank = new InMemoryBank();
@Test
void testInit() {
assertEquals(0, bank.getFunds("foo"));
public void testInit() {
assertEquals(bank.getFunds("foo"), 0);
bank.setFunds("foo", 100);
assertEquals(100, bank.getFunds("foo"));
assertEquals(bank.getFunds("foo"), 100);
bank.setFunds("bar", 150);
assertEquals(150, bank.getFunds("bar"));
assertEquals(bank.getFunds("bar"), 150);
assertTrue(bank.transferFunds(50, "bar", "foo"));
assertEquals(150, bank.getFunds("foo"));
assertEquals(100, bank.getFunds("bar"));
assertEquals(bank.getFunds("foo"), 150);
assertEquals(bank.getFunds("bar"), 100);
}
}

View File

@@ -34,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests for Mongo banking adapter
*/
@Disabled
class MongoBankTest {
public class MongoBankTest {
private static final String TEST_DB = "lotteryDBTest";
private static final String TEST_ACCOUNTS_COLLECTION = "testAccounts";
@@ -42,7 +42,7 @@ class MongoBankTest {
private MongoBank mongoBank;
@BeforeEach
void init() {
public void init() {
MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port")));
@@ -52,12 +52,12 @@ class MongoBankTest {
}
@Test
void testSetup() {
public void testSetup() {
assertEquals(0, mongoBank.getAccountsCollection().count());
}
@Test
void testFundTransfers() {
public void testFundTransfers() {
assertEquals(0, mongoBank.getFunds("000-000"));
mongoBank.setFunds("000-000", 10);
assertEquals(10, mongoBank.getFunds("000-000"));

View File

@@ -38,23 +38,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for {@link LotteryTicketRepository}
*
*/
class InMemoryTicketRepositoryTest {
public class InMemoryTicketRepositoryTest {
private final LotteryTicketRepository repository = new InMemoryTicketRepository();
@BeforeEach
void clear() {
public void clear() {
repository.deleteAll();
}
@Test
void testCrudOperations() {
public void testCrudOperations() {
LotteryTicketRepository repository = new InMemoryTicketRepository();
assertTrue(repository.findAll().isEmpty());
assertEquals(repository.findAll().size(), 0);
LotteryTicket ticket = LotteryTestUtils.createLotteryTicket();
Optional<LotteryTicketId> id = repository.save(ticket);
assertTrue(id.isPresent());
assertEquals(1, repository.findAll().size());
assertEquals(repository.findAll().size(), 1);
Optional<LotteryTicket> optionalTicket = repository.findById(id.get());
assertTrue(optionalTicket.isPresent());
}

View File

@@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for Mongo based ticket repository
*/
@Disabled
class MongoTicketRepositoryTest {
public class MongoTicketRepositoryTest {
private static final String TEST_DB = "lotteryTestDB";
private static final String TEST_TICKETS_COLLECTION = "lotteryTestTickets";
@@ -50,7 +50,7 @@ class MongoTicketRepositoryTest {
private MongoTicketRepository repository;
@BeforeEach
void init() {
public void init() {
MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port")));
@@ -61,20 +61,20 @@ class MongoTicketRepositoryTest {
}
@Test
void testSetup() {
assertEquals(1, repository.getCountersCollection().count());
assertEquals(0, repository.getTicketsCollection().count());
public void testSetup() {
assertTrue(repository.getCountersCollection().count() == 1);
assertTrue(repository.getTicketsCollection().count() == 0);
}
@Test
void testNextId() {
public void testNextId() {
assertEquals(1, repository.getNextId());
assertEquals(2, repository.getNextId());
assertEquals(3, repository.getNextId());
}
@Test
void testCrudOperations() {
public void testCrudOperations() {
// create new lottery ticket and save it
PlayerDetails details = new PlayerDetails("foo@bar.com", "123-123", "07001234");
LotteryNumbers random = LotteryNumbers.createRandom();

View File

@@ -28,7 +28,7 @@ import java.util.Arrays;
import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -37,10 +37,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Unit tests for {@link LotteryNumbers}
*
*/
class LotteryNumbersTest {
public class LotteryNumbersTest {
@Test
void testGivenNumbers() {
public void testGivenNumbers() {
LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertEquals(numbers.getNumbers().size(), 4);
@@ -51,7 +51,7 @@ class LotteryNumbersTest {
}
@Test
void testNumbersCantBeModified() {
public void testNumbersCantBeModified() {
LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertThrows(UnsupportedOperationException.class, () -> {
@@ -60,20 +60,20 @@ class LotteryNumbersTest {
}
@Test
void testRandomNumbers() {
public void testRandomNumbers() {
LotteryNumbers numbers = LotteryNumbers.createRandom();
assertEquals(numbers.getNumbers().size(), LotteryNumbers.NUM_NUMBERS);
}
@Test
void testEquals() {
public void testEquals() {
LotteryNumbers numbers1 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
LotteryNumbers numbers2 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertEquals(numbers1, numbers2);
assertTrue(numbers1.equals(numbers2));
LotteryNumbers numbers3 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(11, 12, 13, 14)));
assertNotEquals(numbers1, numbers3);
assertFalse(numbers1.equals(numbers3));
}
}

View File

@@ -38,7 +38,6 @@ import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -46,7 +45,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Test the lottery system
*
*/
class LotteryTest {
public class LotteryTest {
private Injector injector;
@Inject
@@ -56,22 +55,22 @@ class LotteryTest {
@Inject
private WireTransfers wireTransfers;
LotteryTest() {
public LotteryTest() {
this.injector = Guice.createInjector(new LotteryTestingModule());
}
@BeforeEach
void setup() {
public void setup() {
injector.injectMembers(this);
// add funds to the test player's bank account
wireTransfers.setFunds("123-12312", 100);
}
@Test
void testLottery() {
public void testLottery() {
// admin resets the lottery
administration.resetLottery();
assertEquals(0, administration.getAllSubmittedTickets().size());
assertEquals(administration.getAllSubmittedTickets().size(), 0);
// players submit the lottery tickets
Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com",
@@ -83,7 +82,7 @@ class LotteryTest {
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com",
"123-12312", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
assertTrue(ticket3.isPresent());
assertEquals(3, administration.getAllSubmittedTickets().size());
assertEquals(administration.getAllSubmittedTickets().size(), 3);
// perform lottery
LotteryNumbers winningNumbers = administration.performLottery();
@@ -92,23 +91,23 @@ class LotteryTest {
Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com",
"123-12312", "+12421255", winningNumbers.getNumbers()));
assertTrue(ticket4.isPresent());
assertEquals(4, administration.getAllSubmittedTickets().size());
assertEquals(administration.getAllSubmittedTickets().size(), 4);
// check winners
Map<LotteryTicketId, LotteryTicket> tickets = administration.getAllSubmittedTickets();
for (LotteryTicketId id: tickets.keySet()) {
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(id, winningNumbers);
assertNotEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
assertTrue(checkResult.getResult() != CheckResult.TICKET_NOT_SUBMITTED);
if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
assertTrue(checkResult.getPrizeAmount() > 0);
} else if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
assertEquals(0, checkResult.getPrizeAmount());
assertEquals(checkResult.getPrizeAmount(), 0);
}
}
// check another ticket that has not been submitted
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(new LotteryTicketId(), winningNumbers);
assertEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
assertEquals(0, checkResult.getPrizeAmount());
assertTrue(checkResult.getResult() == CheckResult.TICKET_NOT_SUBMITTED);
assertEquals(checkResult.getPrizeAmount(), 0);
}
}

View File

@@ -26,21 +26,21 @@ import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
*
* Unit tests for {@link LotteryTicketCheckResult}
*
*/
class LotteryTicketCheckResultTest {
public class LotteryTicketCheckResultTest {
@Test
void testEquals() {
public void testEquals() {
LotteryTicketCheckResult result1 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
LotteryTicketCheckResult result2 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
assertEquals(result1, result2);
LotteryTicketCheckResult result3 = new LotteryTicketCheckResult(CheckResult.WIN_PRIZE, 300000);
assertNotEquals(result1, result3);
assertFalse(result1.equals(result3));
}
}

View File

@@ -24,22 +24,22 @@ package com.iluwatar.hexagonal.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests for lottery ticket id
*/
class LotteryTicketIdTest {
public class LotteryTicketIdTest {
@Test
void testEquals() {
public void testEquals() {
LotteryTicketId ticketId1 = new LotteryTicketId();
LotteryTicketId ticketId2 = new LotteryTicketId();
LotteryTicketId ticketId3 = new LotteryTicketId();
assertNotEquals(ticketId1, ticketId2);
assertNotEquals(ticketId2, ticketId3);
assertFalse(ticketId1.equals(ticketId2));
assertFalse(ticketId2.equals(ticketId3));
LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId());
assertEquals(ticketId1, ticketId4);
assertTrue(ticketId1.equals(ticketId4));
}
}

View File

@@ -29,15 +29,14 @@ import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* Test Lottery Tickets for equality
*/
class LotteryTicketTest {
public class LotteryTicketTest {
@Test
void testEquals() {
public void testEquals() {
PlayerDetails details1 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers1 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4)));
LotteryTicket ticket1 = new LotteryTicket(new LotteryTicketId(), details1, numbers1);
@@ -48,6 +47,6 @@ class LotteryTicketTest {
PlayerDetails details3 = new PlayerDetails("elsa@foo.bar", "1223-121212", "+49332322");
LotteryNumbers numbers3 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 8)));
LotteryTicket ticket3 = new LotteryTicket(new LotteryTicketId(), details3, numbers3);
assertNotEquals(ticket1, ticket3);
assertFalse(ticket1.equals(ticket3));
}
}

View File

@@ -25,21 +25,21 @@ package com.iluwatar.hexagonal.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
*
* Unit tests for {@link PlayerDetails}
*
*/
class PlayerDetailsTest {
public class PlayerDetailsTest {
@Test
void testEquals() {
public void testEquals() {
PlayerDetails details1 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425");
PlayerDetails details2 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425");
assertEquals(details1, details2);
PlayerDetails details3 = new PlayerDetails("john@foo.bar", "16412-123439", "+34323432");
assertNotEquals(details1, details3);
assertFalse(details1.equals(details3));
}
}

View File

@@ -35,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests for Mongo event log
*/
@Disabled
class MongoEventLogTest {
public class MongoEventLogTest {
private static final String TEST_DB = "lotteryDBTest";
private static final String TEST_EVENTS_COLLECTION = "testEvents";
@@ -43,7 +43,7 @@ class MongoEventLogTest {
private MongoEventLog mongoEventLog;
@BeforeEach
void init() {
public void init() {
MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port")));
@@ -53,12 +53,12 @@ class MongoEventLogTest {
}
@Test
void testSetup() {
public void testSetup() {
assertEquals(0, mongoEventLog.getEventsCollection().count());
}
@Test
void testFundTransfers() {
public void testFundTransfers() {
PlayerDetails playerDetails = new PlayerDetails("john@wayne.com", "000-000", "03432534543");
mongoEventLog.prizeError(playerDetails, 1000);
assertEquals(1, mongoEventLog.getEventsCollection().count());

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.2" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.throttling.CallsCount" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java" binary="false" corner="BOTTOM_RIGHT">
<position height="211" width="256" x="656" y="228"/>
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.throttling.Tenant" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/Tenant.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="465" y="524"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.throttling.B2BService" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/B2BService.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="464" y="192"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="4" language="java" name="com.iluwatar.throttling.timer.Throttler" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/timer/Throttler.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="167" y="174"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="5" language="java" name="com.iluwatar.throttling.timer.ThrottleTimerImpl" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/timer/ThrottleTimerImpl.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="166" y="396"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="6">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="7" name="callsCount"/>
<multiplicity id="8" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="9">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</dependency>
<dependency id="10">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</dependency>
<association id="11">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="12" name="callsCount"/>
<multiplicity id="13" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="14">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</dependency>
<realization id="15">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="4"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@@ -53,14 +53,14 @@ public class App {
* @param args main arguments
*/
public static void main(String[] args) {
Tenant adidas = new Tenant("Adidas", 5);
Tenant nike = new Tenant("Nike", 6);
CallsCount callsCount = new CallsCount();
Tenant adidas = new Tenant("Adidas", 5, callsCount);
Tenant nike = new Tenant("Nike", 6, callsCount);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> makeServiceCalls(adidas));
executorService.execute(() -> makeServiceCalls(nike));
executorService.execute(() -> makeServiceCalls(adidas, callsCount));
executorService.execute(() -> makeServiceCalls(nike, callsCount));
executorService.shutdown();
try {
@@ -73,9 +73,9 @@ public class App {
/**
* Make calls to the B2BService dummy API
*/
private static void makeServiceCalls(Tenant tenant) {
Throttler timer = new ThrottleTimerImpl(10);
B2BService service = new B2BService(timer);
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
Throttler timer = new ThrottleTimerImpl(10, callsCount);
B2BService service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.

View File

@@ -35,8 +35,10 @@ import java.util.concurrent.ThreadLocalRandom;
class B2BService {
private static final Logger LOGGER = LoggerFactory.getLogger(B2BService.class);
private final CallsCount callsCount;
public B2BService(Throttler timer) {
public B2BService(Throttler timer, CallsCount callsCount) {
this.callsCount = callsCount;
timer.start();
}
@@ -46,13 +48,13 @@ class B2BService {
*/
public int dummyCustomerApi(Tenant tenant) {
String tenantName = tenant.getName();
long count = CallsCount.getCount(tenantName);
long count = callsCount.getCount(tenantName);
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
if (count >= tenant.getAllowedCallsPerSecond()) {
LOGGER.error("API access per second limit reached for: {}", tenantName);
return -1;
}
CallsCount.incrementCount(tenantName);
callsCount.incrementCount(tenantName);
return getRandomCustomerId();
}

View File

@@ -38,13 +38,13 @@ import java.util.concurrent.atomic.AtomicLong;
public final class CallsCount {
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
private Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
/**
* Add a new tenant to the map.
* @param tenantName name of the tenant.
*/
public static void addTenant(String tenantName) {
public void addTenant(String tenantName) {
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
}
@@ -52,7 +52,7 @@ public final class CallsCount {
* Increment the count of the specified tenant.
* @param tenantName name of the tenant.
*/
public static void incrementCount(String tenantName) {
public void incrementCount(String tenantName) {
tenantCallsCount.get(tenantName).incrementAndGet();
}
@@ -61,14 +61,14 @@ public final class CallsCount {
* @param tenantName name of the tenant.
* @return the count of the tenant.
*/
public static long getCount(String tenantName) {
public long getCount(String tenantName) {
return tenantCallsCount.get(tenantName).get();
}
/**
* Resets the count of all the tenants in the map.
*/
public static void reset() {
public void reset() {
LOGGER.debug("Resetting the map.");
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
tenantCallsCount.put(e.getKey(), new AtomicLong(0));

View File

@@ -38,13 +38,13 @@ public class Tenant {
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
* @throws InvalidParameterException If number of calls is less than 0, throws exception.
*/
public Tenant(String name, int allowedCallsPerSecond) {
public Tenant(String name, int allowedCallsPerSecond, CallsCount callsCount) {
if (allowedCallsPerSecond < 0) {
throw new InvalidParameterException("Number of calls less than 0 not allowed");
}
this.name = name;
this.allowedCallsPerSecond = allowedCallsPerSecond;
CallsCount.addTenant(name);
callsCount.addTenant(name);
}
public String getName() {

View File

@@ -37,10 +37,12 @@ import com.iluwatar.throttling.CallsCount;
*/
public class ThrottleTimerImpl implements Throttler {
private int throttlePeriod;
public ThrottleTimerImpl(int throttlePeriod) {
private final int throttlePeriod;
private final CallsCount callsCount;
public ThrottleTimerImpl(int throttlePeriod, CallsCount callsCount) {
this.throttlePeriod = throttlePeriod;
this.callsCount = callsCount;
}
/**
@@ -51,7 +53,7 @@ public class ThrottleTimerImpl implements Throttler {
new Timer(true).schedule(new TimerTask() {
@Override
public void run() {
CallsCount.reset();
callsCount.reset();
}
}, 0, throttlePeriod);
}

View File

@@ -33,18 +33,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class B2BServiceTest {
@Disabled
private CallsCount callsCount = new CallsCount();
@Test
public void dummyCustomerApiTest() {
Tenant tenant = new Tenant("testTenant", 2);
Tenant tenant = new Tenant("testTenant", 2, callsCount);
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
Throttler timer = () -> { };
B2BService service = new B2BService(timer);
B2BService service = new B2BService(timer, callsCount);
for (int i = 0; i < 5; i++) {
service.dummyCustomerApi(tenant);
}
long counter = CallsCount.getCount(tenant.getName());
long counter = callsCount.getCount(tenant.getName());
assertEquals(2, counter, "Counter limit must be reached");
}
}

View File

@@ -36,7 +36,7 @@ public class TenantTest {
@Test
public void constructorTest() {
assertThrows(InvalidParameterException.class, () -> {
Tenant tenant = new Tenant("FailTenant", -1);
Tenant tenant = new Tenant("FailTenant", -1, new CallsCount());
});
}
}