Skip to content

Commit

Permalink
More test and docs
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Kabashniuk <skabashniuk@redhat.com>
  • Loading branch information
skabashnyuk committed Jan 20, 2021
1 parent 4699856 commit 42a26d9
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.che.api.factory.server.bitbucket.server;

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Objects;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
Expand Down Expand Up @@ -125,4 +126,23 @@ public String toString() {
+ permissions
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BitbucketPersonalAccessToken that = (BitbucketPersonalAccessToken) o;
return id == that.id
&& createdDate == that.createdDate
&& lastAuthenticated == that.lastAuthenticated
&& Objects.equals(name, that.name)
&& Objects.equals(token, that.token)
&& Objects.equals(user, that.user)
&& Objects.equals(permissions, that.permissions);
}

@Override
public int hashCode() {
return Objects.hash(id, createdDate, lastAuthenticated, name, token, user, permissions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Objects;

public class Page<T> {
private int start;
Expand Down Expand Up @@ -90,4 +91,22 @@ public String toString() {
+ values
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Page<?> page = (Page<?>) o;
return start == page.start
&& size == page.size
&& limit == page.limit
&& isLastPage == page.isLastPage
&& nextPageStart == page.nextPageStart
&& Objects.equals(values, page.values);
}

@Override
public int hashCode() {
return Objects.hash(start, size, limit, isLastPage, nextPageStart, values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class BitbucketServerApiProvider implements Provider<BitbucketServerApi>
@Inject
public BitbucketServerApiProvider(
@Nullable @Named("che.integration.bitbucket.server_endpoints") String bitbucketEndpoints,
@Nullable @Named("che.oauth1.bitbucket.endpoint") String bitbucketEndpoint,
@Nullable @Named("che.oauth1.bitbucket.endpoint") String bitbucketOauth1Endpoint,
Set<OAuthAuthenticator> authenticators) {
bitbucketServerApi = doGet(bitbucketEndpoints, bitbucketEndpoint, authenticators);
LOG.info("bitbucketServerApi={}", bitbucketServerApi);
bitbucketServerApi = doGet(bitbucketEndpoints, bitbucketOauth1Endpoint, authenticators);
LOG.debug("Bitbucket server api is used {}", bitbucketServerApi);
}

@Override
Expand All @@ -46,39 +46,35 @@ public BitbucketServerApi get() {
}

private static BitbucketServerApi doGet(
String bitbucketEndpoints, String bitbucketEndpoint, Set<OAuthAuthenticator> authenticators) {
LOG.info("bitbucketEndpoint={}", bitbucketEndpoint);
if (isNullOrEmpty(bitbucketEndpoint)) {
String bitbucketEndpoints,
String bitbucketOauth1Endpoint,
Set<OAuthAuthenticator> authenticators) {
if (isNullOrEmpty(bitbucketOauth1Endpoint)) {
return new NopBitbucketServerApi();
} else {
LOG.info("bitbucketEndpoint={}", bitbucketEndpoints);
if (isNullOrEmpty(bitbucketEndpoints)) {
throw new RuntimeException(
"`che.integration.bitbucket.server_endpoints` bitbucket configuration is missing.");
"`che.integration.bitbucket.server_endpoints` bitbucket configuration is missing."
+ " It should contain values from 'che.oauth1.bitbucket.endpoint'");
} else {
LOG.info(
"bitbucketEndpoints.contains(bitbucketEndpoint)={}",
bitbucketEndpoints.contains(bitbucketEndpoint));
LOG.info("authenticators={}", authenticators);
if (bitbucketEndpoints.contains(bitbucketEndpoint)) {
if (bitbucketEndpoints.contains(bitbucketOauth1Endpoint)) {
Optional<OAuthAuthenticator> authenticator =
authenticators
.stream()
.filter(a -> a.getOAuthProvider().equals("bitbucket-server"))
.findFirst();
LOG.info("authenticator={}", authenticators);
if (authenticator.isEmpty()) {
throw new RuntimeException(
"BitbucketServerOAuthAuthenticator is not configured correctly");
"'che.oauth1.bitbucket.endpoint' is set but BitbucketServerOAuthAuthenticator deployed correctly");
}
return new BitbucketServerApiHttpClient(
bitbucketEndpoint,
bitbucketOauth1Endpoint,
new BitbucketServerOAuth1AuthorizationHeaderProvider(
(BitbucketServerOAuthAuthenticator) authenticator.get()));
} else {
throw new RuntimeException(
"`che.integration.bitbucket.server_endpoints` mast contain `"
+ bitbucketEndpoint
+ bitbucketOauth1Endpoint
+ "` value");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.security.oauth1;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApi;
import org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApiHttpClient;
import org.eclipse.che.api.factory.server.bitbucket.server.NopBitbucketServerApi;
import org.eclipse.che.commons.annotation.Nullable;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class BitbucketServerApiProviderTest {
BitbucketServerOAuthAuthenticator oAuthAuthenticator;

@BeforeClass
public void setUp() {
oAuthAuthenticator =
new BitbucketServerOAuthAuthenticator(
"df", "private", " https://bitbucket2.server.com", " https://che.server.com");
}

@Test
public void shouldBeAbleToCreateBitbucketServerApi() {
// given
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(
"https://bitbucket.server.com, https://bitbucket2.server.com",
"https://bitbucket.server.com",
ImmutableSet.of(oAuthAuthenticator));
// when
BitbucketServerApi actual = bitbucketServerApiProvider.get();
// then
assertNotNull(actual);
assertTrue(BitbucketServerApiHttpClient.class.isAssignableFrom(actual.getClass()));
}

@Test(dataProvider = "noopConfig")
public void shouldProvideNoopOAuthAuthenticatorIfSomeConfigurationIsNotSet(
@Nullable String bitbucketEndpoints,
@Nullable String bitbucketOauth1Endpoint,
Set<OAuthAuthenticator> authenticators)
throws IOException {
// given
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(bitbucketEndpoints, bitbucketOauth1Endpoint, authenticators);
// when
BitbucketServerApi actual = bitbucketServerApiProvider.get();
// then
assertNotNull(actual);
assertTrue(NopBitbucketServerApi.class.isAssignableFrom(actual.getClass()));
}

@Test(
expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp =
"`che.integration.bitbucket.server_endpoints` bitbucket configuration is missing. It should contain values from 'che.oauth1.bitbucket.endpoint'")
public void shouldFailToBuildIfEndpointsAreMisconfigured() {
// given
// when
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(
"", "https://bitbucket.server.com", ImmutableSet.of(oAuthAuthenticator));
}

@Test(
expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp =
"'che.oauth1.bitbucket.endpoint' is set but BitbucketServerOAuthAuthenticator deployed correctly")
public void shouldFailToBuildIfEndpointsAreMisconfigured2() {
// given
// when
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(
"https://bitbucket.server.com, https://bitbucket2.server.com",
"https://bitbucket.server.com",
Collections.emptySet());
}

@Test(
expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp =
"`che.integration.bitbucket.server_endpoints` mast contain `https://bitbucket.server.com` value")
public void shouldFailToBuildIfEndpointsAreMisconfigured3() {
// given
// when
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(
"https://bitbucket3.server.com, https://bitbucket2.server.com",
"https://bitbucket.server.com",
ImmutableSet.of(oAuthAuthenticator));
}

@DataProvider(name = "noopConfig")
public Object[][] noopConfig() {
return new Object[][] {
{null, null, null},
{"https://bitbucket.server.com, https://bitbucket2.server.com", null, null},
{
"https://bitbucket.server.com, https://bitbucket2.server.com",
null,
ImmutableSet.of(oAuthAuthenticator)
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</encoder>
</appender>

<root level="DEBUG">
<root level="INFO">
<appender-ref ref="stdout"/>
</root>

Expand Down

0 comments on commit 42a26d9

Please sign in to comment.