### Question:
OperationStore { void add(CTSOperation operation) { OperationMonitor rate; synchronized (operationRate) { rate = operationRate.get(operation); if (rate == null) { rate = operationRateFactory.createOperationRate(); operationRate.put(operation, rate); } } rate.increment(); } OperationStore();   OperationStore(final OperationRateFactory operationRateFactory,
            final Map<CTSOperation, OperationMonitor> operationRate);   }### Answer:
@Test public void shouldAddOperation() { CTSOperation operation = CTSOperation.CREATE; OperationMonitor opRate = mock(OperationMonitor.class); given(operationRateFactory.createOperationRate()).willReturn(opRate); operationStore.add(operation); assertTrue(operationRate.containsKey(CTSOperation.CREATE)); verify(opRate).increment(); }
@Test public void shouldAddOperationToExistingOperationStore() { CTSOperation operation = CTSOperation.CREATE; OperationMonitor opRate = mock(OperationMonitor.class); operationRate.put(CTSOperation.CREATE, opRate); operationStore.add(operation); verifyZeroInteractions(operationRateFactory); verify(opRate).increment(); }
@Test public void shouldAddTokenOperationForSpecificTokenTypeUsingDefaultOperationStoreFactory() { OperationStore.OperationRateFactory operationRateFactory = new OperationStore.OperationRateFactory(); OperationStore localOperationStore = new OperationStore(operationRateFactory, operationRate); CTSOperation operation = CTSOperation.CREATE; localOperationStore.add(operation); assertTrue(operationRate.containsKey(CTSOperation.CREATE)); }
@Test public void shouldOnlyAllowSingleThreadToAddOperationRateToOperationRateMap() throws InterruptedException { final OperationMonitor opRate = mock(OperationMonitor.class); OperationStore.OperationRateFactory operationRateFactory = new OperationStore.OperationRateFactory() { @Override OperationMonitor createOperationRate() { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } return opRate; } }; final OperationStore localOperationStore = new OperationStore(operationRateFactory, operationRate); final CTSOperation operation = CTSOperation.CREATE; Runnable runnable = new Runnable() { public void run() { localOperationStore.add(operation); } }; Thread t1 = new Thread(runnable); t1.setName("t1"); Thread t2 = new Thread(runnable); t2.setName("t2"); t1.start(); t2.start(); t1.join(); t2.join(); verify(opRate, times(2)).increment(); assertEquals(operationRate.size(), 1); }