@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 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("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 @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 delete_project_property() { long projectId1 = insertPrivateProject("A").getId(); long projectId2 = insertPrivateProject("B").getId(); long projectId3 = insertPrivateProject("C").getId(); long id1 = insertProperty("global.one", "one", null, null); long id2 = insertProperty("global.two", "two", null, null); long id3 = insertProperty("struts.one", "one", projectId1, null); long id4 = insertProperty("commonslang.one", "one", projectId2, null); long id5 = insertProperty("user.one", "one", null, 100); long id6 = insertProperty("user.two", "two", null, 100); long id7 = insertProperty("other.one", "one", projectId3, null); assertThatPropertiesRow(id1) .hasKey("global.one") .hasNoResourceId() .hasNoUserId() .hasTextValue("one"); assertThatPropertiesRow(id2) .hasKey("global.two") .hasNoResourceId() .hasNoUserId() .hasTextValue("two"); assertThatPropertiesRow(id3) .doesNotExist(); assertThatPropertiesRow(id4) .hasKey("commonslang.one") .hasResourceId(projectId2) .hasNoUserId() .hasTextValue("one");
@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(); }
underTest.saveGlobalProperties(mapOf( "null_value_property", null, "empty_value_property", "", assertThatPropertiesRow("null_value_property") .hasNoResourceId() .hasNoUserId() .isEmpty() .hasCreatedAt(DATE_1); assertThatPropertiesRow("empty_value_property") .hasNoResourceId() .hasNoUserId() .isEmpty() .hasCreatedAt(DATE_2); assertThatPropertiesRow("text_value_property") .hasNoResourceId() .hasNoUserId() .hasTextValue("dfdsfsd") .hasCreatedAt(DATE_3); assertThatPropertiesRow("4000_char_value_property") .hasNoResourceId() .hasNoUserId() .hasTextValue(VALUE_SIZE_4000) .hasCreatedAt(DATE_4); assertThatPropertiesRow("clob_value_property") .hasNoResourceId() .hasNoUserId()
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); }
underTest.saveProperty(new PropertyDto().setKey("global.clob").setValue(VALUE_SIZE_4001)); assertThatPropertiesRow("global.null") .hasNoResourceId() .hasNoUserId() .isEmpty() .hasCreatedAt(DATE_1); assertThatPropertiesRow("global.empty") .hasNoResourceId() .hasNoUserId() .isEmpty() .hasCreatedAt(DATE_2); assertThatPropertiesRow("global.text") .hasNoResourceId() .hasNoUserId() .hasTextValue("some text") .hasCreatedAt(DATE_3); assertThatPropertiesRow("global.4000") .hasNoResourceId() .hasNoUserId() .hasTextValue(VALUE_SIZE_4000) .hasCreatedAt(DATE_4); assertThatPropertiesRow("global.clob") .hasNoResourceId() .hasNoUserId()
@Test public void select_properties_by_keys_and_component_ids() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); String key = "key"; String anotherKey = "anotherKey"; insertProperties( newGlobalPropertyDto().setKey(key), newComponentPropertyDto(project).setKey(key), newComponentPropertyDto(project2).setKey(key), newComponentPropertyDto(project2).setKey(anotherKey), newUserPropertyDto(user).setKey(key)); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId()))) .extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId(), project2.getId()))) .extracting("key", "resourceId").containsOnly( tuple(key, project.getId()), tuple(key, project2.getId())); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key, anotherKey), newHashSet(project.getId(), project2.getId()))) .extracting("key", "resourceId").containsOnly( tuple(key, project.getId()), tuple(key, project2.getId()), tuple(anotherKey, project2.getId())); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(project.getId()))).isEmpty(); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("key"), newHashSet(123456789L))).isEmpty(); assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(123456789L))).isEmpty(); }
@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 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"); }
underTest.saveProperty(new PropertyDto().setKey("user.clob").setUserId(userId).setValue(VALUE_SIZE_4001)); assertThatPropertiesRow("user.null") .hasNoResourceId() .hasUserId(userId) .isEmpty() .hasCreatedAt(DATE_1); assertThatPropertiesRow("user.empty") .hasNoResourceId() .hasUserId(userId) .isEmpty() .hasCreatedAt(DATE_2); assertThatPropertiesRow("user.text") .hasNoResourceId() .hasUserId(userId) .hasTextValue("some text") .hasCreatedAt(DATE_3); assertThatPropertiesRow("user.4000") .hasNoResourceId() .hasUserId(userId) .hasTextValue(VALUE_SIZE_4000) .hasCreatedAt(DATE_4); assertThatPropertiesRow("user.clob") .hasNoResourceId() .hasUserId(userId)
@Test public void select_component_properties_by_ids() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); String key = "key"; String anotherKey = "anotherKey"; insertProperties( newGlobalPropertyDto().setKey(key), newComponentPropertyDto(project).setKey(key), newComponentPropertyDto(project2).setKey(key), newComponentPropertyDto(project2).setKey(anotherKey), newUserPropertyDto(user).setKey(key)); assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId()))) .extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId(), project2.getId()))) .extracting("key", "resourceId").containsOnly( tuple(key, project.getId()), tuple(key, project2.getId()), tuple(anotherKey, project2.getId())); assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty(); }
@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); assertThatPropertiesRow(id1) .doesNotExist(); assertThatPropertiesRow(id2) .doesNotExist(); assertThatPropertiesRow(id3) .hasKey("sonar.profile.java") .hasNoResourceId() .hasNoUserId() .hasTextValue("Sonar Way"); assertThatPropertiesRow(id4) .hasKey("sonar.profile.js") .hasResourceId(1) .hasNoUserId() .hasTextValue("Sonar Way"); assertThatPropertiesRow(id5) .hasKey("sonar.profile.js") .hasResourceId(2) .hasNoUserId() .hasTextValue("Sonar Way");
@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 public void shouldFindUsersForNotification() { ComponentDto project1 = insertPrivateProject("uuid_45"); ComponentDto project2 = insertPrivateProject("uuid_56"); UserDto user1 = db.users().insertUser(u -> u.setLogin("user1")); UserDto user2 = db.users().insertUser(u -> u.setLogin("user2")); UserDto user3 = db.users().insertUser(u -> u.setLogin("user3")); insertProperty("notification.NewViolations.Email", "true", project1.getId(), user2.getId()); insertProperty("notification.NewViolations.Twitter", "true", null, user3.getId()); insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), user1.getId()); insertProperty("notification.NewViolations.Twitter", "true", project1.getId(), user2.getId()); insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), user3.getId()); db.users().insertProjectPermissionOnUser(user2, UserRole.USER, project1); db.users().insertProjectPermissionOnUser(user3, UserRole.USER, project2);
@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)); }