@Test public void description_url_avatarUrl_and_userId_are_optional() { when(system2.now()).thenReturn(SOME_DATE); insertOrganization(copyOf(ORGANIZATION_DTO_1).setDescription(null).setUrl(null).setAvatarUrl(null)); Map<String, Object> row = selectSingleRow(); assertThat(row.get("uuid")).isEqualTo(ORGANIZATION_DTO_1.getUuid()); assertThat(row.get("key")).isEqualTo(ORGANIZATION_DTO_1.getKey()); assertThat(row.get("name")).isEqualTo(ORGANIZATION_DTO_1.getName()); assertThat(row.get("description")).isNull(); assertThat(row.get("url")).isNull(); assertThat(row.get("avatarUrl")).isNull(); assertThat(row.get("guarded")).isEqualTo(toBool(ORGANIZATION_DTO_1.isGuarded())); assertThat(row.get("userId")).isNull(); assertThat(row.get("createdAt")).isEqualTo(SOME_DATE); assertThat(row.get("updatedAt")).isEqualTo(SOME_DATE); assertThat(row.get("defaultTemplate")).isNull(); assertThat(row.get("projectDefaultTemplate")).isNull(); assertThat(row.get("viewDefaultTemplate")).isNull(); }
@Test public void update_fails_with_NPE_if_OrganizationDto_is_null() { expectDtoCanNotBeNull(); underTest.update(dbSession, null); }
private int insertPrivateProjectsWithBranches(OrganizationDto org, MetricDto ncloc) { // private project ComponentDto project1 = db.components().insertMainBranch(org); return Math.max( // Create the ncloc on main branch insertLiveMeasures(project1, ncloc, 0), // Create 5 branches and set the ncloc on them IntStream.range(1, 5) .map(i -> insertLiveMeasures(db.components().insertProjectBranch(project1), ncloc, 0)) .max().orElse(0) ); }
@Test public void getDefaultTemplates_returns_data_when_project_and_view_default_template_column_are_not_null() { insertOrganization(ORGANIZATION_DTO_1); setDefaultTemplate(ORGANIZATION_DTO_1, PERMISSION_1, PERMISSION_2); verifyGetDefaultTemplates(ORGANIZATION_DTO_1, PERMISSION_1, PERMISSION_2); }
@Test public void insert_persists_boolean_property_guarded_of_OrganizationDto() { insertOrganization(ORGANIZATION_DTO_2); Map<String, Object> row = selectSingleRow(); assertThat(row.get("guarded")).isEqualTo(toBool(ORGANIZATION_DTO_2.isGuarded())); }
@Test public void countByQuery() { insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid3").setKey("key-3")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid1").setKey("key-1")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid2").setKey("key-2")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid5").setKey("key-5")); insertOrganization(copyOf(ORGANIZATION_DTO_1).setUuid("uuid4").setKey("key-4")); assertThat(underTest.countByQuery(dbSession, returnAll())).isEqualTo(5); assertThat(underTest.countByQuery(dbSession, newQueryWithKeys("key-1", "key-2"))).isEqualTo(2); assertThat(underTest.countByQuery(dbSession, newQueryWithKeys("unknown"))).isZero(); }
@Test public void selectByQuery_with_only_non_existent_keys_returns_empty() { insertOrganization(ORGANIZATION_DTO_1); insertOrganization(ORGANIZATION_DTO_2); OrganizationQuery organizationQuery = newQueryWithKeys(PERMISSION_1, PERMISSION_2, "dog"); assertThat(underTest.selectByQuery(dbSession, organizationQuery, forPage(1).andSize(10))) .isEmpty(); }
@Test public void selectByUuids_returns_empty_when_no_uuid_exist() { insertOrganization(ORGANIZATION_DTO_1); insertOrganization(ORGANIZATION_DTO_2); assertThat(underTest.selectByUuids(dbSession, of("foo uuid", "bar uuid"))) .isEmpty(); }
@Test public void selectByUuids_returns_row_data_of_multiple_uuid_when_uuid_exists() { insertOrganization(ORGANIZATION_DTO_1); insertOrganization(ORGANIZATION_DTO_2); List<OrganizationDto> dtos = underTest.selectByUuids(dbSession, of(ORGANIZATION_DTO_1.getUuid(), ORGANIZATION_DTO_2.getUuid())); assertThat(dtos).hasSize(2); verifyOrganization1(dtos.stream().filter((t) -> t.getUuid().equals(ORGANIZATION_DTO_1.getUuid())).findFirst().get()); verifyOrganization(dtos.stream().filter((t) -> t.getUuid().equals(ORGANIZATION_DTO_2.getUuid())).findFirst().get(), ORGANIZATION_DTO_2); }
@Test public void update_fails_if_name_is_null() { insertOrganization(ORGANIZATION_DTO_1); expectedException.expect(PersistenceException.class); underTest.update(dbSession, copyOf(ORGANIZATION_DTO_1).setName(null)); }
@Test public void selectByUuids_returns_row_data_of_single_uuid_when_uuid_exists() { insertOrganization(ORGANIZATION_DTO_1); List<OrganizationDto> dtos = underTest.selectByUuids(dbSession, singleton(ORGANIZATION_DTO_1.getUuid())); assertThat(dtos).hasSize(1); verifyOrganization1(dtos.iterator().next()); }
@Test public void selectByKey_returns_row_data_when_key_exists() { OrganizationDto organizationDto = newOrganizationDto(); insertOrganization(organizationDto); Optional<OrganizationDto> optional = underTest.selectByKey(dbSession, organizationDto.getKey()); verifyOrganization(optional.get(), organizationDto); }
@Test public void update_with_same_information_succeeds_but_has_no_effect() { OrganizationDto organizationDto = newOrganizationDto(); insertOrganization(organizationDto); underTest.update(dbSession, organizationDto); dbSession.commit(); Map<String, Object> row = selectSingleRow(); assertThat(row.get("uuid")).isEqualTo(organizationDto.getUuid()); assertThat(row.get("key")).isEqualTo(organizationDto.getKey()); assertThat(row.get("name")).isEqualTo(organizationDto.getName()); assertThat(row.get("description")).isEqualTo(organizationDto.getDescription()); assertThat(row.get("url")).isEqualTo(organizationDto.getUrl()); assertThat(row.get("avatarUrl")).isEqualTo(organizationDto.getAvatarUrl()); assertThat(row.get("subscription")).isEqualTo(organizationDto.getSubscription().name()); assertThat(row.get("createdAt")).isEqualTo(organizationDto.getCreatedAt()); assertThat(row.get("updatedAt")).isEqualTo(organizationDto.getUpdatedAt()); }
@Test public void getDefaultTemplates_returns_data_when_project_default_templates_column_is_not_null() { insertOrganization(ORGANIZATION_DTO_1); underTest.setDefaultTemplates(dbSession, ORGANIZATION_DTO_1.getUuid(), new DefaultTemplates().setProjectUuid(PERMISSION_1)); verifyGetDefaultTemplates(ORGANIZATION_DTO_1, PERMISSION_1, null); }
@Test public void getDefaultGroupId_returns_data_when_default_group_id_is_not_null() { when(system2.now()).thenReturn(DATE_3); insertOrganization(ORGANIZATION_DTO_1); underTest.setDefaultGroupId(dbSession, ORGANIZATION_DTO_1.getUuid(), GroupTesting.newGroupDto().setId(10)); Optional<Integer> optional = underTest.getDefaultGroupId(dbSession, ORGANIZATION_DTO_1.getUuid()); assertThat(optional).isNotEmpty(); assertThat(optional.get()).isEqualTo(10); verifyOrganizationUpdatedAt(ORGANIZATION_DTO_1.getUuid(), DATE_3); }
@Test public void selectOrganizationsWithNcloc_with_multiple_organizations() { MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(CoreMetrics.NCLOC_KEY)); Tuple[] expectedResults = new Tuple[9]; List<String> orgUuids = new ArrayList<>(); IntStream.range(0, 9).forEach( i -> { OrganizationDto org = db.organizations().insert(); orgUuids.add(org.getUuid()); int maxPrivate = insertPrivateProjectsWithBranches(org, ncloc); // Now we are creating public project asking for maxPrivate as minimum ncloc // because those projects *MUST* not be taken during the calculation of ncloc for a private // organization insertPublicProjectsWithBranches(org, ncloc, maxPrivate); expectedResults[i] = tuple(org.getUuid(), org.getKey(), org.getName(), (long) maxPrivate); } ); assertThat(underTest.selectOrganizationsWithNcloc(dbSession, orgUuids)) .extracting(OrganizationWithNclocDto::getId, OrganizationWithNclocDto::getKee, OrganizationWithNclocDto::getName, OrganizationWithNclocDto::getNcloc) .containsExactlyInAnyOrder(expectedResults); }
@Test public void selectByQuery_filter_on_type() { OrganizationDto personalOrg1 = db.organizations().insert(); db.users().insertUser(u -> u.setOrganizationUuid(personalOrg1.getUuid())); OrganizationDto personalOrg2 = db.organizations().insert(); db.users().insertUser(u -> u.setOrganizationUuid(personalOrg2.getUuid())); OrganizationDto teamOrg1 = db.organizations().insert(); assertThat(selectUuidsByQuery(q -> q.setOnlyPersonal(), forPage(1).andSize(100))) .containsExactlyInAnyOrder(personalOrg1.getUuid(), personalOrg2.getUuid()); assertThat(selectUuidsByQuery(q -> q.setOnlyTeam(), forPage(1).andSize(100))) .containsExactlyInAnyOrder(teamOrg1.getUuid()); }
@Test public void getDefaultTemplates_returns_empty_when_only_view_default_template_column_is_not_null() { dirtyInsertWithDefaultTemplate("uuid1", null, PERMISSION_2); assertThat(underTest.getDefaultTemplates(dbSession, "uuid1")) .isEmpty(); }
@Test public void selectByQuery_with_keys_returns_empty_when_table_is_empty() { assertThat(underTest.selectByQuery(dbSession, newQueryWithKeys("key1", "key2"), forPage(2).andSize(1))) .isEmpty(); }
@Test public void insert_persists_properties_of_OrganizationDto() { OrganizationDto organization = newOrganizationDto(); insertOrganization(organization); Map<String, Object> row = selectSingleRow(); assertThat(row.get("uuid")).isEqualTo(organization.getUuid()); assertThat(row.get("key")).isEqualTo(organization.getKey()); assertThat(row.get("name")).isEqualTo(organization.getName()); assertThat(row.get("description")).isEqualTo(organization.getDescription()); assertThat(row.get("url")).isEqualTo(organization.getUrl()); assertThat(row.get("avatarUrl")).isEqualTo(organization.getAvatarUrl()); assertThat(row.get("createdAt")).isEqualTo(organization.getCreatedAt()); assertThat(row.get("updatedAt")).isEqualTo(organization.getUpdatedAt()); assertThat(row.get("guarded")).isEqualTo(toBool(organization.isGuarded())); assertThat(row.get("subscription")).isEqualTo(organization.getSubscription().name()); assertThat(row.get("defaultTemplate")).isNull(); assertThat(row.get("projectDefaultTemplate")).isNull(); assertThat(row.get("viewDefaultTemplate")).isNull(); }