|
| 1 | +package io.openbas.rest; |
| 2 | + |
| 3 | +import static io.openbas.rest.inject_expectation_trace.InjectExpectationTraceApi.INJECT_EXPECTATION_TRACES_URI; |
| 4 | +import static io.openbas.utils.JsonUtils.asJsonString; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 8 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 9 | + |
| 10 | +import com.jayway.jsonpath.JsonPath; |
| 11 | +import io.openbas.IntegrationTest; |
| 12 | +import io.openbas.database.model.*; |
| 13 | +import io.openbas.database.model.SecurityPlatform.SECURITY_PLATFORM_TYPE; |
| 14 | +import io.openbas.database.repository.*; |
| 15 | +import io.openbas.rest.inject_expectation_trace.form.InjectExpectationTraceInput; |
| 16 | +import io.openbas.utils.fixtures.AssetFixture; |
| 17 | +import io.openbas.utils.fixtures.InjectExpectationFixture; |
| 18 | +import io.openbas.utils.fixtures.InjectFixture; |
| 19 | +import io.openbas.utils.mockUser.WithMockAdminUser; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.List; |
| 22 | +import java.util.UUID; |
| 23 | +import org.junit.jupiter.api.*; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.test.web.servlet.MockMvc; |
| 27 | +import org.springframework.transaction.annotation.Transactional; |
| 28 | + |
| 29 | +@TestInstance(PER_CLASS) |
| 30 | +@Transactional |
| 31 | +class InjectExpectationTraceApiTest extends IntegrationTest { |
| 32 | + |
| 33 | + @Autowired private MockMvc mvc; |
| 34 | + @Autowired private InjectRepository injectRepository; |
| 35 | + @Autowired private CollectorRepository collectorRepository; |
| 36 | + @Autowired private SecurityPlatformRepository securityPlatformRepository; |
| 37 | + @Autowired private InjectExpectationRepository injectExpectationRepository; |
| 38 | + @Autowired private InjectExpectationTraceRepository injectExpectationTraceRepository; |
| 39 | + @Autowired private AssetRepository assetRepository; |
| 40 | + |
| 41 | + private Collector savedCollector; |
| 42 | + private Inject savedInject; |
| 43 | + private InjectExpectation savedInjectExpectation; |
| 44 | + private Asset savedAsset; |
| 45 | + private SecurityPlatform savedSecurityPlatform; |
| 46 | + private InjectExpectationTrace savedInjectExpectationTrace1; |
| 47 | + private InjectExpectationTrace savedInjectExpectationTrace2; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void beforeEach() { |
| 51 | + savedAsset = assetRepository.save(AssetFixture.createDefaultAsset("test")); |
| 52 | + |
| 53 | + SecurityPlatform sp = new SecurityPlatform(); |
| 54 | + sp.setExternalReference(UUID.randomUUID().toString()); |
| 55 | + sp.setName("sp-name"); |
| 56 | + sp.setSecurityPlatformType(SECURITY_PLATFORM_TYPE.SIEM); |
| 57 | + savedSecurityPlatform = securityPlatformRepository.save(sp); |
| 58 | + |
| 59 | + Collector collector = new Collector(); |
| 60 | + collector.setId(UUID.randomUUID().toString()); |
| 61 | + collector.setName("collector-name"); |
| 62 | + collector.setSecurityPlatform(savedSecurityPlatform); |
| 63 | + collector.setType("type"); |
| 64 | + collector.setExternal(true); |
| 65 | + savedCollector = collectorRepository.save(collector); |
| 66 | + |
| 67 | + Inject i = InjectFixture.getDefaultInject(); |
| 68 | + i.setAssets(List.of(savedAsset)); |
| 69 | + savedInject = injectRepository.save(i); |
| 70 | + |
| 71 | + InjectExpectation ie = |
| 72 | + InjectExpectationFixture.createDetectionInjectExpectation(null, savedInject); |
| 73 | + ie.setAsset(savedAsset); |
| 74 | + savedInjectExpectation = injectExpectationRepository.save(ie); |
| 75 | + |
| 76 | + InjectExpectationTrace iet1 = new InjectExpectationTrace(); |
| 77 | + iet1.setInjectExpectation(savedInjectExpectation); |
| 78 | + iet1.setSecurityPlatform(savedSecurityPlatform); |
| 79 | + iet1.setAlertDate(Instant.now()); |
| 80 | + iet1.setAlertLink("http://test-link.com"); |
| 81 | + iet1.setAlertName("Test Alert 1"); |
| 82 | + savedInjectExpectationTrace1 = injectExpectationTraceRepository.save(iet1); |
| 83 | + |
| 84 | + InjectExpectationTrace iet2 = new InjectExpectationTrace(); |
| 85 | + iet2.setInjectExpectation(savedInjectExpectation); |
| 86 | + iet2.setSecurityPlatform(savedSecurityPlatform); |
| 87 | + iet2.setAlertDate(Instant.now()); |
| 88 | + iet2.setAlertLink("http://test-link.com"); |
| 89 | + iet2.setAlertName("Test Alert 2"); |
| 90 | + savedInjectExpectationTrace2 = injectExpectationTraceRepository.save(iet2); |
| 91 | + } |
| 92 | + |
| 93 | + @DisplayName("Create an inject expectation trace for a collector") |
| 94 | + @Test |
| 95 | + @WithMockAdminUser |
| 96 | + void createInjectExpectationTraceForCollector_Success() throws Exception { |
| 97 | + // --PREPARE-- |
| 98 | + InjectExpectationTraceInput input = new InjectExpectationTraceInput(); |
| 99 | + input.setInjectExpectationId(savedInjectExpectation.getId()); |
| 100 | + input.setAlertDate(Instant.now()); |
| 101 | + input.setAlertLink("http://fake-link.com"); |
| 102 | + input.setSourceId(savedCollector.getId()); |
| 103 | + input.setAlertName("Test Alert"); |
| 104 | + |
| 105 | + // --EXECUTE-- |
| 106 | + String response = |
| 107 | + mvc.perform( |
| 108 | + post(INJECT_EXPECTATION_TRACES_URI) |
| 109 | + .content(asJsonString(input)) |
| 110 | + .contentType(MediaType.APPLICATION_JSON) |
| 111 | + .accept(MediaType.APPLICATION_JSON)) |
| 112 | + .andExpect(status().is2xxSuccessful()) |
| 113 | + .andReturn() |
| 114 | + .getResponse() |
| 115 | + .getContentAsString(); |
| 116 | + |
| 117 | + // --ASSERT-- |
| 118 | + assertEquals( |
| 119 | + savedInjectExpectation.getId(), |
| 120 | + JsonPath.read(response, "$.inject_expectation_trace_expectation")); |
| 121 | + assertEquals( |
| 122 | + savedSecurityPlatform.getId(), |
| 123 | + JsonPath.read(response, "$.inject_expectation_trace_source_id")); |
| 124 | + } |
| 125 | + |
| 126 | + @DisplayName("Get the traces for a collector") |
| 127 | + @Test |
| 128 | + @WithMockAdminUser |
| 129 | + void getInjectExpectationTracesForCollector() throws Exception { |
| 130 | + // --EXECUTE-- |
| 131 | + String response = |
| 132 | + mvc.perform( |
| 133 | + get(INJECT_EXPECTATION_TRACES_URI |
| 134 | + + "?injectExpectationId=" |
| 135 | + + savedInjectExpectation.getId() |
| 136 | + + "&sourceId=" |
| 137 | + + savedCollector.getId()) |
| 138 | + .accept(MediaType.APPLICATION_JSON)) |
| 139 | + .andExpect(status().is2xxSuccessful()) |
| 140 | + .andReturn() |
| 141 | + .getResponse() |
| 142 | + .getContentAsString(); |
| 143 | + |
| 144 | + // --ASSERT-- |
| 145 | + assertEquals( |
| 146 | + savedInjectExpectationTrace1.getId(), |
| 147 | + JsonPath.read(response, "$[0].inject_expectation_trace_id")); |
| 148 | + assertEquals( |
| 149 | + savedInjectExpectationTrace2.getId(), |
| 150 | + JsonPath.read(response, "$[1].inject_expectation_trace_id")); |
| 151 | + assertEquals( |
| 152 | + savedSecurityPlatform.getId(), |
| 153 | + JsonPath.read(response, "$[0].inject_expectation_trace_source_id")); |
| 154 | + assertEquals( |
| 155 | + savedSecurityPlatform.getId(), |
| 156 | + JsonPath.read(response, "$[1].inject_expectation_trace_source_id")); |
| 157 | + } |
| 158 | + |
| 159 | + @DisplayName("Count expectation traces for a collector") |
| 160 | + @Test |
| 161 | + @WithMockAdminUser |
| 162 | + void countInjectExpectationTracesForCollector() throws Exception { |
| 163 | + // --EXECUTE-- |
| 164 | + String response = |
| 165 | + mvc.perform( |
| 166 | + get(INJECT_EXPECTATION_TRACES_URI |
| 167 | + + "/count?injectExpectationId=" |
| 168 | + + savedInjectExpectation.getId() |
| 169 | + + "&sourceId=" |
| 170 | + + savedSecurityPlatform.getExternalReference() |
| 171 | + + "&expectationResultSourceType=collector") |
| 172 | + .accept(MediaType.APPLICATION_JSON)) |
| 173 | + .andExpect(status().is2xxSuccessful()) |
| 174 | + .andReturn() |
| 175 | + .getResponse() |
| 176 | + .getContentAsString(); |
| 177 | + |
| 178 | + // --ASSERT-- |
| 179 | + assertEquals(2, Integer.parseInt(response)); |
| 180 | + } |
| 181 | + |
| 182 | + @DisplayName( |
| 183 | + "Count expectation traces for other source than a collector, with an ivalid sourceId given") |
| 184 | + @Test |
| 185 | + @WithMockAdminUser |
| 186 | + void countInjectExpectationTracesForOthers_0() throws Exception { |
| 187 | + // --EXECUTE-- |
| 188 | + String response = |
| 189 | + mvc.perform( |
| 190 | + get(INJECT_EXPECTATION_TRACES_URI |
| 191 | + + "/count?injectExpectationId=" |
| 192 | + + savedInjectExpectation.getId() |
| 193 | + + "&sourceId=" |
| 194 | + + savedSecurityPlatform.getExternalReference() |
| 195 | + + "&expectationResultSourceType=other") |
| 196 | + .accept(MediaType.APPLICATION_JSON)) |
| 197 | + .andExpect(status().is2xxSuccessful()) |
| 198 | + .andReturn() |
| 199 | + .getResponse() |
| 200 | + .getContentAsString(); |
| 201 | + |
| 202 | + // --ASSERT-- |
| 203 | + assertEquals(0, Integer.parseInt(response)); |
| 204 | + } |
| 205 | + |
| 206 | + @DisplayName( |
| 207 | + "Count expectation traces for other source than a collector, with a valid sourceId given") |
| 208 | + @Test |
| 209 | + @WithMockAdminUser |
| 210 | + void countInjectExpectationTracesForOthers() throws Exception { |
| 211 | + // --EXECUTE-- |
| 212 | + String response = |
| 213 | + mvc.perform( |
| 214 | + get(INJECT_EXPECTATION_TRACES_URI |
| 215 | + + "/count?injectExpectationId=" |
| 216 | + + savedInjectExpectation.getId() |
| 217 | + + "&sourceId=" |
| 218 | + + savedSecurityPlatform.getId() |
| 219 | + + "&expectationResultSourceType=other") |
| 220 | + .accept(MediaType.APPLICATION_JSON)) |
| 221 | + .andExpect(status().is2xxSuccessful()) |
| 222 | + .andReturn() |
| 223 | + .getResponse() |
| 224 | + .getContentAsString(); |
| 225 | + |
| 226 | + // --ASSERT-- |
| 227 | + assertEquals(2, Integer.parseInt(response)); |
| 228 | + } |
| 229 | +} |
0 commit comments