BasicLevelPreparer implements LevelPreparer { @Override public PreparedLevel prepare(Level level) { Type<?> type = resolveType(level.getType(), level.getName()); Matcher matcher = resolveMatcher(level.getMatcher(), level.getName()); Function levelCreator = resolveLevelCreator(level.getLevelCreator()); return new PreparedLevel(level.getName(), level.isArray(), type, level.getMatcher(), matcher, levelCreator); } BasicLevelPreparer(MatcherRepository matcherRepository, TypeRepository typeRepository, FunctionProvider functionProvider); @Override PreparedLevel prepare(Level level);  }
@Test @SuppressWarnings("unchecked") public void shouldPrepareLevelResolvingAllDependenciesFromRepositories() { Level level = level().withName("level").withLevelCreator("levelCreator") .withMatcher("matcher").withType("type").build(); Matcher matcher = mock(Matcher.class); when(matcherRepository.getMatcher("matcher")).thenReturn(matcher); Type type = mock(Type.class); when(typeRepository.getType("type")).thenReturn(type); Function levelCreator = mock(Function.class); when(functionProvider.getFunction("levelCreator")).thenReturn(levelCreator); PreparedLevel preparedLevel = basicLevelPreparer.prepare(level); assertThat(preparedLevel).hasName("level").hasLevelCreator(levelCreator) .hasMatcher(matcher).hasType(type); }
@Test public void shouldPrepareLevelWithEmptyDependenciesWhenNotDefined() { Level level = level().withName("level").build(); PreparedLevel preparedLevel = basicLevelPreparer.prepare(level); assertThat(preparedLevel).hasName("level").hasLevelCreator(null) .hasMatcher(null).hasType(null); }
@Test public void shouldThrowExceptionWhenMatcherDefinedButNotFoundInRepo() { Level level = level().withMatcher("matcher").build(); when(matcherRepository.getMatcher("matcher")).thenReturn(null); catchException(basicLevelPreparer).prepare(level); assertThat(caughtException()).isInstanceOf(UnknownMatcherException.class); }
@Test public void shouldThrowExceptionWhenTypeDefinedButNotFoundInRepo() { Level level = level().withType("type").build(); when(typeRepository.getType("type")).thenReturn(null); catchException(basicLevelPreparer).prepare(level); assertThat(caughtException()).isInstanceOf(UnknownTypeException.class); }