OrganizationManager { @Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) public Organization create(Organization newOrganization) throws NotFoundException, ConflictException, ServerException { requireNonNull(newOrganization, "Required non-null organization"); requireNonNull(newOrganization.getName(), "Required non-null organization name"); String qualifiedName; if (newOrganization.getParent() != null) { final Organization parent = getById(newOrganization.getParent()); qualifiedName = parent.getQualifiedName() + "/" + newOrganization.getName(); } else { qualifiedName = newOrganization.getName(); } checkNameReservation(qualifiedName); final OrganizationImpl organization = new OrganizationImpl( NameGenerator.generate("organization", 16), qualifiedName, newOrganization.getParent()); organizationDao.create(organization); addFirstMember(organization); eventService.publish(new OrganizationPersistedEvent(organization)).propagateException(); return organization; } @Inject  OrganizationManager(
      EventService eventService,
      OrganizationDao organizationDao,
      MemberDao memberDao,
      @Named("che.auth.reserved_user_names") String[] reservedNames); @Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) Organization create(Organization newOrganization); @Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) Organization update(String organizationId, Organization update); @Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) void remove(String organizationId); Organization getById(String organizationId); Organization getByName(String organizationName); Page<? extends Organization> getByParent(String parent, int maxItems, long skipCount); Page<OrganizationImpl> getSuborganizations(
      String parentQualifiedName, int maxItems, long skipCount); Page<? extends Organization> getByMember(String userId, int maxItems, int skipCount); Page<? extends Member> getMembers(String organizationId, int maxItems, long skipCount);  }
@Test public void shouldCreateOrganization() throws Exception { final Organization toCreate = DtoFactory.newDto(OrganizationDto.class).withName("newOrg"); manager.create(toCreate); verify(organizationDao).create(organizationCaptor.capture()); final OrganizationImpl createdOrganization = organizationCaptor.getValue(); assertEquals(createdOrganization.getName(), toCreate.getName()); assertEquals(createdOrganization.getQualifiedName(), toCreate.getName()); assertEquals(createdOrganization.getParent(), toCreate.getParent()); verify(eventService).publish(persistEventCaptor.capture()); assertEquals(persistEventCaptor.getValue().getOrganization(), createdOrganization); verify(memberDao) .store( new MemberImpl(USER_ID, createdOrganization.getId(), OrganizationDomain.getActions())); }
@Test public void shouldGenerateIdentifierWhenCreatingOrganization() throws Exception { final Organization organization = DtoFactory.newDto(OrganizationDto.class).withName("newOrg").withId("identifier"); manager.create(organization); verify(organizationDao).create(organizationCaptor.capture()); final String id = organizationCaptor.getValue().getId(); assertNotNull(id); assertNotEquals(id, "identifier"); }
@Test(expectedExceptions = ConflictException.class) public void shouldThrowConflictExceptionOnCreationIfOrganizationNameIsReserved() throws Exception { final Organization organization = DtoFactory.newDto(OrganizationDto.class).withName("reserved").withParent(null); manager.create(organization); }
@Test(expectedExceptions = NullPointerException.class) public void shouldThrowNpeWhenCreatingNullableOrganization() throws Exception { manager.create(null); }