def index_from_json(self, json_string): """ Indexes the given name and the meta data into elasticsearch. Uses or creates the index and type defined in the config :param name: Name of the sdearchive dataset (String) :param meta_data: Set of meta data (Map) :exception: Throws an IndexingException if something went wrong during the request """ data = "No Data" try: data = json_string self.__c_logger.debug("Indexing data: " + str(data)) self.__f_logger.debug("Indexing data: " + str(data)) httplib2.debuglevel = 1 h = httplib2.Http(".cache") url = self.__config["host"] + "/" + self.__config["index"] + "/" + self.__config["type"] print "DATA->" + data print "URL->" + url resp, content = h.request(url, "POST", data) except Exception as e: cache = FailedIndexingCache() cache.write_to_cache(data) self.__c_logger.exception("ERROR while indexing data: " + str(data) + "\n\n" + e.message) self.__f_logger.exception("ERROR while indexing data: " + str(data) + "\n\n" + e.message) raise IndexingException( "ERROR while indexing data: " + str(data) + " -> For more details see the log messages" )
def setUp(self): try: os.remove("./test.cache") except: print "no file" self.cache = FailedIndexingCache("test.cache")
class FailedIndexingCacheTest(unittest.TestCase): def setUp(self): try: os.remove("./test.cache") except: print "no file" self.cache = FailedIndexingCache("test.cache") def tearDown(self): self.cache = None try: os.remove("./test.cache") except: print "no file" def test_reading_particular_written_lines_from_cache_multiple_lines_returns_list_of_these_lines(self): self.cache.write_to_cache("Test line one") self.cache.write_to_cache("Test line two") entries = self.cache.read_from_cache() self.assertEqual(len(entries), 2) def test_reading_multiple_written_lines_from_cache_multiple_lines_returns_list_of_these_lines(self): self.cache.write_all_to_cache(["Test line one", "Test line two"]) entries = self.cache.read_from_cache() self.assertEqual(len(entries), 2) def test_clear_cache_on_data_available_empties_the_file(self): self.cache.write_all_to_cache(["Test line one", "Test line two"]) entries_before = self.cache.read_from_cache() self.cache.clear_cache() entries_after = self.cache.read_from_cache() self.assertEqual(len(entries_before), 2) self.assertEqual(len(entries_after), 0)