test is failing in ~~~Kotlin - kotlin

#Test
fun `makePartialWithdrawal throws exception on minimum remaining balance violation`() {
val balanceValues = BalanceValues(
Money.fromCents(10000, Money.Currency.EUR),
Money.fromCents(9000, Money.Currency.EUR)
)
doReturn(balanceValues).`when`(avaloqBalanceClient).getBalanceValues(defaultUserId, accountId)
val moneyToWithdrawal = Money.fromCents(9500, Money.Currency.EUR)
val ex = BalanceConditionCheckNotPassedException(defaultUserId, accountId, "failed")
whenever(withdrawalMinBalanceValidator.checkValidity(defaultUserId, accountId, balanceValues, moneyToWithdrawal)).thenThrow(ex)
val request = PartialWithdrawalRequest(moneyToWithdrawal)
assertThrows<BalanceConditionCheckNotPassedException> {
underTest.makePartialWithdrawal(defaultUserId, accountId, request)
}
verify(avaloqBalanceClient).getBalanceValues(defaultUserId, accountId)
}
I want to make this test pass.

Related

Google Places OpeningHours.getWeekdayText() is NULL for some locations

I'm using google places to retrieve information for a place such as the business name, address, OpenHours and LatLng.
This works 95% of the time, but for some places I receive the error...
"Attempt to invoke virtual method 'java.util.List com.google.android.libraries.places.api.model.OpeningHours.getWeekdayText()' on a null object reference"
Looking on Google maps, I do see these places have open hours information.
//Add a marker when a POI on map is clicked.
map.setOnPoiClickListener { poi ->
map.clear()
val poiMarker = map.addMarker(
MarkerOptions()
.position(poi.latLng)
.title(poi.name)
)
poiMarker?.showInfoWindow()
placeId = poi.placeId
Timber.i("Place ID: $placeId")
//https://developers.google.com/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/api/net/PlacesClient#fetchPlace(com.google.android.libraries.places.api.net.FetchPlaceRequest)
val placeFields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.OPENING_HOURS, Place.Field.LAT_LNG)
val request = FetchPlaceRequest.newInstance(placeId, placeFields)
if (!Places.isInitialized()) {
Places.initialize(requireContext(), apiKey, Locale.US);
}
val placesClient = Places.createClient(requireContext())
placesClient.fetchPlace(request)
.addOnSuccessListener { response: FetchPlaceResponse ->
val place = response.place
setLiveDataPlace(place)
}.addOnFailureListener { exception: Exception ->
if (exception is ApiException) {
Timber.i( "Place not found: ${exception.message}")
}
}
binding.buttonSave.visibility = View.VISIBLE
}
}
fun setLiveDataPlace(place: Place){
placeId = place.id as String
placeName = place.name as String
placeAddress = place.address as String
try {
placeOpenMonday = place.openingHours.weekdayText[0]
placeOpenTuesday = place.openingHours.weekdayText[1]
placeOpenWednesday = place.openingHours.weekdayText[2]
placeOpenThursday = place.openingHours.weekdayText[3]
placeOpenFriday = place.openingHours.weekdayText[4]
placeOpenSaturday = place.openingHours.weekdayText[5]
placeOpenSunday = place.openingHours.weekdayText[6]
} catch(e : Exception) {
Timber.i("Open hours exception: ${e.message}")
}
placeLat = place.latLng.latitude.toString()
placeLng = place.latLng.longitude.toString()
Timber.i("Place: $place")
}

I tried to run this code and every time I run it shows this result

