ViscosityInterpolatorMap { public ViscosityInterpolator getFor(String key) { if (viscosityMap.containsKey(key)) { return viscosityMap.get(key); } else { return defaultViscosity; } } ViscosityInterpolatorMap(Map<String, ViscosityInterpolator> viscosityMap, ViscosityInterpolator defaultViscosity); static ViscosityInterpolatorMap newInstance(Map<String, ViscosityInterpolator> viscosities); ViscosityInterpolator getFor(String key);  }
@Test public void givenAKnownViscosityWhenWeRetrieveItByTheCorrectKeyThenTheCorrectViscosityIsReturned() throws Exception { Map<String, ViscosityInterpolator> viscosities = new HashMap<>(); String key = "TestKey"; StubViscosityInterpolator expectedViscosity = new StubViscosityInterpolator(); viscosities.put(key, expectedViscosity); ViscosityInterpolatorMap collection = new ViscosityInterpolatorMap(viscosities, new StubViscosityInterpolator()); ViscosityInterpolator actualViscosity = collection.getFor(key); assertThat(actualViscosity).isEqualTo(expectedViscosity); }
@Test public void givenAKnownViscosityWhenWeRetrieveItByAnIncorrectKeyThenTheCorrectViscosityIsNotReturned() throws Exception { Map<String, ViscosityInterpolator> viscosities = new HashMap<>(); StubViscosityInterpolator expectedViscosity = new StubViscosityInterpolator(); viscosities.put("TestKey", expectedViscosity); ViscosityInterpolatorMap collection = new ViscosityInterpolatorMap(viscosities, new StubViscosityInterpolator()); ViscosityInterpolator actualViscosity = collection.getFor("WrongTestKey"); assertThat(actualViscosity).isNotEqualTo(expectedViscosity); }
@Test public void givenAKnownViscosityWhenWeRetrieveItByAnIncorrectKeyThenTheDefaultViscosityIsReturned() throws Exception { Map<String, ViscosityInterpolator> viscosities = new HashMap<>(); StubViscosityInterpolator expectedViscosity = new StubViscosityInterpolator(); viscosities.put("TestKey", expectedViscosity); StubViscosityInterpolator defaultViscosity = new StubViscosityInterpolator(); ViscosityInterpolatorMap collection = new ViscosityInterpolatorMap(viscosities, defaultViscosity); ViscosityInterpolator actualViscosity = collection.getFor("WrongTestKey"); assertThat(actualViscosity).isEqualTo(defaultViscosity); }
@Test public void givenAnEmptyCollectionWhenWeRetrieveByAnyKeyThenTheDefaultViscosityIsReturned() throws Exception { Map<String, ViscosityInterpolator> viscosities = Collections.emptyMap(); StubViscosityInterpolator defaultViscosity = new StubViscosityInterpolator(); ViscosityInterpolatorMap collection = new ViscosityInterpolatorMap(viscosities, defaultViscosity); ViscosityInterpolator actualViscosity = collection.getFor("AnyTestKey"); assertThat(actualViscosity).isEqualTo(defaultViscosity); }