using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using TINK.Model;
using TINK.Model.Device;
using TINK.Repository;
using TINK.Repository.Request;
using TINK.Repository.Response;
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.
///
///
///
///
///
///
///
///
///
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 ReadResource(string resourceName)
{
if (string.IsNullOrEmpty(resourceName))
return string.Empty;
using (var streamReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
{
return streamReader.ReadToEnd();
}
}
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) as IRequestBuilder
: new RequestBuilderLoggedIn(MerchantId, 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.
/// Mailaddress 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.
///
///
public async Task GetBikesAvailableAsync()
=> await Task.Run(() => GetBikesAvailable(BikesAvailableResponse, null, SessionCookie));
///
/// Gets a list of bikes reserved/ booked by acctive 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 behaviour 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 canel 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 => TinkApp.MerchantId;
/// Returns false because cached values are returned.
public bool IsConnected => false;
/// Logs user in.
/// Id specifying user and hardware.
/// Mailaddress 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.
/// Set of samples.
/// Index of the stage.
///
public static BikesAvailableResponse GetBikesAvailable(
string BikesAvailableResponse,
string p_strMerchantId,
string p_strSessionCookie = 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 canel 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)
=> null;
public Task DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
=> null;
public Task BookAndStartOpeningAsync(string bikeId, Uri operatorUri)
=> null;
public Task DoReturn(
string bikeId,
LocationDto geolocation,
ISmartDevice smartDevice,
Uri operatorUri)
=> null;
public Task ReturnAndStartClosingAsync(
string bikeId,
ISmartDevice smartDevice,
Uri operatorUri)
=> null;
public Task DoSubmitFeedback(string bikeId, 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 acctive 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""
}
}";
}
}