BasicParamPreparer implements ParamPreparer { @Override public PreparedParameter prepare(ParameterFromRepository parameterFromRepository) { Parameter parameter = parameterFromRepository.parameter(); int levelCount = getLevelCount(parameter); PreparedLevel[] levels = new PreparedLevel[levelCount]; Type<?>[] types = new Type<?>[levelCount]; Matcher[] matchers = new Matcher[levelCount]; for (int currentLevelIndex = 0; currentLevelIndex < levelCount; currentLevelIndex++) { Level level = getLevel(parameter, currentLevelIndex); PreparedLevel preparedLevel = levelPreparer.prepare(level); levels[currentLevelIndex] = preparedLevel; types[currentLevelIndex] = preparedLevel.getType(); matchers[currentLevelIndex] = preparedLevel.getMatcher(); } PreparedParameter preparedParameter = new PreparedParameter(parameterFromRepository.repositoryName(), parameter, levels); preparedParameter.setLevelNameMap(buildLevelNameToIndexMap(preparedParameter)); if (parameter.isCacheable()) { preparedParameter.setIndex(buildIndex(parameter, types, matchers)); } return preparedParameter; } BasicParamPreparer(LevelPreparer levelPreparer); @Override PreparedParameter prepare(ParameterFromRepository parameterFromRepository); @Override PreparedEntry prepareIdentifiableEntry(ParameterEntry parameterEntry);  }
@Test public void shouldReturnPreparedParameterWithIndexForCacheableParameter() { Level[] levels = new Level[]{ level().withName("level").withMatcher("matcher").withLevelCreator("creator").withType("type").build(), level().withName("outputLevel").withType("type").build() }; Parameter parameter = parameter().withName("param").withInputLevels(1).withArraySeparator('^') .withEntries().withLevels(levels).build(); when(levelPreparer.prepare(any(Level.class))).thenReturn(preparedLevel().build()).thenReturn(preparedLevel().withName("outputLevel").build()); PreparedParameter preparedParameter = paramPreparer.prepare(new ParameterFromRepository(parameter, RepositoryName.from("test"))); assertThat(preparedParameter).hasName("param").hasInputLevels(1).hasArraySeparator('^').hasIndex() .hasLevelNameEntry("outputLevel", 0); }
@Test public void shouldInsertLightParameterEntriesIntoIndexWhenParameterHasNoIdentifyEntriesFlagSet() { Level[] levels = new Level[]{ level().withName("outputLevel").withType("type").build() }; ParameterEntry[] entries = new ParameterEntry[]{ parameterEntry().withLevels("hello").build() }; Parameter parameter = parameter().withName("param").withInputLevels(0) .withLevels(levels).withEntries(entries).build(); when(levelPreparer.prepare(any(Level.class))).thenReturn(preparedLevel().build()).thenReturn(preparedLevel().withName("outputLevel").build()); PreparedParameter preparedParameter = paramPreparer.prepare(new ParameterFromRepository(parameter, RepositoryName.from("test"))); FastLevelIndexWalker<PreparedEntry> walker = new FastLevelIndexWalker<PreparedEntry>(preparedParameter.getIndex()); assertThat(walker.find().get(0)).isExactlyInstanceOf(PreparedEntry.class); }
@Test public void shouldInsertIdentifiableParameterEntriesIntoIndexWhenParameterHasIdentifyEntriesFlagSet() { Level[] levels = new Level[]{ level().withName("outputLevel").withType("type").build() }; ParameterEntry[] entries = new ParameterEntry[]{ parameterEntry().withLevels("hello").build() }; Parameter parameter = parameter().withName("param").identifyEntries().withInputLevels(0) .withLevels(levels).withEntries(entries).build(); when(levelPreparer.prepare(any(Level.class))).thenReturn(preparedLevel().build()).thenReturn(preparedLevel().withName("outputLevel").build()); PreparedParameter preparedParameter = paramPreparer.prepare(new ParameterFromRepository(parameter, RepositoryName.from("test"))); FastLevelIndexWalker<PreparedEntry> walker = new FastLevelIndexWalker<PreparedEntry>(preparedParameter.getIndex()); assertThat(walker.find().get(0)).isInstanceOf(IdentifiablePreparedEntry.class); }
@Test public void shouldNotBuildIndexForNoncacheableParameter() { Parameter parameter = parameter().withName("param").withInputLevels(1).noncacheable().withEntries().withLevels().build(); PreparedParameter preparedParameter = paramPreparer.prepare(new ParameterFromRepository(parameter, RepositoryName.from("test"))); assertThat(preparedParameter).hasName("param").hasNoIndex(); }