### Question:
GatewayConfiguration implements ApplicationContextInitializer<GenericApplicationContext> { @Override public void initialize(GenericApplicationContext applicationContext) { var environment = applicationContext.getEnvironment(); if (!environment.acceptsProfiles(Profiles.of("gateway"))) { return; } var layersProperties = PropertiesUtil.bind(environment, new LayersProperties()); var comparator = Comparator .comparingInt(it -> layersProperties.getOrders().getOrDefault(it.getClass().getName(), 0)) .thenComparing(it -> it.getClass().getName()); applicationContext.registerBean(RecordPreProcessorChain.class, () -> new RecordPreProcessorChain( applicationContext.getBeansOfType(RecordPreProcessor.class).values().stream() .sorted(comparator) .collect(Collectors.toList()) )); applicationContext.registerBean(RecordPostProcessorChain.class, () -> new RecordPostProcessorChain( applicationContext.getBeansOfType(RecordPostProcessor.class).values().stream() .sorted(comparator.reversed()) .collect(Collectors.toList()) )); applicationContext.registerBean(LiiklusService.class); }  @Override void initialize(GenericApplicationContext applicationContext);  }### Answer:
@Test public void testOrderWithEmptyOrders() throws Exception { gatewayConfiguration.initialize(applicationContext); assertThat(applicationContext.getBean(RecordPreProcessorChain.class).getAll()).containsExactly( P1.INSTANCE, P2.INSTANCE, P3.INSTANCE ); assertThat(applicationContext.getBean(RecordPostProcessorChain.class).getAll()).containsExactly( P3.INSTANCE, P2.INSTANCE, P1.INSTANCE ); }
@Test public void testOrderWithDefaultOrder() throws Exception { setOrder(P2.class, 0); gatewayConfiguration.initialize(applicationContext); assertThat(applicationContext.getBean(RecordPreProcessorChain.class).getAll()).containsExactly( P1.INSTANCE, P2.INSTANCE, P3.INSTANCE ); assertThat(applicationContext.getBean(RecordPostProcessorChain.class).getAll()).containsExactly( P3.INSTANCE, P2.INSTANCE, P1.INSTANCE ); }
@Test public void testNegativeOrders() throws Exception { setOrder(P2.class, -100); setOrder(P3.class, -50); gatewayConfiguration.initialize(applicationContext); assertThat(applicationContext.getBean(RecordPreProcessorChain.class).getAll()).containsExactly( P2.INSTANCE, P3.INSTANCE, P1.INSTANCE ); assertThat(applicationContext.getBean(RecordPostProcessorChain.class).getAll()).containsExactly( P1.INSTANCE, P3.INSTANCE, P2.INSTANCE ); }
@Test public void testPositiveOrders() throws Exception { setOrder(P2.class, 100); setOrder(P3.class, 50); gatewayConfiguration.initialize(applicationContext); assertThat(applicationContext.getBean(RecordPreProcessorChain.class).getAll()).containsExactly( P1.INSTANCE, P3.INSTANCE, P2.INSTANCE ); assertThat(applicationContext.getBean(RecordPostProcessorChain.class).getAll()).containsExactly( P2.INSTANCE, P3.INSTANCE, P1.INSTANCE ); }
@Test public void testOrderClashing() throws Exception { setOrder(P3.class, -100); setOrder(P2.class, -100); gatewayConfiguration.initialize(applicationContext); assertThat(applicationContext.getBean(RecordPreProcessorChain.class).getAll()).containsExactly( P2.INSTANCE, P3.INSTANCE, P1.INSTANCE ); assertThat(applicationContext.getBean(RecordPostProcessorChain.class).getAll()).containsExactly( P1.INSTANCE, P3.INSTANCE, P2.INSTANCE ); }