File size: 6,072 Bytes
2795186 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
package amlsim;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import amlsim.model.ModelParameters;
import amlsim.model.normal.SingleTransactionModel;
import sim.engine.Schedule;
import java.util.Random;
import java.util.logging.Logger;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
// How to mock static
// try (MockedStatic<AMLSim> mocked = mockStatic(AMLSim.class)) {
// mocked.when(AMLSim::getRandom).thenReturn(new Random(1));
class AccountTests {
public Schedule schedule;
public AMLSim amlSim;
public SimProperties simProperties;
public Random random;
@BeforeEach
void beforeEach()
{
this.schedule = mock(Schedule.class);
this.amlSim = mock(AMLSim.class);
this.amlSim.schedule = this.schedule;
this.simProperties = mock(SimProperties.class);
this.random = new Random(1);
}
@Test
void zeroSteps()
{
long step = 0;
when(this.schedule.getSteps()).thenReturn(step);
try (MockedStatic<AMLSim> mocked = mockStatic(AMLSim.class)) {
mocked.when(AMLSim::getRandom).thenReturn(new Random(1));
Account anAccount = new Account("1", 5, 1000.0f, "bankid", this.random);
anAccount.handleAction(amlSim);
mocked.verify(() -> AMLSim.handleTransaction(1, "TRANSFER", 1000.0f, anAccount, anAccount, false, 1), never());
}
}
@Test
void SingleTransactionModelBenefitListZero()
{
long step = 1;
when(this.schedule.getSteps()).thenReturn(step);
Account anAccount = new Account("1", 5, 1000.0f, "bankid", this.random);
Account beneAccount = new Account("2", 5, 1000.0f, "bankid", this.random);
try (MockedStatic<AMLSim> mocked = mockStatic(AMLSim.class))
{
mocked.when(AMLSim::getRandom).thenReturn(new Random(1));
anAccount.handleAction(amlSim);
mocked.verify(() -> AMLSim.handleTransaction(1L, "TRANSFER", 3.0, anAccount, beneAccount, false, -1L), never());
}
}
@Test
public void SingleTransactionModelBenefitListExists()
{
long step = 1;
when(this.schedule.getSteps()).thenReturn(step);
try (MockedStatic<AMLSim> mocked = mockStatic(AMLSim.class);
MockedStatic<ModelParameters> mockey = mockStatic(ModelParameters.class)
)
{
SimProperties mockedProperties = mock(SimProperties.class);
when(mockedProperties.getMaxTransactionAmount()).thenReturn(100.0);
mocked.when(AMLSim::getRandom).thenReturn(new Random(1));
mocked.when(AMLSim::getSimProp).thenReturn(
mockedProperties
);
mocked.when(AMLSim::getLogger).thenReturn(
Logger.getLogger("AMLSim")
);
mockey.when(() -> ModelParameters.shouldAddEdge(any(), any())).thenReturn(true);
Account anAccount = new Account("1", 5, 1000.0f, "bankid", this.random);
Account beneAccount = new Account("2", 5, 1000.0f, "bankid", this.random);
anAccount.addBeneAcct(beneAccount);
anAccount.addTxType(beneAccount, "TRANSFER");
AccountGroup accountGroup = new AccountGroup(1, this.amlSim);
accountGroup.addMember(anAccount);
accountGroup.addMember(beneAccount);
accountGroup.setMainAccount(anAccount);
SingleTransactionModel model = new SingleTransactionModel(accountGroup, this.random);
model.setParameters(30, 1, 1);
accountGroup.setModel(
model
);
anAccount.accountGroups.add(accountGroup);
anAccount.handleAction(amlSim);
mocked.verify(() -> AMLSim.handleTransaction(1L, "TRANSFER", 41.00808114922017d, anAccount, beneAccount, false, -1L), times(1));
}
}
@Test
public void SingleTransactionModelHasBenesNotMain()
{
long step = 1;
when(this.schedule.getSteps()).thenReturn(step);
try (MockedStatic<AMLSim> mocked = mockStatic(AMLSim.class);
MockedStatic<ModelParameters> mockey = mockStatic(ModelParameters.class)
)
{
SimProperties mockedProperties = mock(SimProperties.class);
when(mockedProperties.getMaxTransactionAmount()).thenReturn(100.0);
mocked.when(AMLSim::getRandom).thenReturn(new Random(1));
mocked.when(AMLSim::getSimProp).thenReturn(
mockedProperties
);
mocked.when(AMLSim::getLogger).thenReturn(
Logger.getLogger("AMLSim")
);
mockey.when(() -> ModelParameters.shouldAddEdge(any(), any())).thenReturn(true);
Account anAccount = new Account("1", 5, 1000.0f, "bankid", this.random);
Account beneAccount = new Account("2", 5, 1000.0f, "bankid", this.random);
anAccount.addBeneAcct(beneAccount);
anAccount.addTxType(beneAccount, "TRANSFER");
AccountGroup accountGroup = new AccountGroup(1, this.amlSim);
accountGroup.addMember(anAccount);
accountGroup.addMember(beneAccount);
accountGroup.setMainAccount(beneAccount);
SingleTransactionModel model = new SingleTransactionModel(accountGroup, this.random);
model.setParameters(30, 1, 1);
accountGroup.setModel(
model
);
anAccount.accountGroups.add(accountGroup);
anAccount.handleAction(amlSim);
mocked.verify(() -> AMLSim.handleTransaction(1L, "TRANSFER", 41.00808114922017d, anAccount, beneAccount, false, -1L), never());
}
}
}
|