Skip to content

Commit bdbfbd9

Browse files
committed
Add tests for hooks api layers
Signed-off-by: sdimitrov9 <stoyan.dimitrov@limechain.tech>
1 parent 355bbb8 commit bdbfbd9

3 files changed

Lines changed: 241 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package org.hiero.mirror.restjava.mapper;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import org.apache.commons.codec.DecoderException;
8+
import org.hiero.mirror.common.domain.entity.EntityId;
9+
import org.hiero.mirror.common.domain.hook.HookExtensionPoint;
10+
import org.hiero.mirror.common.domain.hook.HookType;
11+
import org.hiero.mirror.rest.model.Hook;
12+
import org.hiero.mirror.rest.model.Key;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
final class HookMapperTest {
17+
18+
private CommonMapper commonMapper;
19+
private HookMapper mapper;
20+
21+
@BeforeEach
22+
void setup() {
23+
commonMapper = new CommonMapperImpl();
24+
mapper = new HookMapperImpl(commonMapper);
25+
}
26+
27+
@Test
28+
void map() throws DecoderException {
29+
// given
30+
final var source = new org.hiero.mirror.common.domain.hook.Hook();
31+
source.setContractId(EntityId.of(100L));
32+
source.setCreatedTimestamp(1234567890000000000L);
33+
source.setHookId(10L);
34+
source.setOwnerId(200L);
35+
source.setExtensionPoint(HookExtensionPoint.ACCOUNT_ALLOWANCE_HOOK);
36+
source.setType(HookType.LAMBDA);
37+
source.setDeleted(false);
38+
39+
final var ed25519Hex = "1220" + "a".repeat(64);
40+
source.setAdminKey(org.apache.commons.codec.binary.Hex.decodeHex(ed25519Hex));
41+
42+
// when
43+
final var result = mapper.map(source);
44+
45+
// then
46+
assertThat(result).isNotNull();
47+
assertThat(result.getContractId()).isEqualTo("0.0.100");
48+
assertThat(result.getOwnerId()).isEqualTo("0.0.200");
49+
assertThat(result.getCreatedTimestamp()).isEqualTo("1234567890.000000000");
50+
assertThat(result.getHookId()).isEqualTo(10L);
51+
assertThat(result.getDeleted()).isFalse();
52+
assertThat(result.getExtensionPoint()).isEqualTo(Hook.ExtensionPointEnum.ACCOUNT_ALLOWANCE_HOOK);
53+
assertThat(result.getType()).isEqualTo(Hook.TypeEnum.LAMBDA);
54+
assertThat(result.getAdminKey().getType()).isEqualTo(Key.TypeEnum.ED25519);
55+
assertThat(result.getAdminKey().getKey()).hasSize(64);
56+
assertThat(result.getAdminKey().getKey())
57+
.isEqualTo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
58+
}
59+
60+
@Test
61+
void mapNulls() {
62+
// given
63+
final var source = new org.hiero.mirror.common.domain.hook.Hook();
64+
// when
65+
final var result = mapper.map(source);
66+
67+
// then
68+
assertThat(result).isNotNull();
69+
assertThat(result.getAdminKey()).as("adminKey should be null").isNull();
70+
assertThat(result.getContractId()).as("contractId should be null").isNull();
71+
assertThat(result.getCreatedTimestamp())
72+
.as("createdTimestamp should be null")
73+
.isNull();
74+
assertThat(result.getDeleted()).as("deleted should be null").isNull();
75+
assertThat(result.getExtensionPoint())
76+
.as("extensionPoint should be null")
77+
.isNull();
78+
assertThat(result.getHookId()).as("hookId default for primitive long").isEqualTo(0L);
79+
assertThat(result.getOwnerId()).as("ownerId should be null").isNull();
80+
assertThat(result.getType()).as("type should be null").isNull();
81+
}
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package org.hiero.mirror.restjava.repository;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import lombok.RequiredArgsConstructor;
10+
import org.hiero.mirror.common.domain.entity.EntityId;
11+
import org.hiero.mirror.common.domain.hook.Hook;
12+
import org.hiero.mirror.restjava.RestJavaIntegrationTest;
13+
import org.hiero.mirror.restjava.common.EntityIdParameter;
14+
import org.hiero.mirror.restjava.dto.HooksRequest;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.params.ParameterizedTest;
18+
import org.junit.jupiter.params.provider.EnumSource;
19+
import org.springframework.data.domain.Sort.Direction;
20+
21+
@RequiredArgsConstructor
22+
final class HookRepositoryTest extends RestJavaIntegrationTest {
23+
24+
private final HookRepository hookRepository;
25+
26+
private long ownerId;
27+
private List<Hook> hooks;
28+
29+
@BeforeEach
30+
void setup() {
31+
setupHooks();
32+
}
33+
34+
@ParameterizedTest
35+
@EnumSource(Direction.class)
36+
void findAllSorted(Direction direction) {
37+
// given
38+
final var request = HooksRequest.builder()
39+
.accountId(EntityIdParameter.valueOf("0.0" + ownerId))
40+
.limit(5)
41+
.order(direction)
42+
.build();
43+
44+
// when
45+
final var result = hookRepository.findAll(request, EntityId.of(ownerId));
46+
47+
// then
48+
assertThat(result).isNotEmpty().hasSizeLessThanOrEqualTo(5);
49+
50+
long previousId = direction == Direction.ASC ? -1 : Long.MAX_VALUE;
51+
for (Hook hook : result) {
52+
assertThat(hook.getOwnerId()).isEqualTo(ownerId);
53+
if (previousId != (direction == Direction.ASC ? -1 : Long.MAX_VALUE)) {
54+
if (direction == Direction.ASC) {
55+
assertThat(hook.getHookId()).isGreaterThan(previousId);
56+
} else {
57+
assertThat(hook.getHookId()).isLessThan(previousId);
58+
}
59+
}
60+
previousId = hook.getHookId();
61+
}
62+
}
63+
64+
@Test
65+
void findAllNoMatch() {
66+
// Request with a non-existent account
67+
final var request = HooksRequest.builder()
68+
.accountId(EntityIdParameter.valueOf("999999"))
69+
.limit(5)
70+
.order(Direction.ASC)
71+
.build();
72+
73+
final var result = hookRepository.findAll(request, EntityId.of(999999L));
74+
assertThat(result).isEmpty();
75+
}
76+
77+
private void setupHooks() {
78+
ownerId = domainBuilder.id();
79+
hooks = new ArrayList<>();
80+
81+
for (int i = 0; i < 5; i++) {
82+
final var hook = domainBuilder
83+
.hook()
84+
.customize(h -> h.ownerId(ownerId).hookId(domainBuilder.id()))
85+
.persist();
86+
hooks.add(hook);
87+
}
88+
}
89+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package org.hiero.mirror.restjava.service;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import lombok.RequiredArgsConstructor;
8+
import org.hiero.mirror.common.domain.entity.EntityId;
9+
import org.hiero.mirror.restjava.RestJavaIntegrationTest;
10+
import org.hiero.mirror.restjava.common.EntityIdNumParameter;
11+
import org.hiero.mirror.restjava.dto.HooksRequest;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.data.domain.Sort.Direction;
15+
16+
@RequiredArgsConstructor
17+
final class HookServiceTest extends RestJavaIntegrationTest {
18+
19+
private final HookService hookService;
20+
21+
private EntityId ownerId;
22+
23+
@BeforeEach
24+
void setup() {
25+
// Persist a test entity to the database
26+
final var entity = domainBuilder.entity().persist();
27+
ownerId = entity.toEntityId();
28+
29+
// Create a few hooks for that entity
30+
for (int i = 0; i < 3; i++) {
31+
domainBuilder
32+
.hook()
33+
.customize(h -> h.ownerId(ownerId.getId()).hookId(domainBuilder.id()))
34+
.persist();
35+
}
36+
}
37+
38+
@Test
39+
void getHooks() {
40+
// given
41+
final var request = HooksRequest.builder()
42+
.accountId(new EntityIdNumParameter(ownerId))
43+
.limit(5)
44+
.order(Direction.ASC)
45+
.build();
46+
47+
// when
48+
final var result = hookService.getHooks(request);
49+
50+
// then
51+
assertThat(result).isNotEmpty().allMatch(h -> h.getOwnerId() == ownerId.getId());
52+
}
53+
54+
@Test
55+
void getHooksEmptyResult() {
56+
// given – no hooks for this account
57+
final var nonexistent = EntityId.of(domainBuilder.id());
58+
final var request = HooksRequest.builder()
59+
.accountId(new EntityIdNumParameter(nonexistent))
60+
.limit(5)
61+
.order(Direction.ASC)
62+
.build();
63+
64+
// when
65+
final var result = hookService.getHooks(request);
66+
67+
// then
68+
assertThat(result).isEmpty();
69+
}
70+
}

0 commit comments

Comments
 (0)