SimplQ / simplQ-backend

SimplQ backend, written in Java for AWS
https://simplq.me
GNU General Public License v3.0
17 stars 27 forks source link

We should have a validator class for queue and token objects #137

Closed daltonfury42 closed 3 years ago

daltonfury42 commented 3 years ago

We have different tests on queue and token entity objects done all over the place.

This should be extracted into other methods and use function composition. It is very hard to follow.

  Predicate<Queue> isNotPaused() {
    return queue -> {
      if (queue.getStatus() == QueueStatus.PAUSED) {
        throw SQInvalidRequestException.queuePausedException();
      }

      return true;
    };
  }

  Predicate<Queue> isNotDeleted() {
    return queue -> {
      if (queue.getStatus() == QueueStatus.DELETED) {
        throw SQInvalidRequestException.queueDeletedException();
      }

      return true;
    };
  }

  Predicate<Queue> isNotFull() {
    return queue -> {
      if (queue.isFull()) {
        throw SQInvalidRequestException.queueDeletedException();
      }

      return true;
    };
  }

  void validateQueue(Queue queue) {
    isNotPaused()
        .and(isNotDeleted())
        .and(isNotFull())
        .test(queue);
  }

and then just call validateQueue(queue)

_Originally posted by @chalx in https://github.com/SimplQ/simplQ-backend/pull/130#discussion_r619428589_

daltonfury42 commented 3 years ago

Also explore this: https://github.com/SimplQ/simplQ-backend/pull/130/files#r619404455