SchemaIdMappingCacheInitialiser { public Map<MediaType, String> initialiseCache() { return schemaIdMappingObserver.getMediaTypeToSchemaIdMappers().stream() .flatMap(this::mapEntriesFrom) .collect(toMap( Map.Entry::getKey, Map.Entry::getValue, (schemaId_1, schemaId_2) -> schemaId_1 )); }  Map<MediaType, String> initialiseCache();  }
@Test @SuppressWarnings("unchecked") public void shouldInitialiseCacheWithSingleMappingBean() throws Exception { final MediaType mediaType = new MediaType("application/vnd.example.add-recipe+json"); final String schemaId = "http: final Map<MediaType, String> mediaTypeToSchemaIdMap = of(mediaType, schemaId); final Bean<MediaTypeToSchemaIdMapper> bean_1 = mock(Bean.class); final MediaTypeToSchemaIdMapper mediaTypeToSchemaIdMapper = mock(MediaTypeToSchemaIdMapper.class); when(schemaIdMappingObserver.getMediaTypeToSchemaIdMappers()).thenReturn(singletonList(bean_1)); when(beanInstantiater.instantiate(bean_1)).thenReturn(mediaTypeToSchemaIdMapper); when(mediaTypeToSchemaIdMapper.getMediaTypeToSchemaIdMap()).thenReturn(mediaTypeToSchemaIdMap); final Map<MediaType, String> mappingCache = schemaIdMappingCacheInitialiser.initialiseCache(); assertThat(mappingCache.size(), is(1)); assertThat(mappingCache.get(mediaType), is(schemaId)); }
@Test @SuppressWarnings("unchecked") public void shouldInitialiseCacheWithMultipleMappingBeans() throws Exception { final MediaType mediaType_1 = new MediaType("application/vnd.example.add-recipe+json"); final String schemaId_1 = "http: final MediaType mediaType_2 = new MediaType("application/vnd.example.remove-recipe+json"); final String schemaId_2 = "http: final MediaType mediaType_3 = new MediaType("application/vnd.example.update-recipe+json"); final String schemaId_3 = "http: final Map<MediaType, String> mediaTypeToSchemaIdMap_1 = of(mediaType_1, schemaId_1, mediaType_2, schemaId_2); final Map<MediaType, String> mediaTypeToSchemaIdMap_2 = of(mediaType_3, schemaId_3); final Bean<MediaTypeToSchemaIdMapper> bean_1 = mock(Bean.class); final Bean<MediaTypeToSchemaIdMapper> bean_2 = mock(Bean.class); final MediaTypeToSchemaIdMapper mediaTypeToSchemaIdMapper_1 = mock(MediaTypeToSchemaIdMapper.class); final MediaTypeToSchemaIdMapper mediaTypeToSchemaIdMapper_2 = mock(MediaTypeToSchemaIdMapper.class); when(schemaIdMappingObserver.getMediaTypeToSchemaIdMappers()).thenReturn(asList(bean_1, bean_2)); when(beanInstantiater.instantiate(bean_1)).thenReturn(mediaTypeToSchemaIdMapper_1); when(beanInstantiater.instantiate(bean_2)).thenReturn(mediaTypeToSchemaIdMapper_2); when(mediaTypeToSchemaIdMapper_1.getMediaTypeToSchemaIdMap()).thenReturn(mediaTypeToSchemaIdMap_1); when(mediaTypeToSchemaIdMapper_2.getMediaTypeToSchemaIdMap()).thenReturn(mediaTypeToSchemaIdMap_2); final Map<MediaType, String> mappingCache = schemaIdMappingCacheInitialiser.initialiseCache(); assertThat(mappingCache.size(), is(3)); assertThat(mappingCache.get(mediaType_1),is(schemaId_1)); assertThat(mappingCache.get(mediaType_2),is(schemaId_2)); assertThat(mappingCache.get(mediaType_3),is(schemaId_3)); }