Skip to content

Commit

Permalink
extract assertIncrementedRepositoryCount
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 6, 2024
1 parent 93b1e8a commit d74878d
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ _%>

@Test<%= transactionalAnnotation %>
void create<%= entityClass %>() throws Exception {
long databaseSizeBeforeCreate = <%= entityInstance %>Repository.count()<%= callBlock %>;
long databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand Down Expand Up @@ -700,8 +700,7 @@ _%>
<%_ if (databaseTypeCouchbase) { _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
long databaseSizeAfterCreate = <%= entityInstance %>Repository.count()<%= callBlock %>;
assertThat(databaseSizeBeforeCreate + 1).isEqualTo(databaseSizeAfterCreate);
assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
<%_ if (searchEngineElasticsearch) { _%>
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
int searchDatabaseSizeAfter = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
Expand Down Expand Up @@ -740,7 +739,7 @@ _%>
<%= dtoClass %> <%= dtoInstance %> = <%= entityInstance %>Mapper.toDto(<%= persistInstance %>);
<%_ } _%>

int databaseSizeBeforeCreate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();
long databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand All @@ -764,7 +763,7 @@ _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeCreate);
assertSameRepositoryCount(databaseSizeBeforeCreate);
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeAfter = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
assertThat(searchDatabaseSizeAfter).isEqualTo(searchDatabaseSizeBefore);
Expand All @@ -777,7 +776,7 @@ _%>
// Initialize the database
<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>;
<%_ const alreadyGeneratedEntities = []; _%>
int databaseSizeBeforeCreate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();
int databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand Down Expand Up @@ -832,7 +831,7 @@ _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeCreate);
assertSameRepositoryCount(databaseSizeBeforeCreate);
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);

// Validate the id for MapsId, the ids must be same
Expand Down Expand Up @@ -1881,4 +1880,16 @@ _%>
if (!field.fieldTypeString) { %>.toString()<% } %>))<%= !reactive ? ')' : '' %><%_ } _%>;
}
<%_ } _%>

protected int getRepositoryCount() {
return <%= entityInstance %>Repository.count()<%= callBlock %>;
}

protected void assertIncrementedRepositoryCount(long countBefore) {
assertThat(countBefore + 1).isEqualTo(getRepositoryCount());
}

protected void assertSameRepositoryCount(long countBefore) {
assertThat(countBefore).isEqualTo(getRepositoryCount());
}
}

0 comments on commit d74878d

Please sign in to comment.