mmiklavc commented on a change in pull request #1399: METRON-2073: Create in-memory use case for enrichment with map type and flatfile summarizer URL: https://github.com/apache/metron/pull/1399#discussion_r286642615
########## File path: metron-platform/metron-enrichment/metron-enrichment-common/src/test/java/org/apache/metron/enrichment/cache/ObjectCacheTest.java ########## @@ -0,0 +1,113 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.metron.enrichment.cache; + +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.metron.common.utils.SerDeUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ObjectCacheTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + FileSystem fs; + List<String> data; + ObjectCache cache; + + @Before + public void setup() throws IOException { + fs = FileSystem.get(new Configuration()); + data = new ArrayList<>(); + { + data.add("apache"); + data.add("metron"); + data.add("is"); + data.add("great"); + } + cache = new ObjectCache(); + } + + @Test + public void test() throws Exception { + String filename = "target/ogt/test.ser"; + Assert.assertTrue(cache.getCache() == null || !cache.getCache().asMap().containsKey(filename)); Review comment: I realize some of this was probably around before, but these are great opportunities to make incremental improvements to the code base with a minimal impact/risk. I think a better option than exposing class internals - see [Indecent Exposure](https://blog.codinghorror.com/code-smells/) - would be to offer methods that provide the functionality you're adding in the test. e.g. ``` ObjectCache cache = new ObjectCache(); Assert.assertTrue(cache.isEmpty()); Assert.assertTrue(cache.size() == 0); Assert.assertFalse(cache.hasKey(filename)); ``` It's both clearer and doesn't require you to expose the internals of your class under test. I get the compulsion to do this - you might be thinking "well, how do I know it's null?" I couldn't find the original text from Kent Beck's book on TDD online that I'm thinking of, but the red, green, refactor cycle really covers this pretty well - part of the refactor phase is to swap out a static implementation, e.g. "return false" and put the real thing there. The test should still pass when you refactor. Not that you're doing TDD here and I'm not suggesting that you do, rather my point is there's precedent for not having to expose class internals for tests. Here's some additional background on it - https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html. Also regarding class internals exposure - https://martinfowler.com/bliki/TellDontAsk.html > Tell-Don't-Ask is a principle that helps people remember that object-orientation is about bundling data with the functions that operate on that data. It reminds us that rather than asking an object for data and acting on that data, we should instead tell an object what to do. This approach also helps avoid "[train wrecks](http://wiki.c2.com/?TrainWreck)" and violating the [Law of Demeter](http://wiki.c2.com/?LawOfDemeter) as seen in `!cache.getCache().asMap().containsKey(filename)` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
