|
|
|
|
|
|
|
|
import com.tngtech.java.junit.dataprovider.DataProvider; |
|
|
import com.tngtech.java.junit.dataprovider.DataProvider; |
|
|
import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
|
|
import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
|
|
import com.tngtech.java.junit.dataprovider.UseDataProvider; |
|
|
import com.tngtech.java.junit.dataprovider.UseDataProvider; |
|
|
import java.sql.SQLException; |
|
|
|
|
|
import java.util.Arrays; |
|
|
import java.util.Arrays; |
|
|
import java.util.Iterator; |
|
|
import java.util.Iterator; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
import org.junit.Test; |
|
|
import org.junit.rules.ExpectedException; |
|
|
import org.junit.rules.ExpectedException; |
|
|
import org.junit.runner.RunWith; |
|
|
import org.junit.runner.RunWith; |
|
|
import org.sonar.api.utils.System2; |
|
|
|
|
|
|
|
|
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; |
|
|
import org.sonar.api.web.UserRole; |
|
|
import org.sonar.api.web.UserRole; |
|
|
import org.sonar.db.DbClient; |
|
|
import org.sonar.db.DbClient; |
|
|
import org.sonar.db.DbSession; |
|
|
import org.sonar.db.DbSession; |
|
|
|
|
|
|
|
|
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; |
|
|
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; |
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
import static org.assertj.core.groups.Tuple.tuple; |
|
|
import static org.assertj.core.groups.Tuple.tuple; |
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto; |
|
|
import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto; |
|
|
import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; |
|
|
import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; |
|
|
import static org.sonar.db.property.PropertyTesting.newPropertyDto; |
|
|
import static org.sonar.db.property.PropertyTesting.newPropertyDto; |
|
|
|
|
|
|
|
|
public class PropertiesDaoTest { |
|
|
public class PropertiesDaoTest { |
|
|
private static final String VALUE_SIZE_4000 = String.format("%1$4000.4000s", "*"); |
|
|
private static final String VALUE_SIZE_4000 = String.format("%1$4000.4000s", "*"); |
|
|
private static final String VALUE_SIZE_4001 = VALUE_SIZE_4000 + "P"; |
|
|
private static final String VALUE_SIZE_4001 = VALUE_SIZE_4000 + "P"; |
|
|
private static final long DATE_1 = 1_555_000L; |
|
|
|
|
|
private static final long DATE_2 = 1_666_000L; |
|
|
|
|
|
private static final long DATE_3 = 1_777_000L; |
|
|
|
|
|
private static final long DATE_4 = 1_888_000L; |
|
|
|
|
|
private static final long DATE_5 = 1_999_000L; |
|
|
|
|
|
|
|
|
private static final long INITIAL_DATE = 1_444_000L; |
|
|
|
|
|
|
|
|
private System2 system2 = mock(System2.class); |
|
|
|
|
|
|
|
|
private AlwaysIncreasingSystem2 system2 = new AlwaysIncreasingSystem2(INITIAL_DATE, 1); |
|
|
|
|
|
|
|
|
@Rule |
|
|
@Rule |
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@DataProvider |
|
|
@DataProvider |
|
|
public static Object[][] allValuesForSelect() { |
|
|
public static Object[][] allValuesForSelect() { |
|
|
return new Object[][] { |
|
|
|
|
|
|
|
|
return new Object[][]{ |
|
|
{null, ""}, |
|
|
{null, ""}, |
|
|
{"", ""}, |
|
|
{"", ""}, |
|
|
{"some value", "some value"}, |
|
|
{"some value", "some value"}, |
|
|
|
|
|
|
|
|
insertProperty(key, "value", null, userId); |
|
|
insertProperty(key, "value", null, userId); |
|
|
insertProperty(anotherKey, "value", null, null); |
|
|
insertProperty(anotherKey, "value", null, null); |
|
|
|
|
|
|
|
|
|
|
|
insertProperty("key1", "value", null, null); |
|
|
|
|
|
insertProperty("key2", "value", null, null); |
|
|
|
|
|
insertProperty("key3", "value", null, null); |
|
|
|
|
|
|
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key))) |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key))) |
|
|
.extracting("key") |
|
|
.extracting("key") |
|
|
.containsOnly(key); |
|
|
|
|
|
|
|
|
.containsExactly(key); |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey))) |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey))) |
|
|
.extracting("key") |
|
|
.extracting("key") |
|
|
.containsOnly(key, anotherKey); |
|
|
|
|
|
|
|
|
.containsExactly(key, anotherKey); |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown"))) |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown"))) |
|
|
.extracting("key") |
|
|
.extracting("key") |
|
|
.containsOnly(key, anotherKey); |
|
|
|
|
|
|
|
|
.containsExactly(key, anotherKey); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("key2", "key1", "key3"))) |
|
|
|
|
|
.extracting("key") |
|
|
|
|
|
.containsExactly("key1", "key2", "key3"); |
|
|
|
|
|
|
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("unknown"))) |
|
|
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("unknown"))) |
|
|
.isEmpty(); |
|
|
.isEmpty(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); |
|
|
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); |
|
|
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId(), project2.getId()))) |
|
|
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId(), project2.getId()))) |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
tuple(key, project.getId()), |
|
|
|
|
|
tuple(key, project2.getId()), |
|
|
|
|
|
tuple(anotherKey, project2.getId())); |
|
|
|
|
|
|
|
|
tuple(key, project.getId()), |
|
|
|
|
|
tuple(key, project2.getId()), |
|
|
|
|
|
tuple(anotherKey, project2.getId())); |
|
|
|
|
|
|
|
|
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty(); |
|
|
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); |
|
|
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId())); |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId(), project2.getId()))) |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId(), project2.getId()))) |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
tuple(key, project.getId()), |
|
|
|
|
|
tuple(key, project2.getId())); |
|
|
|
|
|
|
|
|
tuple(key, project.getId()), |
|
|
|
|
|
tuple(key, project2.getId())); |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key, anotherKey), newHashSet(project.getId(), project2.getId()))) |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key, anotherKey), newHashSet(project.getId(), project2.getId()))) |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
.extracting("key", "resourceId").containsOnly( |
|
|
tuple(key, project.getId()), |
|
|
|
|
|
tuple(key, project2.getId()), |
|
|
|
|
|
tuple(anotherKey, project2.getId())); |
|
|
|
|
|
|
|
|
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("unknown"), newHashSet(project.getId()))).isEmpty(); |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("key"), newHashSet(123456789L))).isEmpty(); |
|
|
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("key"), newHashSet(123456789L))).isEmpty(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void saveProperty_inserts_global_properties_when_they_do_not_exist_in_db() { |
|
|
public void saveProperty_inserts_global_properties_when_they_do_not_exist_in_db() { |
|
|
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5); |
|
|
|
|
|
|
|
|
|
|
|
underTest.saveProperty(new PropertyDto().setKey("global.null").setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global.null").setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global.empty").setValue("")); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global.empty").setValue("")); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global.text").setValue("some text")); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global.text").setValue("some text")); |
|
|
|
|
|
|
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
assertThatPropertiesRow("global.empty") |
|
|
assertThatPropertiesRow("global.empty") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_2); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
assertThatPropertiesRow("global.text") |
|
|
assertThatPropertiesRow("global.text") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("some text") |
|
|
.hasTextValue("some text") |
|
|
.hasCreatedAt(DATE_3); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 4); |
|
|
assertThatPropertiesRow("global.4000") |
|
|
assertThatPropertiesRow("global.4000") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 5); |
|
|
assertThatPropertiesRow("global.clob") |
|
|
assertThatPropertiesRow("global.clob") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasCreatedAt(DATE_5); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 6); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void saveProperty_inserts_component_properties_when_they_do_not_exist_in_db() { |
|
|
public void saveProperty_inserts_component_properties_when_they_do_not_exist_in_db() { |
|
|
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5); |
|
|
|
|
|
|
|
|
|
|
|
long resourceId = 12; |
|
|
long resourceId = 12; |
|
|
underTest.saveProperty(new PropertyDto().setKey("component.null").setResourceId(resourceId).setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("component.null").setResourceId(resourceId).setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("component.empty").setResourceId(resourceId).setValue("")); |
|
|
underTest.saveProperty(new PropertyDto().setKey("component.empty").setResourceId(resourceId).setValue("")); |
|
|
|
|
|
|
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
assertThatPropertiesRow("component.empty") |
|
|
assertThatPropertiesRow("component.empty") |
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_2); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
assertThatPropertiesRow("component.text") |
|
|
assertThatPropertiesRow("component.text") |
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("some text") |
|
|
.hasTextValue("some text") |
|
|
.hasCreatedAt(DATE_3); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 4); |
|
|
assertThatPropertiesRow("component.4000") |
|
|
assertThatPropertiesRow("component.4000") |
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 5); |
|
|
assertThatPropertiesRow("component.clob") |
|
|
assertThatPropertiesRow("component.clob") |
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasCreatedAt(DATE_5); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 6); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void saveProperty_inserts_user_properties_when_they_do_not_exist_in_db() { |
|
|
public void saveProperty_inserts_user_properties_when_they_do_not_exist_in_db() { |
|
|
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5); |
|
|
|
|
|
|
|
|
|
|
|
int userId = 100; |
|
|
int userId = 100; |
|
|
underTest.saveProperty(new PropertyDto().setKey("user.null").setUserId(userId).setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("user.null").setUserId(userId).setValue(null)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("user.empty").setUserId(userId).setValue("")); |
|
|
underTest.saveProperty(new PropertyDto().setKey("user.empty").setUserId(userId).setValue("")); |
|
|
|
|
|
|
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
assertThatPropertiesRow("user.empty") |
|
|
assertThatPropertiesRow("user.empty") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_2); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
assertThatPropertiesRow("user.text") |
|
|
assertThatPropertiesRow("user.text") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.hasTextValue("some text") |
|
|
.hasTextValue("some text") |
|
|
.hasCreatedAt(DATE_3); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 4); |
|
|
assertThatPropertiesRow("user.4000") |
|
|
assertThatPropertiesRow("user.4000") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 5); |
|
|
assertThatPropertiesRow("user.clob") |
|
|
assertThatPropertiesRow("user.clob") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasCreatedAt(DATE_5); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 6); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@UseDataProvider("valueUpdatesDataProvider") |
|
|
@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); |
|
|
|
|
|
|
|
|
public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { |
|
|
|
|
|
long id = insertProperty("global", oldValue, null, null); |
|
|
|
|
|
|
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
propertiesRowAssert.isEmpty(); |
|
|
propertiesRowAssert.isEmpty(); |
|
|
} else if (newValue.length() > 4000) { |
|
|
} else if (newValue.length() > 4000) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@UseDataProvider("valueUpdatesDataProvider") |
|
|
@UseDataProvider("valueUpdatesDataProvider") |
|
|
public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { |
|
|
|
|
|
|
|
|
public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { |
|
|
long resourceId = 999L; |
|
|
long resourceId = 999L; |
|
|
long id = insertProperty("global", oldValue, resourceId, null, DATE_1); |
|
|
|
|
|
when(system2.now()).thenReturn(DATE_4); |
|
|
|
|
|
|
|
|
long id = insertProperty("global", oldValue, resourceId, null); |
|
|
|
|
|
|
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setResourceId(resourceId).setValue(newValue)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setResourceId(resourceId).setValue(newValue)); |
|
|
|
|
|
|
|
|
assertThatPropertiesRow(id) |
|
|
assertThatPropertiesRow(id) |
|
|
.doesNotExist(); |
|
|
.doesNotExist(); |
|
|
|
|
|
|
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
.hasResourceId(resourceId) |
|
|
.hasResourceId(resourceId) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
propertiesRowAssert.isEmpty(); |
|
|
propertiesRowAssert.isEmpty(); |
|
|
} else if (newValue.length() > 4000) { |
|
|
} else if (newValue.length() > 4000) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@UseDataProvider("valueUpdatesDataProvider") |
|
|
@UseDataProvider("valueUpdatesDataProvider") |
|
|
public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { |
|
|
|
|
|
|
|
|
public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { |
|
|
int userId = 90; |
|
|
int userId = 90; |
|
|
long id = insertProperty("global", oldValue, null, userId, DATE_1); |
|
|
|
|
|
when(system2.now()).thenReturn(DATE_4); |
|
|
|
|
|
|
|
|
long id = insertProperty("global", oldValue, null, userId); |
|
|
|
|
|
|
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue)); |
|
|
underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(userId) |
|
|
.hasUserId(userId) |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
if (newValue == null || newValue.isEmpty()) { |
|
|
propertiesRowAssert.isEmpty(); |
|
|
propertiesRowAssert.isEmpty(); |
|
|
} else if (newValue.length() > 4000) { |
|
|
} else if (newValue.length() > 4000) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@DataProvider |
|
|
@DataProvider |
|
|
public static Object[][] valueUpdatesDataProvider() { |
|
|
public static Object[][] valueUpdatesDataProvider() { |
|
|
return new Object[][] { |
|
|
|
|
|
|
|
|
return new Object[][]{ |
|
|
{null, null}, |
|
|
{null, null}, |
|
|
{null, ""}, |
|
|
{null, ""}, |
|
|
{null, "some value"}, |
|
|
{null, "some value"}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void saveGlobalProperties_insert_property_if_does_not_exist_in_db() { |
|
|
public void saveGlobalProperties_insert_property_if_does_not_exist_in_db() { |
|
|
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5); |
|
|
|
|
|
|
|
|
|
|
|
underTest.saveGlobalProperties(mapOf( |
|
|
underTest.saveGlobalProperties(mapOf( |
|
|
"null_value_property", null, |
|
|
"null_value_property", null, |
|
|
"empty_value_property", "", |
|
|
"empty_value_property", "", |
|
|
|
|
|
|
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
assertThatPropertiesRow("empty_value_property") |
|
|
assertThatPropertiesRow("empty_value_property") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.isEmpty() |
|
|
.isEmpty() |
|
|
.hasCreatedAt(DATE_2); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
assertThatPropertiesRow("text_value_property") |
|
|
assertThatPropertiesRow("text_value_property") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("dfdsfsd") |
|
|
.hasTextValue("dfdsfsd") |
|
|
.hasCreatedAt(DATE_3); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 4); |
|
|
assertThatPropertiesRow("4000_char_value_property") |
|
|
assertThatPropertiesRow("4000_char_value_property") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasTextValue(VALUE_SIZE_4000) |
|
|
.hasCreatedAt(DATE_4); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 5); |
|
|
assertThatPropertiesRow("clob_value_property") |
|
|
assertThatPropertiesRow("clob_value_property") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasClobValue(VALUE_SIZE_4001) |
|
|
.hasCreatedAt(DATE_5); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 6); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@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); |
|
|
|
|
|
|
|
|
public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() { |
|
|
|
|
|
long id = insertProperty("to_be_updated", "old_value", null, null); |
|
|
|
|
|
|
|
|
underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value")); |
|
|
underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("new value") |
|
|
.hasTextValue("new value") |
|
|
.hasCreatedAt(DATE_3); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Map<String, String> mapOf(String... values) { |
|
|
private static Map<String, String> mapOf(String... values) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void renamePropertyKey_updates_global_component_and_user_properties() throws SQLException { |
|
|
|
|
|
long id1 = insertProperty("foo", "bar", null, null, DATE_1); |
|
|
|
|
|
long id2 = insertProperty("old_name", "doc1", null, null, DATE_1); |
|
|
|
|
|
long id3 = insertProperty("old_name", "doc2", 15L, null, DATE_1); |
|
|
|
|
|
long id4 = insertProperty("old_name", "doc3", 16L, null, DATE_1); |
|
|
|
|
|
long id5 = insertProperty("old_name", "doc4", null, 100, DATE_1); |
|
|
|
|
|
long id6 = insertProperty("old_name", "doc5", null, 101, DATE_1); |
|
|
|
|
|
|
|
|
public void renamePropertyKey_updates_global_component_and_user_properties() { |
|
|
|
|
|
long id1 = insertProperty("foo", "bar", null, null); |
|
|
|
|
|
long id2 = insertProperty("old_name", "doc1", null, null); |
|
|
|
|
|
long id3 = insertProperty("old_name", "doc2", 15L, null); |
|
|
|
|
|
long id4 = insertProperty("old_name", "doc3", 16L, null); |
|
|
|
|
|
long id5 = insertProperty("old_name", "doc4", null, 100); |
|
|
|
|
|
long id6 = insertProperty("old_name", "doc5", null, 101); |
|
|
|
|
|
|
|
|
underTest.renamePropertyKey("old_name", "new_name"); |
|
|
underTest.renamePropertyKey("old_name", "new_name"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasTextValue("bar") |
|
|
.hasTextValue("bar") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
assertThatPropertiesRow(id2) |
|
|
assertThatPropertiesRow(id2) |
|
|
.hasKey("new_name") |
|
|
.hasKey("new_name") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("doc1") |
|
|
.hasTextValue("doc1") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 3); |
|
|
assertThatPropertiesRow(id3) |
|
|
assertThatPropertiesRow(id3) |
|
|
.hasKey("new_name") |
|
|
.hasKey("new_name") |
|
|
.hasResourceId(15) |
|
|
.hasResourceId(15) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("doc2") |
|
|
.hasTextValue("doc2") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 4); |
|
|
assertThatPropertiesRow(id4) |
|
|
assertThatPropertiesRow(id4) |
|
|
.hasKey("new_name") |
|
|
.hasKey("new_name") |
|
|
.hasResourceId(16) |
|
|
.hasResourceId(16) |
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasTextValue("doc3") |
|
|
.hasTextValue("doc3") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 5); |
|
|
assertThatPropertiesRow(id5) |
|
|
assertThatPropertiesRow(id5) |
|
|
.hasKey("new_name") |
|
|
.hasKey("new_name") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(100) |
|
|
.hasUserId(100) |
|
|
.hasTextValue("doc4") |
|
|
.hasTextValue("doc4") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 6); |
|
|
assertThatPropertiesRow(id6) |
|
|
assertThatPropertiesRow(id6) |
|
|
.hasKey("new_name") |
|
|
.hasKey("new_name") |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasUserId(101) |
|
|
.hasUserId(101) |
|
|
.hasTextValue("doc5") |
|
|
.hasTextValue("doc5") |
|
|
.hasCreatedAt(DATE_1); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 7); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void rename_to_same_key_has_no_effect() throws SQLException { |
|
|
|
|
|
long now = 1_890_999L; |
|
|
|
|
|
long id = insertProperty("foo", "bar", null, null, now); |
|
|
|
|
|
|
|
|
public void rename_to_same_key_has_no_effect() { |
|
|
|
|
|
long id = insertProperty("foo", "bar", null, null); |
|
|
|
|
|
|
|
|
assertThatPropertiesRow(id) |
|
|
assertThatPropertiesRow(id) |
|
|
.hasCreatedAt(now); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
|
|
|
|
|
|
underTest.renamePropertyKey("foo", "foo"); |
|
|
underTest.renamePropertyKey("foo", "foo"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.hasNoUserId() |
|
|
.hasNoUserId() |
|
|
.hasNoResourceId() |
|
|
.hasNoResourceId() |
|
|
.hasTextValue("bar") |
|
|
.hasTextValue("bar") |
|
|
.hasCreatedAt(now); |
|
|
|
|
|
|
|
|
.hasCreatedAt(INITIAL_DATE + 2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
|
|
|
|
|
|
session.commit(); |
|
|
session.commit(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) { |
|
|
private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) { |
|
|
PropertyDto dto = new PropertyDto().setKey(key) |
|
|
PropertyDto dto = new PropertyDto().setKey(key) |
|
|
.setResourceId(resourceId) |
|
|
.setResourceId(resourceId) |