*/
package org.sonar.server.user.ws;
-import com.google.common.base.Function;
-import com.google.common.collect.Lists;
-import javax.annotation.Nonnull;
+import java.util.function.Function;
import org.sonar.api.server.authentication.Display;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.server.authentication.IdentityProviderRepository;
import org.sonarqube.ws.Users;
import org.sonarqube.ws.Users.IdentityProvidersWsResponse;
+import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class IdentityProvidersAction implements UsersWsAction {
private IdentityProvidersWsResponse buildResponse() {
IdentityProvidersWsResponse.Builder response = IdentityProvidersWsResponse.newBuilder();
- response.addAllIdentityProviders(Lists.transform(identityProviderRepository.getAllEnabledAndSorted(), IdentityProviderToWsResponse.INSTANCE));
+ response.addAllIdentityProviders(identityProviderRepository.getAllEnabledAndSorted()
+ .stream()
+ .map(toWsIdentityProvider())
+ .collect(MoreCollectors.toList()));
return response.build();
}
- private enum IdentityProviderToWsResponse implements Function<IdentityProvider, Users.IdentityProvider> {
- INSTANCE;
-
- @Override
- public Users.IdentityProvider apply(@Nonnull IdentityProvider input) {
+ private static Function<IdentityProvider, Users.IdentityProvider> toWsIdentityProvider() {
+ return input -> {
+ Display display = input.getDisplay();
Users.IdentityProvider.Builder builder = Users.IdentityProvider.newBuilder()
.setKey(input.getKey())
- .setName(input.getName());
- Display display = input.getDisplay();
- builder
+ .setName(input.getName())
.setIconPath(display.getIconPath())
.setBackgroundColor(display.getBackgroundColor());
-
+ setNullable(display.getHelpMessage(), builder::setHelpMessage);
return builder.build();
- }
+ };
}
}
"key": "bitbucket",
"name": "Bitbucket",
"iconPath": "/static/authbitbucket/bitbucket.svg",
- "backgroundColor": "#205081"
+ "backgroundColor": "#205081",
+ "helpMessage": "You need an existing account on bitbucket.com"
},
{
"key": "github",
*/
package org.sonar.server.user.ws;
-import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.sonar.test.JsonAssert.assertJson;
public class IdentityProvidersActionTest {
- static IdentityProvider GITHUB = new TestIdentityProvider()
- .setKey("github")
- .setName("Github")
- .setDisplay(Display.builder()
- .setIconPath("/static/authgithub/github.svg")
- .setBackgroundColor("#444444")
- .build())
- .setEnabled(true);
-
- static IdentityProvider BITBUCKET = new TestIdentityProvider()
- .setKey("bitbucket")
- .setName("Bitbucket")
- .setDisplay(Display.builder()
- .setIconPath("/static/authbitbucket/bitbucket.svg")
- .setBackgroundColor("#205081")
- .build())
- .setEnabled(true);
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule()
.addIdentityProvider(GITHUB)
- .addIdentityProvider(BITBUCKET);
+ .addIdentityProvider(BIT_BUCKET);
WsActionTester ws = new WsActionTester(new IdentityProvidersAction(identityProviderRepository));
}
@Test
- public void ws_properties() {
+ public void test_definition() {
WebService.Action webService = ws.getDef();
assertThat(webService.key()).isEqualTo("identity_providers");
assertThat(webService.responseExampleAsString()).isNotEmpty();
assertThat(webService.since()).isEqualTo("5.5");
assertThat(webService.isInternal()).isTrue();
-
}
+
+ private static IdentityProvider GITHUB = new TestIdentityProvider()
+ .setKey("github")
+ .setName("Github")
+ .setDisplay(Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("#444444")
+ .build())
+ .setEnabled(true);
+
+ private static IdentityProvider BIT_BUCKET = new TestIdentityProvider()
+ .setKey("bitbucket")
+ .setName("Bitbucket")
+ .setDisplay(Display.builder()
+ .setIconPath("/static/authbitbucket/bitbucket.svg")
+ .setBackgroundColor("#205081")
+ .setHelpMessage("You need an existing account on bitbucket.com")
+ .build())
+ .setEnabled(true);
}
*/
package org.sonar.api.server.authentication;
+import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import static com.google.common.base.Preconditions.checkArgument;
private final String iconPath;
private final String backgroundColor;
+ private final String helpMessage;
private Display(Builder builder) {
this.iconPath = builder.iconPath;
this.backgroundColor = builder.backgroundColor;
+ this.helpMessage = builder.helpMessage;
}
/**
return backgroundColor;
}
+ /**
+ * Optional help message to be displayed near the provider button.
+ */
+ @CheckForNull
+ public String getHelpMessage() {
+ return helpMessage;
+ }
+
public static Builder builder() {
return new Builder();
}
private String iconPath;
private String backgroundColor = "#236a97";
+ private String helpMessage;
private Builder() {
}
return this;
}
+ /**
+ * @see Display#getHelpMessage()
+ */
+ public Builder setHelpMessage(@CheckForNull String helpMessage) {
+ this.helpMessage = helpMessage;
+ return this;
+ }
+
public Display build() {
checkArgument(isNotBlank(iconPath), "Icon path must not be blank");
validateBackgroundColor();
public ExpectedException thrown = ExpectedException.none();
@Test
- public void create_display() throws Exception {
+ public void create_display() {
Display display = Display.builder()
.setIconPath("/static/authgithub/github.svg")
.setBackgroundColor("#123456")
assertThat(display.getIconPath()).isEqualTo("/static/authgithub/github.svg");
assertThat(display.getBackgroundColor()).isEqualTo("#123456");
+ assertThat(display.getHelpMessage()).isNull();
}
@Test
- public void create_display_with_default_background_color() throws Exception {
+ public void create_display_with_default_background_color() {
Display display = Display.builder()
.setIconPath("/static/authgithub/github.svg")
.build();
assertThat(display.getBackgroundColor()).isEqualTo("#236a97");
}
+
+ @Test
+ public void create_display_with_help_message() {
+ Display display = Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setHelpMessage("Help message")
+ .build();
+
+ assertThat(display.getHelpMessage()).isEqualTo("Help message");
+ }
+
@Test
- public void fail_when_icon_path_is_null() throws Exception {
+ public void fail_when_icon_path_is_null() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Icon path must not be blank");
}
@Test
- public void fail_when_icon_path_is_blank() throws Exception {
+ public void fail_when_icon_path_is_blank() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Icon path must not be blank");
}
@Test
- public void fail_when_background_color_is_null() throws Exception {
+ public void fail_when_background_color_is_null() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Background color must not be blank");
}
@Test
- public void fail_when_background_color_is_blank() throws Exception {
+ public void fail_when_background_color_is_blank() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Background color must not be blank");
}
@Test
- public void fail_when_background_color_has_wrong_size() throws Exception {
+ public void fail_when_background_color_has_wrong_size() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
}
@Test
- public void fail_when_background_color_doesnt_begin_with_sharp() throws Exception {
+ public void fail_when_background_color_doesnt_begin_with_sharp() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
optional string name = 2;
optional string iconPath = 3;
optional string backgroundColor = 4;
+ optional string helpMessage = 5;
}
message CreateWsResponse {