### Question:
MetaDataBasedTargetSelector implements
        BiFunction<Map<String, MetaDataValue>, Set<ClientStreamIdentification>, Set<ClientStreamIdentification>> { @Override public Set<ClientStreamIdentification> apply(Map<String, MetaDataValue> metaDataMap, Set<ClientStreamIdentification> candidates) { if (candidates.size() < 2 || metaDataMap.isEmpty()) { return candidates; } Map<ClientStreamIdentification, Integer> scorePerClient = new HashMap<>(); candidates.forEach(candidate -> scorePerClient.computeIfAbsent(candidate, m -> score(metaDataMap, m))); return getHighestScore(scorePerClient); } MetaDataBasedTargetSelector(ClientTagsCache clientTagsCache); @Override Set<ClientStreamIdentification> apply(Map<String, MetaDataValue> metaDataMap,
                                                 Set<ClientStreamIdentification> candidates);  }### Answer:
@Test public void findOnOneMetaDataValue() { clientTagsCache.on(new ClientTagsUpdate("client1", "context1", Collections.singletonMap("location", "Europe"))); clientTagsCache.on(new ClientTagsUpdate("client2", "context1", Collections.singletonMap("location", "Asia"))); Set<ClientStreamIdentification> clientIdentifications = new HashSet<>(); clientIdentifications.add(new ClientStreamIdentification("context1", "client1")); clientIdentifications.add(new ClientStreamIdentification("context1", "client2")); Map<String, MetaDataValue> metaData = Collections.singletonMap("location", MetaDataValue.newBuilder().setTextValue("Asia") .build()); Set<ClientStreamIdentification> targets = testSubject.apply(metaData, clientIdentifications); assertEquals(1, targets.size()); ClientStreamIdentification client = targets.iterator().next(); assertEquals("client2", client.getClientStreamId()); }
@Test public void findWithoutMetaData() { clientTagsCache.on(new ClientTagsUpdate("client1", "context1", Collections.singletonMap("location", "Europe"))); clientTagsCache.on(new ClientTagsUpdate("client2", "context1", Collections.singletonMap("location", "Asia"))); Set<ClientStreamIdentification> clientIdentifications = new HashSet<>(); clientIdentifications.add(new ClientStreamIdentification("context1", "client1")); clientIdentifications.add(new ClientStreamIdentification("context1", "client2")); Set<ClientStreamIdentification> targets = testSubject.apply(Collections.emptyMap(), clientIdentifications); assertEquals(2, targets.size()); }
@Test public void findClientNotInCache() { clientTagsCache.on(new ClientTagsUpdate("client1", "context1", Collections.singletonMap("location", "Europe"))); clientTagsCache.on(new ClientTagsUpdate("client2", "context1", Collections.singletonMap("location", "Asia"))); Set<ClientStreamIdentification> clientIdentifications = new HashSet<>(); clientIdentifications.add(new ClientStreamIdentification("context1", "client3")); Set<ClientStreamIdentification> targets = testSubject.apply(Collections.emptyMap(), clientIdentifications); assertEquals(1, targets.size()); ClientStreamIdentification client = targets.iterator().next(); assertEquals("client3", client.getClientStreamId()); }
@Test public void findWithoutMatchingMetaData() { clientTagsCache.on(new ClientTagsUpdate("client1", "context1", Collections.singletonMap("location", "Europe"))); clientTagsCache.on(new ClientTagsUpdate("client2", "context1", Collections.singletonMap("location", "Asia"))); Set<ClientStreamIdentification> clientIdentifications = new HashSet<>(); clientIdentifications.add(new ClientStreamIdentification("context1", "client1")); clientIdentifications.add(new ClientStreamIdentification("context1", "client2")); Map<String, MetaDataValue> metaData = Collections.singletonMap("location", MetaDataValue.newBuilder().setTextValue("Africa") .build()); Set<ClientStreamIdentification> targets = testSubject.apply(metaData, clientIdentifications); assertEquals(2, targets.size()); }