Skip to content

Commit

Permalink
Add test to ensure full mapping of types in AbstractStorageBasedProvi…
Browse files Browse the repository at this point in the history
…der (#4318)

Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored Jul 22, 2024
1 parent 0a5886c commit 8557666
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openhab.core.internal.types.StateDescriptionFragmentImpl;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.thing.type.ChannelDefinition;
Expand Down Expand Up @@ -158,6 +163,37 @@ public void testThingTypeProperlyMappedToEntityAndBack() {
}
}

@MethodSource
@ParameterizedTest
public void testMapToEntityIsComplete(Class<?> originalType, Class<?> mappedType, int allowedDelta) {
Class<?> clazz = originalType;
int originalTypeFieldCount = 0;
while (clazz != Object.class) {
originalTypeFieldCount += clazz.getDeclaredFields().length;
clazz = clazz.getSuperclass();
}

int mappedEntityFieldCount = mappedType.getDeclaredFields().length;

assertThat(originalType.getName() + " not properly mapped", mappedEntityFieldCount,
is(originalTypeFieldCount + allowedDelta));
}

private static Stream<Arguments> testMapToEntityIsComplete() {
return Stream.of( //
// isBridge is an extra field for storage and not present in ThingType
Arguments.of(ThingType.class, AbstractStorageBasedTypeProvider.ThingTypeEntity.class, 1),
Arguments.of(ChannelType.class, AbstractStorageBasedTypeProvider.ChannelTypeEntity.class, 0),
Arguments.of(ChannelDefinition.class, AbstractStorageBasedTypeProvider.ChannelDefinitionEntity.class,
0),
// configDescriptionURI is not available for ChannelGroupType
Arguments.of(ChannelGroupType.class, AbstractStorageBasedTypeProvider.ChannelGroupTypeEntity.class, -1),
Arguments.of(ChannelGroupDefinition.class,
AbstractStorageBasedTypeProvider.ChannelGroupDefinitionEntity.class, 0),
Arguments.of(StateDescriptionFragmentImpl.class,
AbstractStorageBasedTypeProvider.StateDescriptionFragmentEntity.class, 0));
}

private void assertChannelDefinition(ChannelDefinition actual, ChannelDefinition expected) {
assertThat(actual.getId(), is(expected.getId()));
assertThat(actual.getChannelTypeUID(), is(expected.getChannelTypeUID()));
Expand Down

0 comments on commit 8557666

Please sign in to comment.