Skip to content

Commit ecd35b4

Browse files
lampajrjohnaohara
authored andcommitted
Replace TestService.get calls when not required
Signed-off-by: Andrea Lamparelli <[email protected]>
1 parent 31c02c6 commit ecd35b4

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/TestServiceImpl.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.node.JsonNodeType;
88
import com.fasterxml.jackson.databind.node.ObjectNode;
99
import io.hyperfoil.tools.horreum.api.SortDirection;
10+
import io.hyperfoil.tools.horreum.api.alerting.Change;
1011
import io.hyperfoil.tools.horreum.api.data.Access;
1112
import io.hyperfoil.tools.horreum.api.data.ExportedLabelValues;
1213
import io.hyperfoil.tools.horreum.api.data.Fingerprints;
@@ -41,6 +42,7 @@
4142
import jakarta.enterprise.context.ApplicationScoped;
4243
import jakarta.inject.Inject;
4344
import jakarta.persistence.EntityManager;
45+
import jakarta.persistence.NoResultException;
4446
import jakarta.persistence.PersistenceException;
4547
import jakarta.persistence.Tuple;
4648
import jakarta.transaction.TransactionManager;
@@ -91,6 +93,7 @@ public class TestServiceImpl implements TestService {
9193
protected static final String LABEL_ORDER_STOP= "combined.stop";
9294
protected static final String LABEL_ORDER_JSONPATH = "jsonb_path_query(combined.values,CAST( :orderBy as jsonpath))";
9395

96+
private static final String COUNT_TEST_BY_ID_QUERY = "SELECT count(id) FROM test WHERE id = ?1";
9497
protected static final String LABEL_VALUES_QUERY = """
9598
WITH
9699
combined as (
@@ -183,6 +186,19 @@ public Test getByNameOrId(String input){
183186
return TestMapper.from(test);
184187
}
185188

189+
/**
190+
* Checks whether the provided id belongs to an existing test and if the user can access it
191+
* the security check is performed by triggering the RLS at database level
192+
* @param id test ID
193+
*/
194+
@WithRoles
195+
@Transactional
196+
protected boolean checkTestExists(int id) {
197+
return 0 != em.createQuery(COUNT_TEST_BY_ID_QUERY, Long.class)
198+
.setParameter(1, id)
199+
.getSingleResult();
200+
}
201+
186202
@WithRoles(extras = Roles.HORREUM_SYSTEM)
187203
public TestDAO ensureTestExists(String testNameOrId, String token){
188204
TestDAO test;// = TestMapper.to(getByNameOrId(input)); //why does getByNameOrId not work to create the DAO?
@@ -572,8 +588,7 @@ public void updateFolder(int id, String folder) {
572588
@SuppressWarnings("unchecked")
573589
@Override
574590
public List<Fingerprints> listFingerprints(int testId) {
575-
Test test = get(testId,null);
576-
if(test == null){
591+
if(!checkTestExists(testId)){
577592
throw ServiceException.serverError("Cannot find test "+testId);
578593
}
579594
return Fingerprints.parse( em.createNativeQuery("""
@@ -582,7 +597,7 @@ public List<Fingerprints> listFingerprints(int testId) {
582597
JOIN dataset ON dataset.id = dataset_id
583598
WHERE dataset.testid = ?1
584599
""")
585-
.setParameter(1, test.id)
600+
.setParameter(1, testId)
586601
.unwrap(NativeQuery.class).addScalar("fingerprint", JsonBinaryType.INSTANCE)
587602
.getResultList());
588603
}
@@ -721,8 +736,7 @@ protected static FilterDef getFilterDef(String filter, Instant before, Instant a
721736
@WithRoles
722737
@Override
723738
public List<ExportedLabelValues> labelValues(int testId, String filter, String before, String after, boolean filtering, boolean metrics, String sort, String direction, int limit, int page, List<String> include, List<String> exclude, boolean multiFilter) {
724-
Test test = get(testId,null);
725-
if(test == null){
739+
if(!checkTestExists(testId)){
726740
throw ServiceException.serverError("Cannot find test "+testId);
727741
}
728742
Object filterObject = Util.getFilterObject(filter);
@@ -785,7 +799,7 @@ public List<ExportedLabelValues> labelValues(int testId, String filter, String b
785799
.replace("ORDER_PLACEHOLDER",orderSql);
786800

787801
NativeQuery query = ((NativeQuery) em.createNativeQuery(sql))
788-
.setParameter("testId", test.id)
802+
.setParameter("testId", testId)
789803
.setParameter("filteringLabels", filtering)
790804
.setParameter("metricLabels", metrics)
791805
;
@@ -924,15 +938,14 @@ public void recalculateDatasets(int testId) {
924938
@Override
925939
@WithRoles
926940
public RecalculationStatus getRecalculationStatus(int testId) {
927-
Test test = get(testId,null);
928-
if(test == null){
941+
if(!checkTestExists(testId)){
929942
throw ServiceException.serverError("Cannot find test "+testId);
930943
}
931-
RecalculationStatus status = recalculations.get(test.id);
944+
RecalculationStatus status = recalculations.get(testId);
932945
if (status == null) {
933-
status = new RecalculationStatus(RunDAO.count("testid = ?1 AND trashed = false", test.id));
946+
status = new RecalculationStatus(RunDAO.count("testid = ?1 AND trashed = false", testId));
934947
status.finished = status.totalRuns;
935-
status.datasets = DatasetDAO.count("testid", test.id);
948+
status.datasets = DatasetDAO.count("testid", testId);
936949
}
937950
return status;
938951
}

horreum-backend/src/test/java/io/hyperfoil/tools/horreum/svc/TestServiceNoRestTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import static io.hyperfoil.tools.horreum.svc.BaseServiceNoRestTest.FOO_TEAM;
3333
import static io.hyperfoil.tools.horreum.svc.BaseServiceNoRestTest.FOO_TESTER;
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
3536
import static org.junit.jupiter.api.Assertions.assertNotNull;
3637
import static org.junit.jupiter.api.Assertions.assertNull;
3738
import static org.junit.jupiter.api.Assertions.assertThrows;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
3840

3941
@QuarkusTest
4042
@QuarkusTestResource(PostgresResource.class)
@@ -160,6 +162,21 @@ void testCreateTestWithExistingName() {
160162
assertEquals(Response.Status.CONFLICT.getStatusCode(), thrown.getResponse().getStatus());
161163
}
162164

165+
@org.junit.jupiter.api.Test
166+
void testCheckTestExists() {
167+
Test t1 = createSampleTest("test", null, null, null);
168+
Test t2 = createSampleTest("1234", null, null, null);
169+
170+
Test created1 = testService.add(t1);
171+
assertNotNull(created1.id);
172+
Test created2 = testService.add(t2);
173+
assertNotNull(created2.id);
174+
175+
assertTrue(((TestServiceImpl) testService).checkTestExists(created1.id));
176+
assertTrue(((TestServiceImpl) testService).checkTestExists(created2.id));
177+
assertFalse(((TestServiceImpl) testService).checkTestExists(9999));
178+
}
179+
163180
@org.junit.jupiter.api.Test
164181
void testUpdateTest() {
165182
Test t = createSampleTest("test", null, null, null);

0 commit comments

Comments
 (0)