|
| 1 | +package org.apereo.openlrw.risk; |
| 2 | + |
| 3 | +import org.apereo.openlrw.FongoConfig; |
| 4 | +import org.apereo.openlrw.OpenLRW; |
| 5 | +import org.apereo.openlrw.risk.service.RiskService; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.test.context.ContextConfiguration; |
| 10 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 11 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 12 | + |
| 13 | + |
| 14 | +import java.text.ParseException; |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.time.Instant; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | + |
| 20 | +import static org.hamcrest.CoreMatchers.*; |
| 21 | +import static org.junit.Assert.assertThat; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author xchopin <xavier.chopin@univ-lorraine.fr> |
| 25 | + */ |
| 26 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 27 | +@ContextConfiguration(classes={OpenLRW.class, FongoConfig.class}) |
| 28 | +@WebAppConfiguration |
| 29 | +public class RiskServiceTest { |
| 30 | + @Autowired |
| 31 | + private RiskService unit; |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testSave() { |
| 35 | + MongoRisk risk = new MongoRisk.Builder() |
| 36 | + .withClassSourcedId("class-id") |
| 37 | + .withUserSourcedId("user-id") |
| 38 | + .withName("Risk name") |
| 39 | + .withDateTime(Instant.now()) |
| 40 | + .withVelocity("-1") |
| 41 | + .build(); |
| 42 | + |
| 43 | + MongoRisk savedRisk = unit.save("tenant-1","org-1", risk,true); |
| 44 | + assertThat(savedRisk, is(notNullValue())); |
| 45 | + assertThat(savedRisk.getSourcedId(), is(notNullValue())); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testFindByClassAndUser() { |
| 51 | + MongoRisk risk = new MongoRisk.Builder() |
| 52 | + .withClassSourcedId("class-id") |
| 53 | + .withUserSourcedId("user-id") |
| 54 | + .withName("Risk name") |
| 55 | + .withDateTime(Instant.now()) |
| 56 | + .withVelocity("-1") |
| 57 | + .build(); |
| 58 | + |
| 59 | + unit.save("tenant-1","org-1", risk, true); |
| 60 | + |
| 61 | + Collection<MongoRisk> found = unit.getRisksForUserAndClass("tenant-1","org-1", "class-id","user-id", ""); |
| 62 | + ArrayList<MongoRisk> list = new ArrayList<>(found); |
| 63 | + |
| 64 | + assertThat(found, is(notNullValue())); |
| 65 | + assertThat(list.get(0).getClassSourcedId(), is(equalTo("class-id"))); |
| 66 | + assertThat(list.get(0).getUserSourcedId(), is(equalTo("user-id"))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testFindByClassAndUserAndDate() throws ParseException { |
| 71 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| 72 | + Instant date = sdf.parse("2019-02-25 08:00").toInstant(); |
| 73 | + |
| 74 | + MongoRisk risk = new MongoRisk.Builder() |
| 75 | + .withClassSourcedId("class-id") |
| 76 | + .withUserSourcedId("user-id") |
| 77 | + .withName("8am risk") |
| 78 | + .withDateTime(date) |
| 79 | + .withVelocity("-1") |
| 80 | + .build(); |
| 81 | + unit.save("tenant-1","org-1", risk, true); |
| 82 | + |
| 83 | + date = sdf.parse("2019-02-25 18:00").toInstant(); |
| 84 | + risk = new MongoRisk.Builder() |
| 85 | + .withClassSourcedId("class-id") |
| 86 | + .withUserSourcedId("user-id") |
| 87 | + .withName("6pm risk") |
| 88 | + .withDateTime(date) |
| 89 | + .withVelocity("1") |
| 90 | + .build(); |
| 91 | + unit.save("tenant-1","org-1", risk, true); |
| 92 | + |
| 93 | + date = sdf.parse("2019-02-25 05:00").toInstant(); |
| 94 | + risk = new MongoRisk.Builder() |
| 95 | + .withClassSourcedId("class-id") |
| 96 | + .withUserSourcedId("user-id") |
| 97 | + .withName("5am risk") |
| 98 | + .withDateTime(date) |
| 99 | + .withVelocity("-1") |
| 100 | + .build(); |
| 101 | + unit.save("tenant-1","org-1", risk, true); |
| 102 | + |
| 103 | + |
| 104 | + Collection<MongoRisk> found = unit.getRisksForUserAndClass("tenant-1", "org-1", "class-id", "user-id", ""); |
| 105 | + ArrayList<MongoRisk> list = new ArrayList<>(found); |
| 106 | + |
| 107 | + |
| 108 | + assertThat(found, is(notNullValue())); |
| 109 | + assertThat(list.size(), is(4)); |
| 110 | + |
| 111 | + |
| 112 | + found = unit.getRisksForUserAndClass( |
| 113 | + "tenant-1", |
| 114 | + "org-1", |
| 115 | + "class-id", |
| 116 | + "user-id", |
| 117 | + "2019-02-25 05:00" |
| 118 | + ); |
| 119 | + list = new ArrayList<>(found); |
| 120 | + |
| 121 | + assertThat(found, is(notNullValue())); |
| 122 | + assertThat(list.get(0).getName(), containsString("5am")); |
| 123 | + assertThat(list.size(), is(1)); |
| 124 | + |
| 125 | + found = unit.getRisksForUserAndClass( |
| 126 | + "tenant-1", |
| 127 | + "org-1", |
| 128 | + "class-id", |
| 129 | + "user-id", |
| 130 | + "latest" |
| 131 | + ); |
| 132 | + |
| 133 | + list = new ArrayList<>(found); |
| 134 | + |
| 135 | + assertThat(found, is(notNullValue())); |
| 136 | + assertThat(list.get(0).getName(), containsString("6pm")); |
| 137 | + assertThat(list.size(), is(1)); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | +} |
0 commit comments