GroupValidator { void validateRegistrationThrowing(Group group) throws ServiceException { if (group.getOwners().getItemsCount() == 0) { throw new ServiceException(Problem.clientProblem("no_owner_for_group", "A group must have at least one one owner", 422)); } group.getOwners().getItemsList().forEach(owner -> { if (Strings.isNullOrEmpty(owner.getEmail()) && Strings.isNullOrEmpty(owner.getUsername())) { throw new ServiceException(Problem.clientProblem("incomplete_owner_for_group", "A group owner must have an email or a username", 422)); } }); if (Strings.isNullOrEmpty(group.getKey())) { throw new ServiceException(Problem.clientProblem("missing_key_for_group", "A group must have a key", 422)); } if (Strings.isNullOrEmpty(group.getName())) { throw new ServiceException(Problem.clientProblem("missing_name_for_group", "A group must have a name", 422)); } }    }
@Test public void validateRegistrationThrowingOk() { Group.Builder builder = Group.newBuilder(); OwnerCollection.Builder oc = OwnerCollection.newBuilder() .addItems(Owner.newBuilder().setName("Bob").setUsername("bob")); builder.setOwners(oc) .setKey("key1") .setName("ns 1"); try { new GroupValidator().validateRegistrationThrowing(builder.build()); } catch (ServiceException e) { fail(); } }