using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; using TINK.Model; using TINK.Model.Bikes.BikeInfoNS.BluetoothLock; using TINK.Model.Connector; using TINK.Repository; using TINK.Repository.Request; using TINK.Repository.Response; using TINK.Repository.Response.Stations; namespace TestFramework.Repository { /// Provides functionality for keeping a set of COPRI responses. public abstract class CopriCallMemoryBase { private string BikesAvailableResponse { get; } private string BikesOccupiedResponse { get; } private string AuthResponse { get; } private string AuthOutResponse { get; } private string Stations { get; } private string BookingRequestResponse { get; } private string CancelBookingRequestResponse { get; } private IRequestBuilder requestBuilder; /// /// Constructs copri server mock from build in resources. /// /// /// /// /// /// /// /// /// Session cookie. /// Merchant identifier. public CopriCallMemoryBase( string bikesAvailableResponseResource = null, string bikesOccupiedResponseResoure = null, string authResponseResource = null, string authOutResponseResource = null, string stationsResponseResource = null, string bookingRequestResponseResource = null, string cancelBookingRequestResponseResource = null, string sessionCookie = null, string merchantId = null) { string ReadResource(string resourceName) { if (string.IsNullOrEmpty(resourceName)) return string.Empty; using (var streamReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))) { return streamReader.ReadToEnd(); } } MerchantId = merchantId ?? TinkApp.MerchantId; SessionCookie = sessionCookie; BikesAvailableResponse = ReadResource(bikesAvailableResponseResource); BikesOccupiedResponse = ReadResource(bikesOccupiedResponseResoure); AuthResponse = ReadResource(authResponseResource); AuthOutResponse = ReadResource(authOutResponseResource); Stations = ReadResource(stationsResponseResource); BookingRequestResponse = ReadResource(bookingRequestResponseResource); CancelBookingRequestResponse = ReadResource(cancelBookingRequestResponseResource); requestBuilder = string.IsNullOrEmpty(sessionCookie) ? new RequestBuilder(merchantId ?? MerchantId, null /*UI language */) as IRequestBuilder : new RequestBuilderLoggedIn(merchantId ?? MerchantId, null /*UI language */, sessionCookie); } /// Holds the session id of the logged in user, null otherwise. public string SessionCookie { get; private set; } /// Logs user in. /// User to log in. /// Id specifying user and hardware. /// Mail address of user to log in. /// Password to log in. /// Response which holds auth cookie public async Task DoAuthorizationAsync( string mailAddress, string password, string deviceId) => await Task.Run(() => DoAuthorize(AuthResponse, mailAddress, password, deviceId)); /// Logs user out. /// User to log in. /// Response which holds auth cookie public async Task DoAuthoutAsync() => await Task.Run(() => DoAuthout(AuthOutResponse, SessionCookie)); /// /// Gets list of bikes from memory. /// /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// public async Task GetBikesAvailableAsync(Uri operatorUri = null) => await Task.Run(() => GetBikesAvailable( BikesAvailableResponse, null /* merchant id*/, SessionCookie, operatorUri)); /// /// Gets a list of bikes reserved/ booked by active user from Copri. /// /// Cookie to authenticate user. /// Response holding list of bikes. public async Task GetBikesOccupiedAsync() { try { requestBuilder.GetBikesOccupied(); // Non mock implementation if ICopriServer call this member as well. To ensure comparable behavior this member is called here as well. } catch (NotSupportedException) { // No user logged in. await Task.CompletedTask; return ResponseHelper.GetBikesOccupiedNone(); } return await Task.Run(() => GetBikesOccupied(BikesOccupiedResponse, SessionCookie)); } /// /// Get list of stations from file. /// /// Auto cookie of user if user is logged in. /// List of files. public async Task GetStationsAsync() => await Task.Run(() => GetStationsAll(Stations, null, SessionCookie)); /// /// Gets booking request response. /// /// Id of the bike to book. /// Booking response. public async Task DoReserveAsync(string bikeId, Uri operatorUri) => await Task.Run(() => DoReserve(BookingRequestResponse, bikeId, SessionCookie)); /// /// Gets cancel booking request response. /// /// Id of the bike to book. /// Cookie of the logged in user. /// Response on cancel booking request. public async Task DoCancelReservationAsync(string bikeId, Uri operatorUri) => await Task.Run(() => DoCancelReservation(CancelBookingRequestResponse, bikeId, SessionCookie)); /// Gets the merchant id. public string MerchantId { get; } /// Returns false because cached values are returned. public bool IsConnected => false; /// Logs user in. /// Id specifying user and hardware. /// Mail address of user to log in. /// Password to log in. /// Response which holds auth cookie public static AuthorizationResponse DoAuthorize( string doAuthResponse, string mailAddress, string password, string deviceId) { return mailAddress == "javaminister@gmail.com" && password == "*********" && deviceId == "HwId1000000000000" ? JsonConvertRethrow.DeserializeObject>(doAuthResponse).shareejson : JsonConvertRethrow.DeserializeObject>(DO_AUTH_Unknown_User_FILE).shareejson; } /// Logs user in. /// Response which holds auth cookie public static AuthorizationoutResponse DoAuthout( string authOutResponse, string sessionCookie) { // Response contains auth cookie of user "JavaministerHardwareNr1" // For this reason do not return answer if mail and pwd do not match. return !string.IsNullOrEmpty(sessionCookie) ? JsonConvertRethrow.DeserializeObject>(authOutResponse).shareejson : throw new NotSupportedException(); } /// /// Gets list of bikes from memory. /// /// Id of the merchant. /// Auto cookie of user if user is logged in. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// public static BikesAvailableResponse GetBikesAvailable( string BikesAvailableResponse, string merchantId, string sessionCookie = null, Uri operatorUri = null) => CopriCallsStatic.DeserializeResponse(BikesAvailableResponse); /// /// Gets stations response. /// /// Id of the merchant. /// Auto cookie of user if user is logged in. /// /// /// public static StationsAvailableResponse GetStationsAll( string stations, string merchantId, string cookie = null) => JsonConvertRethrow.DeserializeObject>(stations).shareejson; /// /// Gets booking request response. /// /// Id of the bike. /// Identifies the logged in user. /// Sample set to use. /// Index of the stage. /// public static ReservationBookingResponse DoReserve( string bookingRequestResponse, string bikeId, string sessionCookie) => JsonConvertRethrow.DeserializeObject>(bookingRequestResponse).shareejson; /// /// Gets cancel booking request response. /// /// Id of the bike to book. /// Cookie of the logged in user. /// Response on cancel booking request. public static ReservationCancelReturnResponse DoCancelReservation( string cancelBookingRequestResponse, string bikeId, string cookie) => JsonConvertRethrow.DeserializeObject>(cancelBookingRequestResponse).shareejson; public Task CalculateAuthKeysAsync(string bikeId, Uri operatorUri) => null; public Task StartReturningBike( string bikeId, Uri operatorUri) => null; public Task UpdateLockingStateAsync( string bikeId, lock_state state, Uri operatorUri, LocationDto geolocation, double batteryLevel, IVersionInfo versionInfo) => null; public Task DoBookAsync(Uri operatorUri, string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null) => null; public Task BookAvailableAndStartOpeningAsync(string bikeId, Uri operatorUri) => null; public Task BookReservedAndStartOpeningAsync(string bikeId, Uri operatorUri) => null; public Task DoReturn( string bikeId, LocationDto geolocation, Uri operatorUri) => null; public Task ReturnAndStartClosingAsync( string bikeId, Uri operatorUri) => null; public Task DoSubmitFeedback(string bikeId, int? currentChargeBars, string message, bool isBikeBroken, Uri operatorUri) => null; /// Submits mini survey to copri server. /// Collection of answers. public Task DoSubmitMiniSurvey(IDictionary answers) => null; /// /// Gets a list of bikes reserved/ booked by active user from Copri. /// /// Cookie to authenticate user. /// Sample set to use. /// Index of the stage. /// Response holding list of bikes. public static BikesReservedOccupiedResponse GetBikesOccupied( string bikesOccupied, string sessionCookie = null) { var response = CopriCallsStatic.DeserializeResponse(bikesOccupied); return sessionCookie != null && (response?.authcookie?.Contains(sessionCookie) ?? false) ? response : ResponseHelper.GetBikesOccupiedNone(sessionCookie); } public const string DO_AUTH_Unknown_User_FILE = @" { ""shareejson"" : { ""response"" : ""authorization"", ""authcookie"" : 0, ""response_state"" : ""Failure: cannot generate authcookie"", ""apiserver"" : ""https://tinkwwp.copri-bike.de"" } }"; } }