2022-09-20 13:51:55 +02:00
using System ;
2021-11-14 23:27:29 +01:00
using System.Collections.Generic ;
using System.IO ;
using System.Reflection ;
using System.Threading.Tasks ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model ;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock ;
using ShareeBike.Model.Connector ;
using ShareeBike.Repository ;
using ShareeBike.Repository.Request ;
using ShareeBike.Repository.Response ;
using ShareeBike.Repository.Response.Stations ;
namespace SharedBusinessLogic.Tests.Framework.Repository
2021-11-14 23:27:29 +01:00
{
2022-09-06 16:08:19 +02:00
/// <summary> Provides functionality for keeping a set of COPRI responses. </summary>
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 ;
/// <summary>
/// Constructs copri server mock from build in resources.
/// </summary>
/// <param name="bikesAvailableResponseResource"></param>
/// <param name="bikesOccupiedResponseResoure"></param>
/// <param name="authResponseResource"></param>
/// <param name="authOutResponseResource"></param>
/// <param name="stationsResponseResource"></param>
/// <param name="bookingRequestResponseResource"></param>
/// <param name="cancelBookingRequestResponseResource"></param>
2023-04-19 12:14:14 +02:00
/// <param name="sessionCookie">Session cookie.</param>
/// <param name="merchantId">Merchant identifier.</param>
2022-09-06 16:08:19 +02:00
public CopriCallMemoryBase (
string bikesAvailableResponseResource = null ,
string bikesOccupiedResponseResoure = null ,
string authResponseResource = null ,
string authOutResponseResource = null ,
string stationsResponseResource = null ,
string bookingRequestResponseResource = null ,
string cancelBookingRequestResponseResource = null ,
2023-04-19 12:14:14 +02:00
string sessionCookie = null ,
string merchantId = null )
2022-09-06 16:08:19 +02:00
{
string ReadResource ( string resourceName )
{
if ( string . IsNullOrEmpty ( resourceName ) )
return string . Empty ;
using ( var streamReader = new StreamReader ( Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) ) )
{
return streamReader . ReadToEnd ( ) ;
}
}
2024-04-09 12:53:23 +02:00
MerchantId = merchantId ? ? ShareeBikeApp . MerchantId ;
2023-04-19 12:14:14 +02:00
2022-09-06 16:08:19 +02:00
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 )
2023-04-19 12:14:14 +02:00
? new RequestBuilder ( merchantId ? ? MerchantId , null /*UI language */ ) as IRequestBuilder
: new RequestBuilderLoggedIn ( merchantId ? ? MerchantId , null /*UI language */ , sessionCookie ) ;
2022-09-06 16:08:19 +02:00
}
/// <summary> Holds the session id of the logged in user, null otherwise. </summary>
public string SessionCookie { get ; private set ; }
/// <summary> Logs user in. </summary>
/// <param name="p_oUser">User to log in.</param>
/// <param name="deviceId">Id specifying user and hardware.</param>
2023-04-19 12:14:14 +02:00
/// <param name="mailAddress">Mail address of user to log in.</param>
2022-09-06 16:08:19 +02:00
/// <param name="password">Password to log in.</param>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public async Task < AuthorizationResponse > DoAuthorizationAsync (
string mailAddress ,
string password ,
string deviceId )
= > await Task . Run ( ( ) = > DoAuthorize ( AuthResponse , mailAddress , password , deviceId ) ) ;
/// <summary> Logs user out. </summary>
/// <param name="sessionCookie">User to log in.</param>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public async Task < AuthorizationoutResponse > DoAuthoutAsync ( )
= > await Task . Run ( ( ) = > DoAuthout ( AuthOutResponse , SessionCookie ) ) ;
/// <summary>
/// Gets list of bikes from memory.
/// </summary>
2023-11-06 12:23:09 +01:00
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
2023-11-21 15:26:57 +01:00
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
2023-11-21 15:26:57 +01:00
public async Task < BikesAvailableResponse > GetBikesAvailableAsync ( Uri operatorUri = null , string stationId = null , string bikeId = null )
2023-11-06 12:23:09 +01:00
= > await Task . Run ( ( ) = > GetBikesAvailable (
BikesAvailableResponse ,
null /* merchant id*/ ,
SessionCookie ,
2023-11-21 15:26:57 +01:00
operatorUri ,
stationId ,
bikeId ) ) ;
2022-09-06 16:08:19 +02:00
/// <summary>
2023-04-19 12:14:14 +02:00
/// Gets a list of bikes reserved/ booked by active user from Copri.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="p_strSessionCookie">Cookie to authenticate user.</param>
/// <returns>Response holding list of bikes.</returns>
public async Task < BikesReservedOccupiedResponse > GetBikesOccupiedAsync ( )
{
try
{
2023-04-19 12:14:14 +02:00
requestBuilder . GetBikesOccupied ( ) ; // Non mock implementation if ICopriServer call this member as well. To ensure comparable behavior this member is called here as well.
2022-09-06 16:08:19 +02:00
}
catch ( NotSupportedException )
{
// No user logged in.
await Task . CompletedTask ;
return ResponseHelper . GetBikesOccupiedNone ( ) ;
}
return await Task . Run ( ( ) = > GetBikesOccupied ( BikesOccupiedResponse , SessionCookie ) ) ;
}
/// <summary>
/// Get list of stations from file.
/// </summary>
2023-02-22 14:03:35 +01:00
/// <param name="cookie">Auto cookie of user if user is logged in.</param>
2022-09-06 16:08:19 +02:00
/// <returns>List of files.</returns>
public async Task < StationsAvailableResponse > GetStationsAsync ( )
= > await Task . Run ( ( ) = > GetStationsAll ( Stations , null , SessionCookie ) ) ;
/// <summary>
/// Gets booking request response.
/// </summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <returns>Booking response.</returns>
public async Task < ReservationBookingResponse > DoReserveAsync ( string bikeId , Uri operatorUri )
= > await Task . Run ( ( ) = > DoReserve ( BookingRequestResponse , bikeId , SessionCookie ) ) ;
/// <summary>
2023-04-19 12:14:14 +02:00
/// Gets cancel booking request response.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="bikeId">Id of the bike to book.</param>
2023-02-22 14:03:35 +01:00
/// <param name="cookie">Cookie of the logged in user.</param>
2022-09-06 16:08:19 +02:00
/// <returns>Response on cancel booking request.</returns>
2024-04-09 12:53:23 +02:00
public async Task < BookingActionResponse > DoCancelReservationAsync ( string bikeId , Uri operatorUri )
2022-09-06 16:08:19 +02:00
= > await Task . Run ( ( ) = > DoCancelReservation ( CancelBookingRequestResponse , bikeId , SessionCookie ) ) ;
/// <summary> Gets the merchant id.</summary>
2023-04-19 12:14:14 +02:00
public string MerchantId { get ; }
2022-09-06 16:08:19 +02:00
/// <summary> Returns false because cached values are returned. </summary>
public bool IsConnected = > false ;
/// <summary> Logs user in. </summary>
/// <param name="deviceId">Id specifying user and hardware.</param>
2023-04-19 12:14:14 +02:00
/// <param name="mailAddress">Mail address of user to log in.</param>
2022-09-06 16:08:19 +02:00
/// <param name="password">Password to log in.</param>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public static AuthorizationResponse DoAuthorize (
string doAuthResponse ,
string mailAddress ,
string password ,
string deviceId )
{
return mailAddress = = "javaminister@gmail.com"
& & password = = "*********" & &
deviceId = = "HwId1000000000000"
? JsonConvertRethrow . DeserializeObject < ResponseContainer < AuthorizationResponse > > ( doAuthResponse ) . shareejson
: JsonConvertRethrow . DeserializeObject < ResponseContainer < AuthorizationResponse > > ( DO_AUTH_Unknown_User_FILE ) . shareejson ;
}
/// <summary> Logs user in. </summary>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
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 < ResponseContainer < AuthorizationoutResponse > > ( authOutResponse ) . shareejson
: throw new NotSupportedException ( ) ;
}
/// <summary>
/// Gets list of bikes from memory.
/// </summary>
2023-02-22 14:03:35 +01:00
/// <param name="merchantId">Id of the merchant.</param>
2023-11-06 12:23:09 +01:00
/// <param name="sessionCookie">Auto cookie of user if user is logged in.</param>
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
public static BikesAvailableResponse GetBikesAvailable (
string BikesAvailableResponse ,
2023-02-22 14:03:35 +01:00
string merchantId ,
2023-11-06 12:23:09 +01:00
string sessionCookie = null ,
2023-11-21 15:26:57 +01:00
Uri operatorUri = null ,
string stationId = null ,
string bikeid = null )
{
return CopriCallsStatic . DeserializeResponse < BikesAvailableResponse > ( BikesAvailableResponse )
. FilterByStation ( stationId )
. FilterByBike ( bikeid ) ;
}
2022-09-06 16:08:19 +02:00
/// <summary>
/// Gets stations response.
/// </summary>
/// <param name="merchantId">Id of the merchant.</param>
/// <param name="cookie">Auto cookie of user if user is logged in.</param>
/// <param name="p_eSampleSet"></param>
/// <param name="p_lStageIndex"></param>
/// <returns></returns>
public static StationsAvailableResponse GetStationsAll (
string stations ,
string merchantId ,
string cookie = null )
= > JsonConvertRethrow . DeserializeObject < ResponseContainer < StationsAvailableResponse > > ( stations ) . shareejson ;
/// <summary>
/// Gets booking request response.
/// </summary>
/// <param name="bikeId">Id of the bike.</param>
/// <param name="sessionCookie">Identifies the logged in user.</param>
/// <param name="sampleSet">Sample set to use.</param>
/// <param name="stageIndex">Index of the stage.</param>
/// <returns></returns>
public static ReservationBookingResponse DoReserve (
string bookingRequestResponse ,
string bikeId ,
string sessionCookie )
= > JsonConvertRethrow . DeserializeObject < ResponseContainer < ReservationBookingResponse > > ( bookingRequestResponse ) . shareejson ;
/// <summary>
2023-04-19 12:14:14 +02:00
/// Gets cancel booking request response.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <param name="cookie">Cookie of the logged in user.</param>
/// <returns>Response on cancel booking request.</returns>
2024-04-09 12:53:23 +02:00
public static BookingActionResponse DoCancelReservation (
2022-09-06 16:08:19 +02:00
string cancelBookingRequestResponse ,
string bikeId ,
string cookie )
2024-04-09 12:53:23 +02:00
= > JsonConvertRethrow . DeserializeObject < ResponseContainer < BookingActionResponse > > ( cancelBookingRequestResponse ) . shareejson ;
2022-09-06 16:08:19 +02:00
public Task < ReservationBookingResponse > CalculateAuthKeysAsync ( string bikeId , Uri operatorUri )
= > null ;
public Task < ResponseBase > StartReturningBike (
string bikeId ,
Uri operatorUri )
= > null ;
public Task < ReservationBookingResponse > UpdateLockingStateAsync (
string bikeId ,
lock_state state ,
Uri operatorUri ,
LocationDto geolocation ,
2022-09-20 13:51:55 +02:00
double batteryLevel ,
IVersionInfo versionInfo )
2022-09-06 16:08:19 +02:00
= > null ;
2022-12-27 21:08:09 +01:00
public Task < ReservationBookingResponse > DoBookAsync ( Uri operatorUri , string bikeId , Guid guid , double batteryPercentage , LockingAction ? nextAction = null )
2022-09-06 16:08:19 +02:00
= > null ;
public Task < ReservationBookingResponse > BookAvailableAndStartOpeningAsync ( string bikeId , Uri operatorUri )
= > null ;
public Task < ReservationBookingResponse > BookReservedAndStartOpeningAsync ( string bikeId , Uri operatorUri )
= > null ;
public Task < DoReturnResponse > DoReturn (
string bikeId ,
LocationDto geolocation ,
Uri operatorUri )
= > null ;
public Task < DoReturnResponse > ReturnAndStartClosingAsync (
string bikeId ,
Uri operatorUri )
= > null ;
public Task < SubmitFeedbackResponse > DoSubmitFeedback ( string bikeId , int? currentChargeBars , string message , bool isBikeBroken , Uri operatorUri )
= > null ;
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
public Task < ResponseBase > DoSubmitMiniSurvey ( IDictionary < string , string > answers )
= > null ;
/// <summary>
2023-04-19 12:14:14 +02:00
/// Gets a list of bikes reserved/ booked by active user from Copri.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="sessionCookie">Cookie to authenticate user.</param>
/// <param name="SampleSet">Sample set to use.</param>
/// <param name="p_lStageIndex">Index of the stage.</param>
/// <returns>Response holding list of bikes.</returns>
public static BikesReservedOccupiedResponse GetBikesOccupied (
string bikesOccupied ,
string sessionCookie = null )
{
var response = CopriCallsStatic . DeserializeResponse < BikesReservedOccupiedResponse > ( bikesOccupied ) ;
return sessionCookie ! = null & & ( response ? . authcookie ? . Contains ( sessionCookie ) ? ? false )
? response
: ResponseHelper . GetBikesOccupiedNone ( sessionCookie ) ;
}
public const string DO_AUTH_Unknown_User_FILE = @ "
2021-11-14 23:27:29 +01:00
{
"" shareejson "" : {
"" response "" : "" authorization "" ,
"" authcookie "" : 0 ,
"" response_state "" : "" Failure : cannot generate authcookie "" ,
"" apiserver "" : "" https : //tinkwwp.copri-bike.de""
}
} ";
2022-09-06 16:08:19 +02:00
}
2021-11-14 23:27:29 +01:00
}