import org.sonarqube.ws.Permissions.PermissionTemplate;
import org.sonarqube.ws.Permissions.UpdateTemplateWsResponse;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isBlank;
private PermissionTemplateDto getAndBuildTemplateToUpdate(DbSession dbSession, String uuid, @Nullable String newName, @Nullable String newDescription,
@Nullable String newProjectKeyPattern) {
PermissionTemplateDto templateToUpdate = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(uuid, null));
- templateToUpdate.setName(firstNonNull(newName, templateToUpdate.getName()));
- templateToUpdate.setDescription(firstNonNull(newDescription, templateToUpdate.getDescription()));
- templateToUpdate.setKeyPattern(firstNonNull(newProjectKeyPattern, templateToUpdate.getKeyPattern()));
+ templateToUpdate.setName(coalesce(newName, templateToUpdate.getName()));
+ templateToUpdate.setDescription(coalesce(newDescription, templateToUpdate.getDescription()));
+ templateToUpdate.setKeyPattern(coalesce(newProjectKeyPattern, templateToUpdate.getKeyPattern()));
templateToUpdate.setUpdatedAt(new Date(system.now()));
return templateToUpdate;
}
+ @CheckForNull
+ private static String coalesce(@Nullable String s1, @Nullable String s2) {
+ return s1 != null ? s1 : s2;
+ }
+
private PermissionTemplateDto updateTemplate(DbSession dbSession, PermissionTemplateDto templateToUpdate) {
return dbClient.permissionTemplateDao().update(dbSession, templateToUpdate);
}
assertThat(reloaded.getKeyPattern()).isEqualTo(template.getKeyPattern());
}
+ @Test
+ public void update_with_null_values() {
+ template = db.getDbClient().permissionTemplateDao().insert(db.getSession(), newPermissionTemplateDto()
+ .setName("Test")
+ .setDescription(null)
+ .setKeyPattern(null)
+ .setCreatedAt(new Date(1_000_000_000_000L))
+ .setUpdatedAt(new Date(1_000_000_000_000L)));
+ db.commit();
+
+ loginAsAdmin();
+
+ call(template.getUuid(), template.getName(), null, null);
+
+ PermissionTemplateDto reloaded = db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid());
+ assertThat(reloaded.getName()).isEqualTo(template.getName());
+ assertThat(reloaded.getDescription()).isNull();
+ assertThat(reloaded.getKeyPattern()).isNull();
+ }
+
@Test
public void update_name_only() {
loginAsAdmin();