private fun moveMarkerAnimation(key: String, newData: AnimationModel, marker: Marker?, from: String, to: String) {
if (!newData.isRun)
{
compositeDisposable.add(
iGoogleAPI.getDirections(
"driving",
"less_driving",
from, to,
getString(R.string.google_api_key1)
)!!.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { returnResult ->
Log.d("API_RETURN",returnResult,)
try {
val jsonObject = JSONObject(returnResult)
val jsonArray = jsonObject.getJSONArray("routes")
for ( i in 0 until jsonArray.length())
{
val route = jsonArray.getJSONObject(i)
val poly = route.getJSONObject("overview_polyLine")
val polyLine = poly.getString("points")
polylineList = Common.decodePoly(polyLine) as java.util.ArrayList<LatLng?>
}
handler = Handler()
index = -1
next = 1
val runnable = object :Runnable{
override fun run() {
if (polylineList!!.size > 1)
{
if (index< polylineList!!.size)
{
index ++
next = index+1
start = polylineList!![index] !!
end = polylineList!![next]!!
}
val valueAnimator = ValueAnimator.ofInt(0,1)
valueAnimator.duration = 3000
valueAnimator.interpolator = LinearInterpolator()
valueAnimator.addUpdateListener { value ->
v = value.animatedFraction
lat = v*end !!.latitude + (1-v) * start!!.latitude
lng = v*end !!.longitude+ (1-v) * start!!.longitude
val newPos = LatLng(lat,lng)
marker!!.position = newPos
marker!!.setAnchor(0.5f,0.5f)
marker!!.rotation = Common.getBearing(start!!,newPos)
}
valueAnimator.start()
if (index < polylineList!!.size-2)
{
handler!!.postDelayed(this,1500)
}else if (index < polylineList!!.size-1)
{
newData.isRun = false
Common.driversSubscrib.put(key,newData)
}
}
}
}
handler!!.postDelayed(runnable,1500)
}
catch (e:java.lang.Exception){
Snackbar.make(requireView(),e.message!!,Snackbar.LENGTH_LONG).show()
}
When the site changes from from firebase this result appears
022-04-26 13:19:30.912 23482-23482/com.example.bustrackerriderapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bustrackerriderapp, PID: 23482
io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading
what should I do to fix this problem

How to combine Flux and Mono

Assume we have the following function
fun getAnnualData(tenant: String): Flux<DashboardResponse> {
val year = LocalDate.now().year
val annualExpenses = expenseFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
val warehouseExpenses = expenseFinder.sumWarehouseByYearAndTenant(year = year, tenant = tenant)
val annualRevenues = revenueFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
return annualExpenses.zipWith(annualRevenues)
.filter { it.t1._id?.year == year }
.filter { it.t2._id?.year == year }
.map {
DashboardResponse(
period = Period.ANNUAL,
expenses = it.t1,
revenue = it.t2
)
}
}
and I want to add the warehouseExpenses to the returning value. How would I do that?
Where
annualExpenses = Flux
warehouseExpenses = Mono
annualRevenue = Flux
You can simply use a flatMap to combine Mono:
Flux.just(...).zipWith(Flux.just(...))
.filter(...)
.flatMap(tuple -> Mono.just(true).map(m -> new DashboardResponse(tuple.getT1(), tuple.getT2(), m)));

Sort List<T> based on String DateTime

I want to sort list descending based on UTC DateTime which is in String form.
My Class
data class CartEntity( val itemId: String, var itemName: String, var createdDate: String)
in this createdDate is "2020-07-28T14:28:52.877Z"
What I have tried
const val UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
Collections.sort(list, object : Comparator<CartEntity> {
var f: DateFormat =
SimpleDateFormat(
AppConstants.UTC_FORMAT, Locale.ENGLISH
)
override fun compare(o1: CartEntity?, o2: CartEntity?): Int {
return try {
val firstitem = f.parse(o1?.createdDate!!)
val seconditem = f.parse(o2?.createdDate!!)
firstitem.compareTo(seconditem)
} catch (e: ParseException) {
throw IllegalArgumentException(e)
}
}
})
But still sort by descending is not working as expected
In kotlin style, you can use the standard library functions (following idioms):
Create a new sorted list out of the list you wanna sort:
fun main() {
val list = listOf<CartEntity>(
CartEntity(itemId = "", itemName = "", createdDate = "2020-07-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-09-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-08-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-04-28T14:28:52.877Z"),
)
val format: DateFormat = SimpleDateFormat(AppConstants.UTC_FORMAT, Locale.ENGLISH)
val sortedList = list.sortedByDescending { format.parse(it.createdDate) }
println(sortedList) // `sortedList` is sorted out list, the `list` is holding the original order
}
Sort the original list (List should be mutable):
fun main() {
val list = mutableListOf<CartEntity>(
CartEntity(itemId = "", itemName = "", createdDate = "2020-07-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-09-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-08-28T14:28:52.877Z"),
CartEntity(itemId = "", itemName = "", createdDate = "2020-04-28T14:28:52.877Z"),
)
val format: DateFormat = SimpleDateFormat(AppConstants.UTC_FORMAT, Locale.ENGLISH)
list.sortByDescending { format.parse(it.createdDate) }
println(list) // `list` is sorted out list
}
This could be done using following approach.
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Tester {
static class Car {
private String date;
String getDate() {
return this.date;
}
void setDate(String date) {
this.date = date;
}
#Override
public String toString() {
return date;
}
// Imagine if you keep date not as strings
ZonedDateTime toDateTime(){
return ZonedDateTime.parse(this.date);
}
}
public static void main(String[] args) {
Car first = new Car();
first.setDate("2020-07-28T14:28:52.877Z");
Car second = new Car();
second.setDate("2010-07-28T14:28:52.877Z");
Car third = new Car();
third.setDate("2021-07-28T14:28:52.877Z");
List<Car> cars = Arrays.asList(first, second, third);
List<Car> sorted = cars.stream().sorted(Comparator.nullsLast(
(current, next) -> {
ZonedDateTime currentDate = ZonedDateTime.parse(current.getDate());
ZonedDateTime nextDate = ZonedDateTime.parse(next.getDate());
return nextDate.compareTo(currentDate);
})).collect(Collectors.toList());
}
}
If you keep the date as regular Date object it will be easier to work with them and the code above could be simplified a bit.
List<Car> sorted = cars.stream()
.sorted(Comparator.comparing(Car::toDateTime, Comparator.nullsLast(Comparator.reverseOrder())))
.collect(Collectors.toList());
Your code works pretty much fine #Charwaka.
data class CartEntity(val itemId: String, var itemName: String, var createdDate: String)
const val UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
fun main() {
val list = mutableListOf<CartEntity>().apply {
add(CartEntity(itemId = "", itemName = "", createdDate = "2020-07-28T14:28:52.877Z"))
add(CartEntity(itemId = "", itemName = "", createdDate = "2020-09-28T14:28:52.877Z"))
add(CartEntity(itemId = "", itemName = "", createdDate = "2020-08-28T14:28:52.877Z"))
add(CartEntity(itemId = "", itemName = "", createdDate = "2020-04-28T14:28:52.877Z"))
}
Collections.sort(list, object : Comparator<CartEntity> {
var f: DateFormat = SimpleDateFormat(UTC_FORMAT, Locale.ENGLISH)
override fun compare(o1: CartEntity, o2: CartEntity): Int {
return try {
val firstitem = f.parse(o1.createdDate)
val seconditem = f.parse(o2.createdDate)
seconditem.compareTo(firstitem)
} catch (e: ParseException) {
throw IllegalArgumentException(e)
}
}
})
list
}
output:
CartEntity(itemId=, itemName=, createdDate=2020-09-28T14:28:52.877Z),
CartEntity(itemId=, itemName=, createdDate=2020-08-28T14:28:52.877Z),
CartEntity(itemId=, itemName=, createdDate=2020-07-28T14:28:52.877Z),
CartEntity(itemId=, itemName=, createdDate=2020-04-28T14:28:52.877Z)
code from #Animesh Sahu also works!
val format: DateFormat = SimpleDateFormat(AppConstants.UTC_FORMAT, Locale.ENGLISH)
list.sortByDescending { format.parse(it.createdDate) }

Get price in-app Billing Library

My apk is uploaded in the alpha channel, my products created, the buy button if it works.
I'm trying to show several products to buy in a RecyclerView. The purchases work for me. What I could not do is show the price and title of the products.
In my myadapter.kt file I have the following var var p = ArrayList<String>() and function:
fun queryskudetails() {
billingClient = BillingClient.newBuilder(context).setListener(this).build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingServiceDisconnected() {
Log.i("Disconnected", "billing client")
}
override fun onBillingSetupFinished(responseCode: Int) {
billingClient.let { billingClient ->
val skulist = ArrayList<String>()
skulist.add("books")
skulist.add("pens")
skulist.add("keychains")
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skulist).setType(BillingClient.SkuType.INAPP)
billingClient.querySkuDetailsAsync(params.build(), { responseCode, skuDetailsList ->
if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
for (skuDetails in skuDetailsList) {
val sku = skuDetails.sku
val price = skuDetails.price
Log.i("skudetails", sku)
Log.i("skuprice", price)
hashMap[sku] = price
println("===== price and sku ======")
println(price)
println(sku)
println("===== /proce and sku ======")
// add price to array p1 (defined as a global variable)
p1.add(price)
}
p = precios
}
})
}
}
})
}
In the section onBindViewHolder which is where I assign the price and title to a textView:
override fun onBindViewHolder(holder: Vholder, position: Int) {
queryskudetails()
print("-----Here array price print [] -----")
println (p)
var text: String = array[position]
Log.i("text", text)
holder.textView.text = text
holder.Price.text = hashMap[text.toLowerCase()].toString() // this does not work for me, retun null
Log.i("price", hashMap["books"].toString())
println(hashMap[array[position]]) // retunr null
holder.btn.setOnClickListener(View.OnClickListener {
Log.i("button", text.toLowerCase())
var skuid = hashMap2[text]
val flowParams = BillingFlowParams.newBuilder()
.setSku(text.toLowerCase())
.setType(BillingClient.SkuType.INAPP)
.build()
val responseCode = billingClient.launchBillingFlow(context as Activity?, flowParams)
})
}
When I show the price in a textview the following code does not work for me:
holder.Price.text = hashMap[text.toLowerCase()].toString() where Price is var Price: TextView = itemView.findViewById(R.id.price)
As a second option I try to use the matrix p1 where I stored all the prices in thequeryskudetails ()function but it returns empty.
How can I do to use the content of the array p1?
The price is stored in the Map: hashMap, to recover it use the sku (identifier in google play console)
hashMap = {sku1=USD 3.99, sku2=USD 1.99, sku3=USD 3.99}
//to recover the values according to the sku (key)
hashMap[sku1] = USD 3.99
hashMap[sku2] = USD 1.99
As I see in your code holder.Price.text = hashMap[text.toLowerCase()].toString() (in the variable Text you must have the identifier (identifier = sku) in order to recover the price of each product ), it is correct, check that in another part is not making conflict or has repeated.