Skip to content

Commit 7635f46

Browse files
committed
test fixes, updates
1 parent f11da3a commit 7635f46

6 files changed

Lines changed: 192 additions & 25 deletions

File tree

src/main/java/org/folio/fqm/repository/CustomEntityTypeMigrationMappingRepository.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public Map<UUID, Map<String, UUID>> getMappings() {
4949
try {
5050
return objectMapper.readValue(
5151
readerJooqContext
52-
.select(field("mapping"))
52+
.select(field("mapping", String.class))
5353
.from(table(MAPPING_TABLE))
54-
.fetchOne()
55-
.get(field("mapping"), String.class),
54+
.fetchOne(field("mapping", String.class)),
5655
new TypeReference<>() {}
5756
);
5857
} catch (JsonProcessingException | IllegalArgumentException | DataAccessException e) {

src/main/java/org/folio/fqm/service/EntityTypeService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.folio.fqm.client.CrossTenantHttpClient;
1919
import org.folio.fqm.client.LanguageClient;
2020
import org.folio.fqm.client.SimpleHttpClient;
21-
import org.folio.fqm.config.MigrationConfiguration;
2221
import org.folio.fqm.domain.dto.EntityTypeSummary;
2322
import org.folio.fqm.exception.EntityTypeInUseException;
2423
import org.folio.fqm.exception.EntityTypeNotFoundException;
@@ -92,7 +91,6 @@ public class EntityTypeService {
9291
private final EntityTypeFlatteningService entityTypeFlatteningService;
9392
private final EntityTypeValidationService entityTypeValidationService;
9493
private final LocalizationService localizationService;
95-
private final MigrationConfiguration migrationConfiguration;
9694
private final MigrationService migrationService;
9795
private final QueryProcessorService queryService;
9896
private final CrossTenantHttpClient crossTenantHttpClient;
@@ -491,7 +489,7 @@ public CustomEntityType createCustomEntityType(CustomEntityType customEntityType
491489

492490
CustomEntityType updatedCustomEntityType = customEntityType.toBuilder()
493491
.id(customEntityTypeId.toString())
494-
.version(migrationConfiguration.getCurrentVersion())
492+
.version(migrationService.getLatestVersion())
495493
.createdAt(now)
496494
.updatedAt(now)
497495
.owner(folioExecutionContext.getUserId())
@@ -508,7 +506,7 @@ public CustomEntityType updateCustomEntityType(UUID entityTypeId, CustomEntityTy
508506
permissionsService.verifyUserCanAccessCustomEntityType(oldET);
509507

510508
CustomEntityType updatedCustomEntityType = customEntityType.toBuilder()
511-
.version(migrationConfiguration.getCurrentVersion())
509+
.version(migrationService.getLatestVersion())
512510
.createdAt(oldET.getCreatedAt())
513511
.updatedAt(clockService.now())
514512
.owner(Objects.requireNonNullElse(customEntityType.getOwner(), oldET.getOwner()))

src/test/java/org/folio/fqm/migration/warnings/WarningTest.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class WarningTest {
3232
ArgumentCaptor<Object[]> varargCaptor;
3333

3434
public static List<Arguments> getExpectedTypes() {
35-
return List.of(
35+
return Arrays.asList(
3636
Arguments.of(DeprecatedEntityWarning.withAlternative("old", "new").apply(null), WarningType.DEPRECATED_ENTITY),
3737
Arguments.of(DeprecatedFieldWarning.build().apply("prefix.", "old", "{}"), WarningType.DEPRECATED_FIELD),
3838
Arguments.of(OperatorBreakingWarning.builder().build(), WarningType.OPERATOR_BREAKING),
@@ -49,66 +49,76 @@ void testTypes(Warning warning, WarningType expectedType) {
4949
}
5050

5151
public static List<Arguments> getExpectedTranslations() {
52-
return List.of(
52+
return Arrays.asList(
5353
Arguments.of(
5454
DeprecatedEntityWarning.withAlternative("old", "alt").apply(null),
5555
"mod-fqm-manager.migration.warning.DEPRECATED_ENTITY.withAlternative",
56-
List.of("name", "old", "alternative", "alt")
56+
Arrays.asList("name", "old", "alternative", "alt")
5757
),
5858
Arguments.of(
5959
DeprecatedEntityWarning.withoutAlternative("old").apply(null),
6060
"mod-fqm-manager.migration.warning.DEPRECATED_ENTITY.withoutAlternative",
61-
List.of("name", "old")
61+
Arrays.asList("name", "old")
6262
),
6363
Arguments.of(
6464
DeprecatedFieldWarning.build().apply("prefix.", "old", null),
6565
"mod-fqm-manager.migration.warning.DEPRECATED_FIELD.field",
66-
List.of("name", "prefix.old")
66+
Arrays.asList("name", "prefix.old")
6767
),
6868
Arguments.of(
6969
DeprecatedFieldWarning.build().apply("prefix.", "old", "{}"),
7070
"mod-fqm-manager.migration.warning.DEPRECATED_FIELD.query",
71-
List.of("name", "prefix.old")
71+
Arrays.asList("name", "prefix.old")
7272
),
7373
Arguments.of(
7474
OperatorBreakingWarning.builder().field("old").operator("$ne").fql("{}").build(),
7575
"mod-fqm-manager.migration.warning.OPERATOR_BREAKING",
76-
List.of("name", "old", "operator", "$ne", "fql", "{}")
76+
Arrays.asList("name", "old", "operator", "$ne", "fql", "{}")
7777
),
7878
Arguments.of(
7979
QueryBreakingWarning.withAlternative("alt").apply("prefix.", "old", "{}"),
8080
"mod-fqm-manager.migration.warning.QUERY_BREAKING.withAlternative",
81-
List.of("name", "prefix.old", "alternative", "prefix.alt", "fql", "{}")
81+
Arrays.asList("name", "prefix.old", "alternative", "prefix.alt", "fql", "{}")
8282
),
8383
Arguments.of(
8484
QueryBreakingWarning.withoutAlternative().apply("prefix.", "old", "{}"),
8585
"mod-fqm-manager.migration.warning.QUERY_BREAKING.withoutAlternative",
86-
List.of("name", "prefix.old", "fql", "{}")
86+
Arrays.asList("name", "prefix.old", "fql", "{}")
8787
),
8888
Arguments.of(
8989
RemovedEntityWarning.withAlternative("old", "alt").apply("{}"),
9090
"mod-fqm-manager.migration.warning.REMOVED_ENTITY.withAlternative",
91-
List.of("name", "old", "alternative", "alt", "fql", "{}")
91+
Arrays.asList("name", "old", "alternative", "alt", "fql", "{}")
9292
),
9393
Arguments.of(
9494
RemovedEntityWarning.withoutAlternative("old").apply("{}"),
9595
"mod-fqm-manager.migration.warning.REMOVED_ENTITY.withoutAlternative",
96-
List.of("name", "old", "fql", "{}")
96+
Arrays.asList("name", "old", "fql", "{}")
9797
),
9898
Arguments.of(
9999
RemovedFieldWarning.withAlternative("alt").apply("prefix.", "old", "{}"),
100-
"mod-fqm-manager.migration.warning.REMOVED_FIELD.withAlternative",
101-
List.of("name", "prefix.old", "alternative", "prefix.alt", "fql", "{}")
100+
"mod-fqm-manager.migration.warning.REMOVED_FIELD.query.withAlternative",
101+
Arrays.asList("name", "prefix.old", "alternative", "prefix.alt", "fql", "{}")
102102
),
103103
Arguments.of(
104104
RemovedFieldWarning.withoutAlternative().apply("prefix.", "old", "{}"),
105-
"mod-fqm-manager.migration.warning.REMOVED_FIELD.withoutAlternative",
106-
List.of("name", "prefix.old", "fql", "{}")
105+
"mod-fqm-manager.migration.warning.REMOVED_FIELD.query.withoutAlternative",
106+
Arrays.asList("name", "prefix.old", "alternative", null, "fql", "{}")
107+
),
108+
Arguments.of(
109+
RemovedFieldWarning.withAlternative("alt").apply("prefix.", "old", null),
110+
"mod-fqm-manager.migration.warning.REMOVED_FIELD.field.withAlternative",
111+
Arrays.asList("name", "prefix.old", "alternative", "prefix.alt", "fql", null)
112+
),
113+
Arguments.of(
114+
RemovedFieldWarning.withoutAlternative().apply("prefix.", "old", null),
115+
"mod-fqm-manager.migration.warning.REMOVED_FIELD.field.withoutAlternative",
116+
Arrays.asList("name", "prefix.old", "alternative", null, "fql", null)
107117
),
108118
Arguments.of(
109119
ValueBreakingWarning.builder().field("old").value("val").fql("{}").build(),
110120
"mod-fqm-manager.migration.warning.VALUE_BREAKING",
111-
List.of("name", "old", "value", "val", "fql", "{}")
121+
Arrays.asList("name", "old", "value", "val", "fql", "{}")
112122
)
113123
);
114124
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package org.folio.fqm.repository;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.junit.Assert.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
7+
import static org.mockito.ArgumentMatchers.any;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.never;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.when;
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import java.util.Map;
15+
import java.util.UUID;
16+
import org.apache.commons.lang3.exception.UncheckedException;
17+
import org.jooq.DSLContext;
18+
import org.jooq.Field;
19+
import org.jooq.InsertSetMoreStep;
20+
import org.jooq.InsertSetStep;
21+
import org.jooq.Record1;
22+
import org.jooq.SelectJoinStep;
23+
import org.jooq.SelectSelectStep;
24+
import org.jooq.Table;
25+
import org.jooq.UpdateSetFirstStep;
26+
import org.jooq.UpdateSetMoreStep;
27+
import org.jooq.exception.DataAccessException;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.ExtendWith;
31+
import org.mockito.Mock;
32+
import org.mockito.junit.jupiter.MockitoExtension;
33+
34+
@SuppressWarnings("unchecked")
35+
@ExtendWith(MockitoExtension.class)
36+
class CustomEntityTypeMigrationMappingRepositoryTest {
37+
38+
@Mock
39+
DSLContext jooqContext;
40+
41+
CustomEntityTypeMigrationMappingRepository customEntityTypeMigrationMappingRepository;
42+
43+
@BeforeEach
44+
void setup() {
45+
customEntityTypeMigrationMappingRepository =
46+
new CustomEntityTypeMigrationMappingRepository(jooqContext, jooqContext, new ObjectMapper());
47+
}
48+
49+
@Test
50+
void testGetMappingsWhenAlreadyPresent() {
51+
mockFetchCount(1);
52+
mockFetch("{\"b50630f8-eb40-5f67-8b6f-b7612314e67a\": {\"something\": \"d290f1ee-6c54-4b01-90e6-d701748f0851\"}}");
53+
54+
assertThat(
55+
customEntityTypeMigrationMappingRepository.getMappings(),
56+
is(
57+
Map.of(
58+
UUID.fromString("b50630f8-eb40-5f67-8b6f-b7612314e67a"),
59+
Map.of("something", UUID.fromString("d290f1ee-6c54-4b01-90e6-d701748f0851"))
60+
)
61+
)
62+
);
63+
64+
verify(jooqContext, never()).insertInto(any(Table.class));
65+
}
66+
67+
@Test
68+
void testGetMappingsInvalidFallback() {
69+
mockFetchCount(1);
70+
mockFetch("invalid");
71+
72+
assertThat(customEntityTypeMigrationMappingRepository.getMappings(), is(Map.of()));
73+
74+
verify(jooqContext, never()).insertInto(any(Table.class));
75+
}
76+
77+
@Test
78+
void testGetMappingsInitializeEmpty() {
79+
mockFetchCount(0);
80+
mockInsert();
81+
mockFetch("{\"6dbb66a6-078c-5983-aa4d-a450a126c025\":{}}");
82+
83+
assertThat(
84+
customEntityTypeMigrationMappingRepository.getMappings(),
85+
is(Map.of(UUID.fromString("6dbb66a6-078c-5983-aa4d-a450a126c025"), Map.of()))
86+
);
87+
}
88+
89+
@Test
90+
void testUpdate() {
91+
mockFetchCount(1);
92+
mockUpdate(null);
93+
94+
assertDoesNotThrow(() ->
95+
customEntityTypeMigrationMappingRepository.saveMappings(
96+
Map.of(
97+
UUID.fromString("b50630f8-eb40-5f67-8b6f-b7612314e67a"),
98+
Map.of("something", UUID.fromString("d290f1ee-6c54-4b01-90e6-d701748f0851"))
99+
)
100+
)
101+
);
102+
}
103+
104+
@Test
105+
void testUpdateExceptional() {
106+
mockFetchCount(1);
107+
mockUpdate(new DataAccessException("nope"));
108+
109+
assertThrows(
110+
UncheckedException.class,
111+
() ->
112+
customEntityTypeMigrationMappingRepository.saveMappings(
113+
Map.of(
114+
UUID.fromString("b50630f8-eb40-5f67-8b6f-b7612314e67a"),
115+
Map.of("something", UUID.fromString("d290f1ee-6c54-4b01-90e6-d701748f0851"))
116+
)
117+
)
118+
);
119+
}
120+
121+
private void mockFetchCount(int count) {
122+
when(jooqContext.fetchCount(any(Table.class))).thenReturn(count);
123+
}
124+
125+
private void mockFetch(String result) {
126+
SelectSelectStep<Record1<String>> selection = mock(SelectSelectStep.class);
127+
when(jooqContext.select(any(Field.class))).thenReturn(selection);
128+
SelectJoinStep<Record1<String>> selection2 = mock(SelectJoinStep.class);
129+
when(selection.from(any(Table.class))).thenReturn(selection2);
130+
when(selection2.fetchOne(any(Field.class))).thenReturn(result);
131+
}
132+
133+
private void mockInsert() {
134+
InsertSetStep<?> insertStep = mock(InsertSetStep.class);
135+
when(jooqContext.insertInto(any(Table.class))).thenReturn(insertStep);
136+
InsertSetMoreStep<?> insertStep2 = mock(InsertSetMoreStep.class);
137+
when(insertStep.set(any(Field.class), any())).thenReturn(insertStep2);
138+
when(insertStep2.execute()).thenReturn(1);
139+
}
140+
141+
private void mockUpdate(Exception toThrow) {
142+
UpdateSetFirstStep<?> updateStep = mock(UpdateSetFirstStep.class);
143+
when(jooqContext.update(any(Table.class))).thenReturn(updateStep);
144+
UpdateSetMoreStep<?> updateStep2 = mock(UpdateSetMoreStep.class);
145+
when(updateStep.set(any(Field.class), any())).thenReturn(updateStep2);
146+
if (toThrow == null) {
147+
when(updateStep2.execute()).thenReturn(1);
148+
} else {
149+
when(updateStep2.execute()).thenThrow(toThrow);
150+
}
151+
}
152+
}

src/test/java/org/folio/fqm/service/EntityTypeServiceAvailableJoinsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void getAccessibleEntityTypesById_shouldReturnOnlyAccessibleAndFlattenedEntityTy
538538
when(entityTypeFlatteningService.getFlattenedEntityType(eq(etId1), any(), eq(true))).thenReturn(et1);
539539
when(entityTypeFlatteningService.getFlattenedEntityType(eq(etId2), any(), eq(true))).thenReturn(et2);
540540

541-
Map<UUID, EntityType> result = new EntityTypeService(repo, entityTypeFlatteningService, null, null, null, null, null, null, permissionsService, null, null, executionContext, null, null)
541+
Map<UUID, EntityType> result = new EntityTypeService(repo, entityTypeFlatteningService, null, null, null, null, null, permissionsService, null, null, executionContext, null, null)
542542
.getAccessibleEntityTypesById();
543543

544544
assertEquals(1, result.size());
@@ -554,7 +554,7 @@ void getAccessibleEntityTypesById_shouldExcludeCustomEntityTypesIfNotAccessible(
554554
when(repo.getEntityTypeDefinitions(Set.of(), null)).thenReturn(Stream.of(et));
555555
when(permissionsService.getUserPermissions()).thenReturn(Set.of());
556556

557-
EntityTypeService svc = spy(new EntityTypeService(repo, entityTypeFlatteningService, null, null, null, null, null, null, permissionsService, null, null, executionContext, null, null));
557+
EntityTypeService svc = spy(new EntityTypeService(repo, entityTypeFlatteningService, null, null, null, null, null, permissionsService, null, null, executionContext, null, null));
558558
doReturn(false).when(svc).currentUserCanAccessCustomEntityType(etId.toString());
559559

560560
Map<UUID, EntityType> result = svc.getAccessibleEntityTypesById();

src/test/java/org/folio/fqm/service/EntityTypeValidationServiceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Optional;
1818
import java.util.UUID;
1919
import java.util.function.UnaryOperator;
20+
import org.folio.fqm.config.MigrationConfiguration;
2021
import org.folio.fqm.exception.FqmException;
2122
import org.folio.fqm.exception.InvalidEntityTypeDefinitionException;
2223
import org.folio.fqm.repository.EntityTypeRepository;
@@ -77,6 +78,7 @@ class EntityTypeValidationServiceTest {
7778
.owner(UUID.fromString("81dc8e91-c93b-5e86-b8de-dd5b7b4c75d2"))
7879
.name("test")
7980
._private(false)
81+
.version("current")
8082
.isCustom(true)
8183
.sources(
8284
List.of(new EntityTypeSourceEntityType().alias("source1").type("entity-type").targetId(EXISTING_TARGET_ET_ID))
@@ -89,6 +91,9 @@ class EntityTypeValidationServiceTest {
8991
@Mock
9092
private FolioExecutionContext executionContext;
9193

94+
@Mock
95+
private MigrationConfiguration migrationConfiguration;
96+
9297
@Spy
9398
@InjectMocks
9499
private EntityTypeValidationService entityTypeValidationService;
@@ -99,6 +104,7 @@ void baseMocks() {
99104
lenient()
100105
.when(repo.getEntityTypeDefinition(EXISTING_TARGET_ET_ID, TENANT_ID))
101106
.thenReturn(Optional.of(new EntityType()));
107+
lenient().when(migrationConfiguration.getCurrentVersion()).thenReturn("current");
102108
}
103109

104110
// shorthand to build a variant of the base CustomEntityType
@@ -190,6 +196,8 @@ void testEntityWithValidCustomFieldMetadata() {
190196
static List<Arguments> customEntityTypeInvalidCases() {
191197
// input, expected exception message (supports regex)
192198
return List.of(
199+
Arguments.of(customETFactory(b -> b.version(null)), "Custom entity type must have _version=current"),
200+
Arguments.of(customETFactory(b -> b.version("old")), "Custom entity type must have _version=current"),
193201
Arguments.of(
194202
customETFactory(b -> b.sourceView("something")),
195203
"Custom entity types must not contain a sourceView property"

0 commit comments

Comments
 (0)