vertx/src/test/java/eu/internetofus/common/vertx/ModelResourcesTest.java
/**
   * Should create field element setting timestamps.
   *
   * @param searcher          the function that will search the model.
   * @param resultHandler     handler to manage the HTTP result.
   * @param success           function to call when the validation success.
   * @param storerCreateModel the function used to update a model.
   *
   * @see ModelResources#createModelFieldElementChain
   */
Should create field element setting timestamps.
@param searcher          the function that will search the model.
@param resultHandler     handler to manage the HTTP result.
@param success           function to call when the validation success.
@param storerCreateModel the function used to update a model.


Should create field element setting timestamps . @param searcher the function that will search the model . @param resultHandler handler to manage the HTTP result . @param success function to call when the validation success . @param storerCreateModel the function used to update a model .
@Test
  @SuppressWarnings("unchecked")
  public void shouldSetTimeStampWhenCreateFieldElement(
      @Mock final BiConsumer<String, Handler<AsyncResult<DummyTsModel>>> searcher,
      @Mock final BiConsumer<DummyTsModel, Handler<AsyncResult<Void>>> storerCreateModel,
      @Mock final Handler<AsyncResult<ServiceResponse>> resultHandler, @Mock final Runnable success) {
    final var context = this.createServiceContext(resultHandler);
    final var modelFieldContext = this.createModelFieldContextForDummiesInDummyTsModel();
    final var newElement = new DummyTsModel();
    newElement._creationTs = 2;
    newElement._lastUpdateTs = 3;
    newElement.value = "ElementToCreate";
    ModelResources.createModelFieldElementChain(newElement.toJsonObject(), modelFieldContext, searcher,
        model -> model.dummies, (model, dummies) -> model.dummies = dummies, storerCreateModel, context, success);
    final var expectedModel = new DummyTsModel();
    expectedModel._id = "1";
    expectedModel.value = "ModelToAddNewDummy";
    expectedModel._creationTs = 0;
    expectedModel._lastUpdateTs = 1;
    final var start = TimeManager.now();
    final ArgumentCaptor<Handler<AsyncResult<DummyTsModel>>> searchHandler = ArgumentCaptor.forClass(Handler.class);
    verify(searcher, timeout(30000).times(1)).accept(any(), searchHandler.capture());
    searchHandler.getValue().handle(Future.succeededFuture(expectedModel));
    final ArgumentCaptor<DummyTsModel> modelToStore = ArgumentCaptor.forClass(DummyTsModel.class);
    final ArgumentCaptor<Handler<AsyncResult<Void>>> storerHandler = ArgumentCaptor.forClass(Handler.class);
    verify(storerCreateModel, timeout(30000).times(1)).accept(modelToStore.capture(), storerHandler.capture());
    storerHandler.getValue().handle(Future.succeededFuture());
    final var storedElement = modelToStore.getValue();
    verify(success, timeout(30000).times(1)).run();
    final var end = TimeManager.now();
    assertThat(storedElement._creationTs).isEqualTo(0l);
    assertThat(storedElement._lastUpdateTs).isBetween(start, end);
    assertThat(storedElement.dummies).hasSize(1);
    assertThat(storedElement.dummies.get(0)._creationTs).isBetween(start, end);
    assertThat(storedElement.dummies.get(0)._lastUpdateTs).isEqualTo(storedElement.dummies.get(0)._creationTs);
    newElement._creationTs = storedElement.dummies.get(0)._creationTs;
    newElement._lastUpdateTs = storedElement.dummies.get(0)._lastUpdateTs;
    assertThat(storedElement.dummies.get(0)).isEqualTo(newElement);
  }
@ Test @ SuppressWarnings ( "unchecked" ) public void shouldSetTimeStampWhenCreateFieldElement ( @ Mock final BiConsumer < String , Handler < AsyncResult < DummyTsModel > > > searcher , @ Mock final BiConsumer < DummyTsModel , Handler < AsyncResult < Void > > > storerCreateModel , @ Mock final Handler < AsyncResult < ServiceResponse > > resultHandler , @ Mock final Runnable success ) { final var context = this . createServiceContext ( resultHandler ) ; final var modelFieldContext = this . createModelFieldContextForDummiesInDummyTsModel ( ) ; final var newElement = new DummyTsModel ( ) ; newElement . _creationTs = 2 ; newElement . _lastUpdateTs = 3 ; newElement . value = "ElementToCreate" ; ModelResources . createModelFieldElementChain ( newElement . toJsonObject ( ) , modelFieldContext , searcher , model -> model . dummies , ( model , dummies ) -> model . dummies = dummies , storerCreateModel , context , success ) ; final var expectedModel = new DummyTsModel ( ) ; expectedModel . _id = "1" ; expectedModel . value = "ModelToAddNewDummy" ; expectedModel . _creationTs = 0 ; expectedModel . _lastUpdateTs = 1 ; final var start = TimeManager . now ( ) ; final ArgumentCaptor < Handler < AsyncResult < DummyTsModel > > > searchHandler = ArgumentCaptor . forClass ( Handler . class ) ; verify ( searcher , timeout ( 30000 ) . times ( 1 ) ) . accept ( any ( ) , searchHandler . capture ( ) ) ; searchHandler . getValue ( ) . handle ( Future . succeededFuture ( expectedModel ) ) ; final ArgumentCaptor < DummyTsModel > modelToStore = ArgumentCaptor . forClass ( DummyTsModel . class ) ; final ArgumentCaptor < Handler < AsyncResult < Void > > > storerHandler = ArgumentCaptor . forClass ( Handler . class ) ; verify ( storerCreateModel , timeout ( 30000 ) . times ( 1 ) ) . accept ( modelToStore . capture ( ) , storerHandler . capture ( ) ) ; storerHandler . getValue ( ) . handle ( Future . succeededFuture ( ) ) ; final var storedElement = modelToStore . getValue ( ) ; verify ( success , timeout ( 30000 ) . times ( 1 ) ) . run ( ) ; final var end = TimeManager . now ( ) ; assertThat ( storedElement . _creationTs ) . isEqualTo ( 0l ) ; assertThat ( storedElement . _lastUpdateTs ) . isBetween ( start , end ) ; assertThat ( storedElement . dummies ) . hasSize ( 1 ) ; assertThat ( storedElement . dummies . get ( 0 ) . _creationTs ) . isBetween ( start , end ) ; assertThat ( storedElement . dummies . get ( 0 ) . _lastUpdateTs ) . isEqualTo ( storedElement . dummies . get ( 0 ) . _creationTs ) ; newElement . _creationTs = storedElement . dummies . get ( 0 ) . _creationTs ; newElement . _lastUpdateTs = storedElement . dummies . get ( 0 ) . _lastUpdateTs ; assertThat ( storedElement . dummies . get ( 0 ) ) . isEqualTo ( newElement ) ; }
Should create field element setting timestamps.
Should create field element setting timestamps .