Skip to content

Commit

Permalink
rh-che redhat-developer#1042: Return blank tenant data instead of thr…
Browse files Browse the repository at this point in the history
…owing an exception in namespace of type 'user' is missing (this might happen only if there is a bug on init-tenant or 'api/user/services' side)

Signed-off-by: Ilya Buziuk <ibuziuk@redhat.com>
  • Loading branch information
ibuziuk committed Dec 11, 2019
1 parent 12c081e commit 0509b80
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ class TenantDataCacheLoader extends CacheLoader<TenantDataCacheKey, UserCheTenan
}

@Override
public UserCheTenantData load(TenantDataCacheKey cacheKey) throws InfrastructureException {
String responseBody;
public UserCheTenantData load(final TenantDataCacheKey cacheKey) throws InfrastructureException {
final String responseBody;
try {
responseBody = getResponseBody(fabric8UserServiceEndpoint, cacheKey.getKeycloakToken());
} catch (ApiException | IOException e) {
LOG.error(e.getMessage(), e);
throw new InfrastructureException("Exception during the user tenant data retrieval", e);
}
final String namespace = cacheKey.getNamespaceType();
final UserServicesJsonResponse userServicesData;
try {
final Gson gson = new Gson();
final UserServicesJsonResponse userServicesData =
gson.fromJson(responseBody, UserServicesJsonResponse.class);
userServicesData = gson.fromJson(responseBody, UserServicesJsonResponse.class);
List<Namespace> namespaces = userServicesData.getNamespaces();
for (Namespace ns : namespaces) {
if (cacheKey.getNamespaceType().equals(ns.getType())) {
if (namespace.equals(ns.getType())) {
UserCheTenantData cheTenantData =
new UserCheTenantData(
ns.getName(),
Expand All @@ -79,6 +80,13 @@ public UserCheTenantData load(TenantDataCacheKey cacheKey) throws Infrastructure
throw new InfrastructureException(
"Invalid response from Fabric8 user services:" + responseBody, e);
}
if ("user".equals(namespace)) {
// This happens only if there is a bug on the '/api/user/services' or init tenant side
// Let's log the error and return blank data instead of throwing an exception and failing the
// workspace startup
LOG.error("No namespace with type 'user' was found in the user tenant: {}", responseBody);
return new UserCheTenantData("", "", "", false);
}
throw new InfrastructureException(
format(
"No namespace with type '%s' was found in the user tenant",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class TenantDataCacheLoaderTest {
private static final String ENDPOINT = "test-endpoint";
private static final String CLUSTER_URL = "test-url";
private static final String ROUTE_PREFIX = "test-route-prefix";
private static final String NAMESPACE_TYPE = "che";
private static final String NAMESPACE_TYPE_CHE = "che";
private static final String NAMESPACE_TYPE_USER = "user";
private static final String BAD_NAMESPACE_TYPE = "not-che";

@Mock private HttpJsonRequestFactory httpJsonRequestFactory;
Expand All @@ -62,7 +63,7 @@ public void setUp() throws Exception {
public void shouldParseJsonAndReturnTenantData() throws Exception {
// Given
String jsonResponse =
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, NAMESPACE_TYPE, false);
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, NAMESPACE_TYPE_CHE, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", "che");

Expand Down Expand Up @@ -96,7 +97,7 @@ public void shouldThrowInfrastructureExceptionWhenNoNamespaceMatchesCacheKey() t
String jsonResponse =
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, BAD_NAMESPACE_TYPE, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", NAMESPACE_TYPE);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", NAMESPACE_TYPE_CHE);

// When
cacheLoader.load(cacheKey);
Expand All @@ -107,7 +108,7 @@ public void shouldThrowInfrastructureExceptionWhenNoNamespaceMatchesCacheKey() t
expectedExceptionsMessageRegExp = "Invalid response.*")
public void shouldThrowInfrastructureExceptionWhenResponseFailsToValidate() throws Exception {
// Given
String jsonResponse = generateBadResponse(ROUTE_PREFIX, NAMESPACE, NAMESPACE_TYPE, false);
String jsonResponse = generateBadResponse(ROUTE_PREFIX, NAMESPACE, NAMESPACE_TYPE_CHE, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", "che");

Expand All @@ -127,6 +128,56 @@ public void shouldThrowInfrastructureExceptionWhenResponseParsesToNull() throws
cacheLoader.load(cacheKey);
}

@Test
public void shouldReturnValidTenantDataIfUserNamespaceExists() throws Exception {
// Given
String jsonResponse =
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, NAMESPACE_TYPE_USER, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", "user");

// When
UserCheTenantData data = cacheLoader.load(cacheKey);

// Then
assertEquals(data.getRouteBaseSuffix(), ROUTE_PREFIX);
assertEquals(data.getClusterUrl(), CLUSTER_URL);
assertEquals(data.getNamespace(), NAMESPACE);
assertEquals(data.isClusterCapacityExhausted(), false);
}

@Test
public void shouldNotThrowExceptionWhenUserNamespaceIsMissing() throws InfrastructureException {
// Given
String jsonResponse =
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, NAMESPACE_TYPE_CHE, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", "user");

// When
UserCheTenantData data = cacheLoader.load(cacheKey);

// Then expect blank data
assertEquals(data.getRouteBaseSuffix(), "");
assertEquals(data.getClusterUrl(), "");
assertEquals(data.getNamespace(), "");
assertEquals(data.isClusterCapacityExhausted(), false);
}

@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp = "No namespace with type '" + BAD_NAMESPACE_TYPE + "'.*")
public void shouldThrowInfrastructureExceptionWhenNamespaceDoesNotExist() throws Exception {
// Given
String jsonResponse =
generateResponse(ROUTE_PREFIX, CLUSTER_URL, NAMESPACE, NAMESPACE_TYPE_CHE, false);
when(httpJsonResponse.asString()).thenReturn(jsonResponse);
TenantDataCacheKey cacheKey = new TenantDataCacheKey("token", BAD_NAMESPACE_TYPE);

// When
cacheLoader.load(cacheKey);
}

private String generateResponse(
String routePrefix,
String clusterUrl,
Expand Down

0 comments on commit 0509b80

Please sign in to comment.