FileSystemPreparationRepository extends ObjectPreparationRepository { @Override public void add(Identifiable object) { if (object == null) { LOG.warn("cannot save null..."); return; } final File outputFile = getIdentifiableFile(object); try { FileUtils.touch(outputFile); } catch (IOException e) { LOG.error("Unable to prepare file for {}.", object, e); } try (GZIPOutputStream output = new GZIPOutputStream(new FileOutputStream(outputFile))) { mapper.writer().writeValue(output, object); } catch (IOException e) { LOG.error("Error saving {}", object, e); throw new TDPException(CommonErrorCodes.UNABLE_TO_SAVE_PREPARATION, e, ExceptionContext.build().put("id", object.id())); } LOG.debug("{} #{} saved", object.getClass().getSimpleName(), object.id()); }  @Override void add(Identifiable object); @Override Stream<T> source(Class<T> clazz); @Override void clear(); @Override void remove(Identifiable object);  }
@Test public void shouldGetPreparationThatWasAdded() throws JsonProcessingException { Preparation expected = getPreparation("7561486"); repository.add(expected); final Preparation actual = repository.get(expected.id(), Preparation.class); assertEquals(expected, actual); }
@Test public void shouldGetStepThatWasAdded() { final Step expected = new Step(Step.ROOT_STEP.id(), "684fdqs638", versionService.version().getVersionId()); repository.add(expected); final Step actual = repository.get(expected.id(), Step.class); assertEquals(expected, actual); }
@Test public void shouldListActionThatWasAdded() { final List<Action> actions = PreparationTest.getSimpleAction("uppercase", "column_name", "lastname"); PreparationActions expected = new PreparationActions(actions, versionService.version().getVersionId()); repository.add(expected); final PreparationActions actual = repository.get(expected.id(), PreparationActions.class); assertEquals(expected, actual); }
@Test public void shouldGetOnlyWantedClass() { final Step expected = new Step(Step.ROOT_STEP.id(), "8rq4868", versionService.version().getVersionId()); repository.add(expected); assertNull(repository.get(expected.id(), Preparation.class)); assertNull(repository.get(expected.id(), PreparationActions.class)); }
@Test public void shouldOverwriteContent() { Preparation expected = getPreparation("khdgf"); expected.setName("old name"); repository.add(expected); expected.setName("new name"); repository.add(expected); final Preparation actual = repository.get(expected.id(), Preparation.class); assertEquals(expected, actual); }
@Test public void shouldListOnlyPreparationsContainingGivenName() { final List<Integer> ids = Arrays.asList(1, 12, 3); final List<Preparation> preparations = ids .stream() .map(i -> getPreparation(String.valueOf(i))) .collect(Collectors.toList()); preparations.forEach(prep -> repository.add(prep)); ids .stream() .map(i -> new Step(Step.ROOT_STEP.id(), "step" + i, versionService.version().getVersionId())) .forEach(step -> repository.add(step)); final Preparation expected1 = preparations.get(0); final Preparation expected2 = preparations.get(1); final Collection<Preparation> actual = repository.list(Preparation.class, contains("name", "1")).collect(Collectors.toList()); assertEquals(2, actual.size()); assertTrue(actual.contains(expected1)); assertTrue(actual.contains(expected2)); }
@Test public void shouldListOnlyPreparationsExactlyMatchingGivenName() { final List<Integer> ids = Arrays.asList(1, 12, 3); final List<Preparation> preparations = ids .stream() .map(i -> getPreparation(String.valueOf(i))) .collect(Collectors.toList()); preparations.forEach(prep -> repository.add(prep)); ids .stream() .map(i -> new Step(Step.ROOT_STEP.id(), "step" + i, versionService.version().getVersionId())) .forEach(step -> repository.add(step)); final Preparation expected = preparations.get(0); final Collection<Preparation> actual = repository.list(Preparation.class, eq("name", "prep-1")).collect(Collectors.toList()); assertEquals(1, actual.size()); assertTrue(actual.contains(expected)); }