<commit_before>package banking.persistence;

import org.junit.Test;

public class FileRepositoryTest {
  
  @Test
  public void testName() throws Exception {
    
  }<commit_msg>Start wrigin the FileRepository<commit_after>package banking.persistence;

import static org.assertj.core.api.Assertions.assertThat;

import banking.model.Identifiable;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.assertj.core.util.Files;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class FileRepositoryTest {
  
  private File tempFile;

  @Before
  public void makeTempFile() throws Exception {
    tempFile = File.createTempFile("banking", "test.json");
    tempFile.deleteOnExit();
  }
  
  @Test
  public void savesNewItemsToAFile() throws Exception {
    FileRepository<FakeItem> repository = new FileRepository<FakeItem>(tempFile);
    
    FakeItem item = new FakeItem();
    repository.save(item);
    
    String fileContents = Files.contentOf(tempFile, Charset.defaultCharset());
    assertThat(fileContents).containsSequence(item.getId().toString());
  }
  
  @Test
  @Ignore
  public void findsAnItemFromFileInJSONFormat() throws Exception {
    FakeItem item1 = new FakeItem();
    FakeItem item2 = new FakeItem();
    
    try (FileWriter writer = new FileWriter(tempFile)) {
      List<FakeItem> items = Arrays.asList(item1, item2);
      
      Type type = new TypeToken<ArrayList<Identifiable>>(){}.getType();
      
      new Gson().toJson(items, type, writer);
    }
    FileRepository<FakeItem> repository = new FileRepository<FakeItem>(tempFile);

    Optional<FakeItem> foundItem = repository.findOne(item1.getId());
    
    assertThat(foundItem).isPresent();
    assertThat(foundItem.get().getId()).isEqualTo(item1.getId());
    
  }
{'fraction_non_alphanumeric': 0.11352418558736427, 'fraction_numerical': 0.0029615004935834156, 'mean_word_length': 4.264935064935065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1369148', 'n_tokens_mistral': 610, 'n_tokens_neox': 615, 'n_words': 128}