Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Wrote unit tests for RetryableProcessor
Browse files Browse the repository at this point in the history
Signed-off-by: Aashir Siddiqui <aashir_sidiki@hotmail.com>
  • Loading branch information
aashir21 committed Sep 12, 2024
1 parent bf1a570 commit 151bf7d
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<IInternalAuthToken> getTokensByLoginId(String loginId) throws AuthSt

// Build up a list of all the tokens using the document IDs
for (ViewRow row : tokenDocuments) {
tokens.add(getAuthTokenFromDocument(row.key));
tokens.add(getAuthTokenFromDocument(row.id));
}

logger.info("Tokens retrieved from CouchDB OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.net.URI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
Expand All @@ -22,9 +21,13 @@

import com.google.gson.JsonSyntaxException;


import dev.galasa.extensions.common.api.LogFactory;

import dev.galasa.extensions.common.couchdb.CouchdbBaseValidator;
import dev.galasa.extensions.common.couchdb.CouchdbClashingUpdateException;
import dev.galasa.extensions.common.couchdb.CouchdbException;
import dev.galasa.extensions.common.couchdb.RetryableCouchdbUpdateOperationProcessor;
import dev.galasa.auth.couchdb.internal.beans.*;
import dev.galasa.extensions.common.api.HttpRequestFactory;
import dev.galasa.framework.spi.utils.GalasaGson;
Expand All @@ -34,12 +37,27 @@

public class CouchdbAuthStoreValidator extends CouchdbBaseValidator {

private final Log logger = LogFactory.getLog(getClass());
private final Log logger ;
private final GalasaGson gson = new GalasaGson();
private final LogFactory logFactory;

// A couchDB view, it gets all the access tokens of a the user based on the loginId provided.
public static final String DB_TABLE_TOKENS_DESIGN = "function (doc) { if (doc.owner && doc.owner.loginId) {emit(doc.owner.loginId, doc); } }";

public CouchdbAuthStoreValidator() {
this(new LogFactory(){
@Override
public Log getLog(Class<?> clazz) {
return org.apache.commons.logging.LogFactory.getLog(clazz);
}
});
}

public CouchdbAuthStoreValidator(LogFactory logFactory) {
this.logFactory = logFactory;
this.logger = logFactory.getLog(getClass());
}

@Override
public void checkCouchdbDatabaseIsValid(
URI couchdbUri,
Expand All @@ -51,7 +69,7 @@ public void checkCouchdbDatabaseIsValid(
// Perform the base CouchDB checks
super.checkCouchdbDatabaseIsValid(couchdbUri, httpClient, httpRequestFactory, timeService);

RetryableCouchdbUpdateOperationProcessor retryProcessor = new RetryableCouchdbUpdateOperationProcessor(timeService);
RetryableCouchdbUpdateOperationProcessor retryProcessor = new RetryableCouchdbUpdateOperationProcessor(timeService, this.logFactory);

retryProcessor.retryCouchDbUpdateOperation(
()->{ tryToCheckAndUpdateCouchDBTokenView(couchdbUri, httpClient, httpRequestFactory);
Expand Down Expand Up @@ -170,13 +188,15 @@ private void updateTokenDesignDocument(CloseableHttpClient httpClient, URI couch
try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();

EntityUtils.consumeQuietly(response.getEntity());

if (statusCode == HttpStatus.SC_CONFLICT) {
// Someone possibly updated the document while we were thinking about it.
// It was probably another instance of this exact code.
throw new CouchdbClashingUpdateException(ERROR_FAILED_TO_UPDATE_COUCHDB_DESING_DOC_CONFLICT.toString());
}

EntityUtils.consumeQuietly(response.getEntity());
if (statusCode != HttpStatus.SC_CREATED) {

throw new CouchdbException(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testGetTokensReturnsTokensWithFailingRequestReturnsError() throws Ex
MockLogFactory logFactory = new MockLogFactory();

List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs?include_docs=true&endkey=%22_%22", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));

MockCloseableHttpClient mockHttpClient = new MockCloseableHttpClient(interactions);

Expand Down Expand Up @@ -137,7 +137,7 @@ public void testGetTokensReturnsTokensFromCouchdbOK() throws Exception {

CouchdbAuthToken mockToken = new CouchdbAuthToken("token1", "dex-client", "my test token", Instant.now(), new CouchdbUser("johndoe", "dex-user-id"));
List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs?include_docs=true&endkey=%22_%22", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetTokenDocumentInteraction<CouchdbAuthToken>("https://my-auth-store/galasa_tokens/token1", HttpStatus.SC_OK, mockToken));

MockCloseableHttpClient mockHttpClient = new MockCloseableHttpClient(interactions);
Expand All @@ -164,7 +164,7 @@ public void testGetTokensReturnsTokensByLoginIdFromCouchdbOK() throws Exception
MockLogFactory logFactory = new MockLogFactory();

ViewRow tokenDoc = new ViewRow();
tokenDoc.key = "token1";
tokenDoc.id = "token1";
List<ViewRow> mockDocs = List.of(tokenDoc);

ViewResponse mockAllDocsResponse = new ViewResponse();
Expand All @@ -173,7 +173,7 @@ public void testGetTokensReturnsTokensByLoginIdFromCouchdbOK() throws Exception
CouchdbAuthToken mockToken = new CouchdbAuthToken("token1", "dex-client", "my test token", Instant.now(), new CouchdbUser("johndoe", "dex-user-id"));
CouchdbAuthToken mockToken2 = new CouchdbAuthToken("token2", "dex-client", "my test token", Instant.now(), new CouchdbUser("notJohnDoe", "dex-user-id"));
List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_design/docs/_view/loginId-view?key=johndoe", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_design/docs/_view/loginId-view?key=%22johndoe%22", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetTokenDocumentInteraction<CouchdbAuthToken>("https://my-auth-store/galasa_tokens/token1", HttpStatus.SC_OK, mockToken));
interactions.add(new GetTokenDocumentInteraction<CouchdbAuthToken>("https://my-auth-store/galasa_tokens/token1", HttpStatus.SC_OK, mockToken2));

Expand All @@ -183,7 +183,6 @@ public void testGetTokensReturnsTokensByLoginIdFromCouchdbOK() throws Exception
MockTimeService mockTimeService = new MockTimeService(Instant.now());

CouchdbAuthStore authStore = new CouchdbAuthStore(authStoreUri, httpClientFactory, new HttpRequestFactoryImpl(), logFactory, new MockCouchdbValidator(), mockTimeService);

// When...
List<IInternalAuthToken> tokens = authStore.getTokensByLoginId("johndoe");

Expand All @@ -201,7 +200,7 @@ public void testGetTokensReturnsTokensByLoginIdWithFailingRequestReturnsError()
MockLogFactory logFactory = new MockLogFactory();

List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_design/docs/_view/loginId-view?key=johndoe", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_design/docs/_view/loginId-view?key=%22johndoe%22", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));

MockCloseableHttpClient mockHttpClient = new MockCloseableHttpClient(interactions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
implementation ('org.apache.httpcomponents:httpclient-osgi:4.5.13')
implementation ('org.apache.httpcomponents:httpcore-osgi:4.4.14')
implementation ('com.google.code.gson:gson:2.10.1')

testImplementation(project(':dev.galasa.extensions.mocks'))
}

// Note: These values are consumed by the parent build process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum Errors {
ERROR_GALASA_REST_CALL_TO_GET_CPS_NAMESPACES_FAILED (7020,"GAL7020E: Could not get the CPS namespaces information from URL ''{0}''. Cause: {1}"),
ERROR_GALASA_REST_CALL_TO_GET_CPS_NAMESPACES_BAD_JSON_RETURNED (7021,"GAL7021E: Could not get the CPS namespaces value from URL ''{0}''. Cause: Bad json returned from the server. {1}"),

ERROR_GALASA_COUCHDB_UPDATED_FAILED_AFTER_RETRIES (7022,"GAL7022E: Couchdb operation failed after {0} attempts, due to conflicts."),
;

private String template;
Expand Down
Loading

0 comments on commit 151bf7d

Please sign in to comment.