private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId, long createdAt) { when(system2.now()).thenReturn(createdAt); return insertProperty(key, value, resourceId, userId); }
@Test public void select_by_query() { // global insertProperty("global.one", "one", null, null); insertProperty("global.two", "two", null, null); // struts insertProperty("struts.one", "one", 10L, null); // commons insertProperty("commonslang.one", "one", 11L, null); // user insertProperty("user.one", "one", null, 100); insertProperty("user.two", "two", 10L, 100); // other insertProperty("other.one", "one", 12L, null); List<PropertyDto> results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build(), db.getSession()); assertThat(results).hasSize(1); assertThat(results.get(0).getValue()).isEqualTo("two"); results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build(), db.getSession()); assertThat(results).hasSize(1); assertThat(results.get(0).getValue()).isEqualTo("one"); }
@Test public void delete_by_key_and_value() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto anotherProject = db.components().insertPrivateProject(); insertProperty("KEY", "VALUE", null, null); insertProperty("KEY", "VALUE", project.getId(), null); insertProperty("KEY", "VALUE", null, 100); insertProperty("KEY", "VALUE", project.getId(), 100); insertProperty("KEY", "VALUE", anotherProject.getId(), null); // Should not be removed insertProperty("KEY", "ANOTHER_VALUE", null, null); insertProperty("ANOTHER_KEY", "VALUE", project.getId(), 100); underTest.deleteByKeyAndValue(session, "KEY", "VALUE"); db.commit(); assertThat(db.select("select prop_key as \"key\", text_value as \"value\", resource_id as \"projectId\", user_id as \"userId\" from properties")) .extracting((row) -> row.get("key"), (row) -> row.get("value"), (row) -> row.get("projectId"), (row) -> row.get("userId")) .containsOnly(tuple("KEY", "ANOTHER_VALUE", null, null), tuple("ANOTHER_KEY", "VALUE", project.getId(), 100L)); }
@Test public void hasNotificationSubscribers() { int userId1 = db.users().insertUser(u -> u.setLogin("user1")).getId(); int userId2 = db.users().insertUser(u -> u.setLogin("user2")).getId(); Long projectId = insertPrivateProject("PROJECT_A").getId(); // global subscription insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userId2); // project subscription insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectId, userId1); insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", 56L, userId1); insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectId, userId1); // global subscription insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, userId2); // Nobody is subscribed assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("NotSexyDispatcher"))) .isFalse(); // Global subscribers assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithGlobalSubscribers"))) .isTrue(); // Project subscribers assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithProjectSubscribers"))) .isTrue(); assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithProjectSubscribers"))) .isFalse(); // Global + Project subscribers assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithGlobalAndProjectSubscribers"))) .isTrue(); assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithGlobalAndProjectSubscribers"))) .isTrue(); }
@Test public void selectGlobalProperty() { // global insertProperty("global.one", "one", null, null); insertProperty("global.two", "two", null, null); // project insertProperty("project.one", "one", 10L, null); // user insertProperty("user.one", "one", null, 100); assertThatDto(underTest.selectGlobalProperty("global.one")) .hasNoResourceId() .hasNoUserId() .hasValue("one"); assertThat(underTest.selectGlobalProperty("project.one")).isNull(); assertThat(underTest.selectGlobalProperty("user.one")).isNull(); assertThat(underTest.selectGlobalProperty("unexisting")).isNull(); }
@Test public void select_global_properties_by_keys() { insertPrivateProject("A"); int userId = db.users().insertUser(u -> u.setLogin("B")).getId(); String key = "key"; String anotherKey = "anotherKey"; insertProperty(key, "value", null, null); insertProperty(key, "value", 10L, null); insertProperty(key, "value", null, userId); insertProperty(anotherKey, "value", null, null); assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key))) .extracting("key") .containsOnly(key); assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey))) .extracting("key") .containsOnly(key, anotherKey); assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown"))) .extracting("key") .containsOnly(key, anotherKey); assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("unknown"))) .isEmpty(); }
@Test @UseDataProvider("allValuesForSelect") public void selectGlobalProperties_supports_all_values(String dbValue, String expected) { insertProperty("global.one", dbValue, null, null); List<PropertyDto> dtos = underTest.selectGlobalProperties(); assertThat(dtos) .hasSize(1); assertThatDto(dtos.iterator().next()) .hasKey("global.one") .hasNoResourceId() .hasNoUserId() .hasValue(expected); }
@Test public void selectProjectProperties() { ComponentDto projectDto = insertPrivateProject("A"); long projectId = projectDto.getId(); // global insertProperty("global.one", "one", null, null); insertProperty("global.two", "two", null, null); // project insertProperty("project.one", "Pone", projectId, null); insertProperty("project.two", "Ptwo", projectId, null); List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey()); assertThat(dtos) .hasSize(2); assertThatDto(findByKey(dtos, "project.one")) .hasKey("project.one") .hasResourceId(projectId) .hasValue("Pone"); assertThatDto(findByKey(dtos, "project.two")) .hasKey("project.two") .hasResourceId(projectId) .hasValue("Ptwo"); }
@Test @UseDataProvider("allValuesForSelect") public void selectProjectProperties_supports_all_values(String dbValue, String expected) { ComponentDto projectDto = insertPrivateProject("A"); insertProperty("project.one", dbValue, projectDto.getId(), null); List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey()); assertThat(dtos).hasSize(1); assertThatDto(dtos.iterator().next()) .hasKey("project.one") .hasResourceId(projectDto.getId()) .hasValue(expected); }
@Test public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() throws SQLException { long id = insertProperty("to_be_updated", "old_value", null, null, DATE_1); when(system2.now()).thenReturn(DATE_3); underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value")); assertThatPropertiesRow(id) .doesNotExist(); assertThatPropertiesRow("to_be_updated") .hasNoResourceId() .hasNoUserId() .hasTextValue("new value") .hasCreatedAt(DATE_3); }
@Test public void selectGlobalProperties() { // global long id1 = insertProperty("global.one", "one", null, null); long id2 = insertProperty("global.two", "two", null, null); List<PropertyDto> properties = underTest.selectGlobalProperties(); assertThat(properties.size()) .isEqualTo(2); assertThatDto(findByKey(properties, "global.one")) .hasKey("global.one") .hasNoUserId() .hasNoResourceId() .hasValue("one"); assertThatDto(findByKey(properties, "global.two")) .hasKey("global.two") .hasNoResourceId() .hasNoUserId() .hasValue("two"); }
@Test @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { long id = insertProperty("global", oldValue, null, null, DATE_1); when(system2.now()).thenReturn(DATE_4); underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue)); assertThatPropertiesRow(id) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") .hasNoResourceId() .hasNoUserId() .hasCreatedAt(DATE_4); if (newValue == null || newValue.isEmpty()) { propertiesRowAssert.isEmpty(); } else if (newValue.length() > 4000) { propertiesRowAssert.hasClobValue(newValue); } else { propertiesRowAssert.hasTextValue(newValue); } }
@Test @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { long resourceId = 999L; long id = insertProperty("global", oldValue, resourceId, null, DATE_1); when(system2.now()).thenReturn(DATE_4); underTest.saveProperty(new PropertyDto().setKey("global").setResourceId(resourceId).setValue(newValue)); assertThatPropertiesRow(id) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") .hasResourceId(resourceId) .hasNoUserId() .hasCreatedAt(DATE_4); if (newValue == null || newValue.isEmpty()) { propertiesRowAssert.isEmpty(); } else if (newValue.length() > 4000) { propertiesRowAssert.hasClobValue(newValue); } else { propertiesRowAssert.hasTextValue(newValue); } }
@Test public void delete_project_properties() { long id1 = insertProperty("sonar.profile.java", "Sonar Way", 1L, null); long id2 = insertProperty("sonar.profile.java", "Sonar Way", 2L, null); long id3 = insertProperty("sonar.profile.java", "Sonar Way", null, null); long id4 = insertProperty("sonar.profile.js", "Sonar Way", 1L, null); long id5 = insertProperty("sonar.profile.js", "Sonar Way", 2L, null); long id6 = insertProperty("sonar.profile.js", "Sonar Way", null, null);
@Test @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { int userId = 90; long id = insertProperty("global", oldValue, null, userId, DATE_1); when(system2.now()).thenReturn(DATE_4); underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue)); assertThatPropertiesRow(id) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") .hasNoResourceId() .hasUserId(userId) .hasCreatedAt(DATE_4); if (newValue == null || newValue.isEmpty()) { propertiesRowAssert.isEmpty(); } else if (newValue.length() > 4000) { propertiesRowAssert.hasClobValue(newValue); } else { propertiesRowAssert.hasTextValue(newValue); } }
@Test public void selectProjectProperty() { insertProperty("project.one", "one", 10L, null); PropertyDto property = underTest.selectProjectProperty(10L, "project.one"); assertThatDto(property) .hasKey("project.one") .hasResourceId(10L) .hasNoUserId() .hasValue("one"); }
@Test @UseDataProvider("allValuesForSelect") public void selectGlobalProperty_supports_all_values(String dbValue, String expected) { insertProperty("global.one", dbValue, null, null); assertThatDto(underTest.selectGlobalProperty("global.one")) .hasNoResourceId() .hasNoUserId() .hasValue(expected); }
@Test public void deleteGlobalProperty() { // global long id1 = insertProperty("global.key", "new_global", null, null); long id2 = insertProperty("to_be_deleted", "xxx", null, null); // project - do not delete this project property that has the same key long id3 = insertProperty("to_be_deleted", "new_project", 10L, null); // user long id4 = insertProperty("user.key", "new_user", null, 100); underTest.deleteGlobalProperty("to_be_deleted"); assertThatPropertiesRow(id1) .hasKey("global.key") .hasNoUserId() .hasNoResourceId() .hasTextValue("new_global"); assertThatPropertiesRow(id2) .doesNotExist(); assertThatPropertiesRow("to_be_deleted", null, null) .doesNotExist(); assertThatPropertiesRow(id3) .hasKey("to_be_deleted") .hasResourceId(10) .hasNoUserId() .hasTextValue("new_project"); assertThatPropertiesRow(id4) .hasKey("user.key") .hasNoResourceId() .hasUserId(100) .hasTextValue("new_user"); }
@Test public void delete_by_organization_and_matching_login() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); insertProperty("KEY_11", user.getLogin(), project.getId(), null); insertProperty("KEY_12", user.getLogin(), project.getId(), null); insertProperty("KEY_11", anotherUser.getLogin(), project.getId(), null); insertProperty("KEY_11", user.getLogin(), anotherProject.getId(), null); underTest.deleteByOrganizationAndMatchingLogin(session, organization.getUuid(), user.getLogin(), newArrayList("KEY_11", "KEY_12")); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session)) .hasSize(1) .extracting(PropertyDto::getValue).containsOnly(anotherUser.getLogin()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getValue) .hasSize(1).containsOnly(user.getLogin()); }
@Test public void delete_by_organization_and_user() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); insertProperty("KEY_11", "VALUE", project.getId(), user.getId()); insertProperty("KEY_12", "VALUE", project.getId(), user.getId()); insertProperty("KEY_11", "VALUE", project.getId(), anotherUser.getId()); insertProperty("KEY_11", "VALUE", anotherProject.getId(), user.getId()); underTest.deleteByOrganizationAndUser(session, organization.getUuid(), user.getId()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session)) .hasSize(1) .extracting(PropertyDto::getUserId).containsOnly(anotherUser.getId()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getUserId) .hasSize(1).containsOnly(user.getId()); }