show RewardedAd from object class - kotlin

I need to reduce my app size by collect recurring functions in one place
All functions works fine Except RewardedAd
I want to return a value confirming that ad was shown until end by set variable named adRewardedBoolean to True
Here's my Kotlin code
//Rewarded
private const val adRewardedID = "ca-app-pub-3306064401573277/5337641258"
private var adRewardedBoolean: Boolean = false
private var adRewardedIsLoading = false
private var adRewarded: RewardedAd? = null
private var adRewardAmount: Int = 0
private var adRewardedTimes: Int = 0
//Call loadRewardedAd from any activities
//Ads.loadRewardedAd(this)
fun loadRewardedAd(context: Context): Boolean {
context as Activity
if (adRewarded == null) {
adRewardedIsLoading = true
val adRequest = AdRequest.Builder().build()
RewardedAd.load(context,
adRewardedID,
adRequest,
object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
adRewardedIsLoading = false
adRewarded = null
if (adRewardedTimes == 3) {
adRewardAmount++
adRewardedBoolean = true
log("onAdFailedToLoad")
//checkRewardedAd(adRewardAmount)
} else {
adRewardedTimes++
loadRewardedAd(context)
}
}
override fun onAdLoaded(rewardedAd: RewardedAd) {
adRewarded = rewardedAd
adRewardedIsLoading = false
adRewarded?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
adRewarded = null
adRewardedBoolean = true
log("onAdDismissedFullScreenContent")
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
adRewarded = null
loadRewardedAd(context)
}
override fun onAdShowedFullScreenContent() {
}
}
adRewarded?.show(context) { rewardItem ->
adRewardAmount = rewardItem.amount
}
}
})
}
log(adRewardedBoolean.toString())
return adRewardedBoolean
}

Related

Loading state adapter is always loading

My task is to load recyclerView and show pagination which I both implemented.
I also implemented LoadingState for adapter.
ApiCall:
#GET("top-headlines?sources=bbc-news,techcrunch&apiKey=${BuildConfig.API_KEY}")
suspend fun getTopHeadlinesArticles(
#Query("page") page:Int = 1,
#Query("q") query: String,
) : Response<ArticleListResponse>
I wont show paging because it is working so I will jump to repository:
fun getSearchResult(query: String) =
Pager(
config = PagingConfig(
pageSize = 1,
maxSize = 20,
enablePlaceholders = false
),
pagingSourceFactory = { ArticlePaging(newsService, query)}
).flow
ViewModel:
#OptIn(ExperimentalCoroutinesApi::class)
val news = _currentQuery.flatMapLatest { query ->
articleRepository.getSearchResult(query).cachedIn(viewModelScope)
}
Fragment:
binding.recyclerViewTop.layoutManager = LinearLayoutManager(context)
binding.recyclerViewTop.adapter = adapter.withLoadStateHeaderAndFooter(
header = ArticleLoadStateAdapter { adapter.retry() },
footer = ArticleLoadStateAdapter { adapter.retry() }
)
lifecycleScope.launchWhenCreated {
viewModel.news.collect { articles ->
adapter.submitData(articles)
}
}
And LoadStateViewHolder in LoadStateAdapter:
init {
binding.buttonRetry.setOnClickListener {
retry.invoke()
}
}
fun bind(loadState: LoadState) {
binding.apply {
progressBar.isVisible = loadState is LoadState.Loading
buttonRetry.isVisible = loadState !is LoadState.Loading
textViewError.isVisible = loadState !is LoadState.Loading
}
}
I already predefined DEFAULT_QUERY so I only get 1 Article.
Problem is that progressBar loading is always visible and I have no more articles to show.
Edit: when i have at least 10 items to show this works fine
Edit 2nd: Add ArticlePagingSource if that can help
override fun getRefreshKey(state: PagingState<Int, ArticleResponse>): Int? {
return state.anchorPosition?.let {
val anchorPage = state.closestPageToPosition(it)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
try {
val currentPageList = params.key ?: 1
response = if(query != ""){
newsService.getTopHeadlinesArticles(currentPageList, query)
} else{
newsService.getTopHeadlinesArticles(currentPageList)
}
val responseList = mutableListOf<ArticleResponse>()
val data = response.body()?.articleResponses ?: emptyList()
responseList.addAll(data)
val prevKey = if (currentPageList == 1) null else currentPageList - 1
return LoadResult.Page(
responseList,
prevKey,
currentPageList.plus(1)
)

Data fetch from API not showing on recyclerview MVVM kotlin

I got blocked for this problem, Api will call and get the objects from the response and it will display on the recyclerview. But it is not showing,
fetchProductCategories will do the api call.
prepareProducts will handle the fetched from fetchProductCategories
Fragment:
#AndroidEntryPoint
class ProductsWelcomeFragment : BaseProductsFragment<ProductsWelcomeViewModel, ProductsWelcomeFragmentBinding>() {
private val TAG = "ProductsWelcomeFragment"
#Inject
lateinit var animationQueue: AnimationQueue
override fun getViewModelClass(): KClass<ProductsWelcomeViewModel> = ProductsWelcomeViewModel::class
override fun getContentViewRes(): Int = R.layout.products_welcome_fragment
private val cordovaViewModel: CordovaViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/**
* Initialize only once when everytime accessing products screen.
* */
sharedViewModel.initialSetUpRequest()
viewModel.fetchProductCategories()
}
override fun onBindView() {
with(dataBinding) {
viewModel = this#ProductsWelcomeFragment.viewModel
title = getString(R.string.products_screen_welcome_header)
recyclerViewProducts.configure()
executePendingBindings()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.productCategoriesLiveData.observe(viewLifecycleOwner) {
Log.i(TAG, it.toString())
viewModel.items.observe(viewLifecycleOwner) {
viewModel.prepareProducts()
}
}
viewModel.showProgress.observe(viewLifecycleOwner) {}
viewModel.failedAtRetrievingData.observe(viewLifecycleOwner) {
// showErrorScreen(true)
}
}
private fun RecyclerView.configure() {
with(dataBinding.recyclerViewProducts) {
addItemBindings(SectionHeaderItemViewBinder)
addItemBindings(SpaceItemViewDtoBinder)
addItemBindings(getListButtonItemViewBinder(ProductItem.NormalProduct::dto, ::onProductItemClick))
addItemBindings(getListBigtileItemViewBinder(ProductItem.FeaturedProduct::dto, ::onProductItemClick))
}
}
private fun onProductItemClick(product: ProductItem.NormalProduct) {
when (product.id) {
else -> { // TODO : )}
}
// TODO : implement move to another screen
}
private fun onProductItemClick(product: ProductItem.FeaturedProduct) {
// TODO : implement move to another screen
}
private fun showErrorScreen(isDataRetrievalError: Boolean) {
val dismissAction = ErrorHandlingAction(
getString(R.string.native_done),
null,
null,
ButtonType.PRIMARY
) { dismissDialog ->
dismissDialog.dismiss()
if (isDataRetrievalError) {
findNavController().popBackStack()
}
}
DialogFactory().showBottomDialog(
fragmentManager = parentFragmentManager,
window = requireActivity().window,
title = getString(R.string.native_error_title),
description = getString(R.string.online_identity_something_went_wrong_error_description),
iconId = R.drawable.ic_error_thick_exclamation_icon,
errorHandlingActions = arrayOf(dismissAction),
isHtmlDescription = true
)
}
private fun openCordovaScreen(destination: CordovaPage) {
cordovaViewModel.requestPage(destination)
findNavController().popBackStack(R.id.homeScreenMainFragment, false)
}
}
ViewModel:
#HiltViewModel
class ProductsWelcomeViewModel #Inject constructor(
private val productsRepository: ProductsRepository
) : BaseRequestViewModel(), ViewModelWithItems {
private val TAG = "ProductsWelcomeViewModel"
private val _failedAtRetrievingData = SingleLiveEvent<Boolean>()
val failedAtRetrievingData: LiveData<Boolean> = _failedAtRetrievingData
private val _showProgress = MutableLiveData<Boolean>()
val showProgress: LiveData<Boolean> = _showProgress
private val onProductCategories: SingleLiveEvent<ProductBasketsCategoriesModel?> = SingleLiveEvent()
val productCategoriesLiveData: LiveData<ProductBasketsCategoriesModel?> = onProductCategories
private val _items: MutableLiveData<List<Any>> = MutableLiveData()
override val items: LiveData<List<Any>> = _items
fun fetchProductCategories() {
viewModelScope.launch {
request({ productsRepository.getProductCategories() },
success = { response -> onProductCategories.value = response },
failure = { })
}
}
fun prepareProducts() {
_items.value = mutableListOf<Any>().apply {
productCategoriesLiveData.value?.embedded?.categories?.filter { categories ->
categories.isFeatured == true }?.let { filteredCategories ->
add(HeaderItem(ListSectionHeaderItemViewDto(
text = TextLine(
textRes = if (filteredCategories.isNotEmpty()) {
R.string.products_screen_welcome_header } else { null }
)
)
))
filteredCategories.toItems()?.let { addAll(it) }
}
productCategoriesLiveData.value?.embedded?.categories?.filter { categories ->
categories.isFeatured == false }.let { filteredCategories ->
if (filteredCategories != null) {
add(HeaderItem(ListSectionHeaderItemViewDto(
text = TextLine(
textRes = if (filteredCategories.isNotEmpty()) {
R.string.products_screen_welcome_header } else { null }
)
)
))
}
filteredCategories.toItems()?.let { addAll(it) }
}
}
}
private fun List<CategoriesItemModel>?.toItems(): List<ProductItem>? =
this?.mapIndexed { index, item ->
if (item.isFeatured == true) {
ProductItem.FeaturedProduct(
id = item.id as Any,
name = item.name,
dto = BigTileDto(
title = TextLine(text = item.name),
image = item.id.toString().let { toFeatureIcon(it, item.isFeatured) },
description = TextLine(item.description.toString()),
background = BackgroundType.SINGLE
)
)
} else {
ProductItem.NormalProduct(
id = item.id as Any,
name = item.name,
dto = ListButtonItemViewDto(
firstLine = TextLine(text = item.name),
rightDrawable = R.drawable.ic_arrow_right,
separatorDrawable = R.drawable.list_divider_margin_start_72dp,
background = index.indexToBackgroundType(this.size),
avatarDto = AvatarDto(iconBackgroundColor = R.color.ubs_concrete, avatarSize = AvatarDto.AvatarSize.SMALL_ICON_SIZE, iconId = toFeatureIcon(
item.id, item.isFeatured
)),
)
)
}
}
private fun toFeatureIcon(id: String?, isFeature: Boolean?): Int = if (ProductFeature.verify(id) == true) {
ProductFeature.icon()
} else { if (isFeature == true) { R.drawable.abc_vector_test } else {
R.drawable.balloon_illustration } }
}

implementation of view-viewmodel pattern MVVM

I am new to development and I want to learn how to use the MVVM pattern. I have a problem with the fact that I need to implement the methods that I have in SignUpFragment in the ViewModel class. Please help me understand how to finally do this. I will be very grateful! Thanks in advance
please do not send links to materials, I want to understand my example, thanks
My fragment
class SignUpFragment : Fragment() {
private val viewModel: SignUpViewModel by activityViewModels()
private val binding: FragmentSignUpBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val viewModel:SignUpViewModel = ViewModelProviders.of(this)[SignUpViewModel::class.java]
val binding: FragmentSignUpBinding =
DataBindingUtil.inflate(
inflater, R.layout.fragment_sign_up, container, false
)
return binding.root
}
// call next sign up screen window
fun callNextSignUpScreen(view: View) {
//validate form
if (!(validateFullName() || validateUserName() as Boolean || validateEmail() as Boolean || validatePassword() as Boolean)) {
return
} else {
binding?.signupNextBtn?.setOnClickListener {
findNavController()
.navigate(
R.id.action_signUpFragment_to_signUpSecondFragment
)
}
}
//add transition
val pairs: Array<Pair<*, *>?> = arrayOfNulls(4)
pairs[0] = Pair<View, String>(binding?.signupBackButton, "transition_back_arrow_btn")
pairs[1] = Pair<View, String>(binding?.signupLoginBtn, "transition_login_btn")
pairs[2] = Pair<View, String>(binding?.signupNextBtn, "transition_next_btn")
pairs[3] = Pair<View, String>(binding?.signupTitleText, "transition_title_btn")
}
//validate field full name
private fun validateFullName(): Boolean {
val validName: String = binding!!.signupFullname.editText?.text.toString().trim()
return if (validName.isEmpty()) {
binding.signupFullname.error = "field can not be Empty"
false
} else {
binding.signupFullname.error = null
binding.signupFullname.isErrorEnabled = false
true
}
}
//validate field user name
private fun validateUserName(): Any {
val userName: String = binding!!.signupUserName.editText?.text.toString().trim()
val checksPaces: Regex = "[a-zA-Z0-9._-]+#[a-z]+\\.++[a-z]+".toRegex()
return if (userName.isEmpty()) {
binding.signupUserName.error = "field can not be Empty"
false
} else (if (userName.length > 20) {
binding.signupUserName.error = "Username is too large!"
false
} else if (!userName.matches(checksPaces)) {
binding.signupEmail.error = "Invalid Email"
} else {
binding.signupUserName.error = null
binding.signupUserName.isErrorEnabled = false
true
})
}
//validate field email
private fun validateEmail(): Any {
val email: String = binding!!.signupEmail.editText?.text.toString().trim()
val checkEmail: Regex = "[a-zA-Z0-9._-]+#[a-z]+\\.++[a-z]+".toRegex()
return if (email.isEmpty()) {
binding.signupEmail.error = "field can not be Empty"
false
} else if (!email.matches(checkEmail)) {
binding.signupEmail.error = "Invalid Email"
} else {
binding.signupEmail.error = null
binding.signupEmail.isErrorEnabled = false
true
}
}
//validate field password
private fun validatePassword(): Any {
val password: String = binding!!.signupPassword.editText?.text.toString().trim()
val checkPassword: Regex =
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##\$%^&+=])(?=\\S+\$).{8,}\$".toRegex()
return if (password.isEmpty()) {
binding.signupPassword.error = "Password should contain4 characters!"
false
} else if (!password.matches(checkPassword)) {
binding.signupPassword.error = "Invalid Email"
} else {
binding.signupPassword.error = null
binding.signupPassword.isErrorEnabled = false
true
}
}

Problem saving data into room db from paging library api response

I have an application built using Jetpack Compose , where i also use paging library 3 to fetch data from db , i have multiple remote mediator where i fetch data and save it directly into database , the issue is that sometimes data gets saved , sometimes not , it goes to the point that sometimes one of the two only gets data stored.
Remote Mediator 1:
#ExperimentalPagingApi
class PopularClothingRemoteMediator #Inject constructor(
private val clothingApi: ClothingApi,
private val clothingDatabase: ClothingDatabase
) : RemoteMediator<Int, Clothing>(){
private val clothingDao = clothingDatabase.clothingDao()
private val clothingRemoteKeysDao = clothingDatabase.clothingRemoteKeysDao()
override suspend fun initialize(): InitializeAction {
val currentTime = System.currentTimeMillis()
val lastUpdated = clothingRemoteKeysDao.getRemoteKeys(clothingId = 1)?.lastUpdated ?: 0L
val cacheTimeout = 1440
val diffInMinutes = (currentTime - lastUpdated) / 1000 / 60
return if (diffInMinutes.toInt() <= cacheTimeout) {
// Log.d("RemoteMediator", "UP TO DATE")
InitializeAction.SKIP_INITIAL_REFRESH
} else {
// Log.d("RemoteMediator", "REFRESH")
InitializeAction.LAUNCH_INITIAL_REFRESH
}
}
override suspend fun load(loadType: LoadType, state: PagingState<Int, Clothing>): MediatorResult {
return try {
val page = when (loadType) {
LoadType.REFRESH -> {
val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
remoteKeys?.nextPage?.minus(1) ?: 1
}
LoadType.PREPEND -> {
val remoteKeys = getRemoteKeyForFirstItem(state)
val prevPage = remoteKeys?.prevPage
?: return MediatorResult.Success(
endOfPaginationReached = remoteKeys != null
)
prevPage
}
LoadType.APPEND -> {
val remoteKeys = getRemoteKeyForLastItem(state)
val nextPage = remoteKeys?.nextPage
?: return MediatorResult.Success(
endOfPaginationReached = remoteKeys != null
)
nextPage
}
}
val response = clothingApi.getPopularClothing(page = page)
if (response.popularClothing.isNotEmpty()) {
clothingDatabase.withTransaction {
if (loadType == LoadType.REFRESH) {
clothingDao.deleteAllClothing()
clothingRemoteKeysDao.deleteAllRemoteKeys()
}
val prevPage = response.prevPage
val nextPage = response.nextPage
val keys = response.popularClothing.map { clothing ->
ClothingRemoteKeys(
clothingId = clothing.clothingId,
prevPage = prevPage,
nextPage = nextPage,
lastUpdated = response.lastUpdated
)
}
// When i debug this code , it works fine and the last line is executed
// the issue data sometimes gets saved , sometimes not
clothingRemoteKeysDao.addAllRemoteKeys(clothingRemoteKeys = keys)
clothingDao.addClothing(clothing = response.popularClothing)
}
}
MediatorResult.Success(endOfPaginationReached = response.nextPage == null)
} catch (e: Exception) {
return MediatorResult.Error(e)
}
}
private suspend fun getRemoteKeyClosestToCurrentPosition(
state: PagingState<Int, Clothing>
): ClothingRemoteKeys? {
return state.anchorPosition?.let { position ->
state.closestItemToPosition(position)?.clothingId?.let { clothingId ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothingId)
}
}
}
private suspend fun getRemoteKeyForFirstItem(
state: PagingState<Int, Clothing>
): ClothingRemoteKeys? {
return state.pages.firstOrNull { it.data.isNotEmpty() }?.data?.firstOrNull()
?.let { clothing ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothing.clothingId)
}
}
private suspend fun getRemoteKeyForLastItem(
state: PagingState<Int, Clothing>
): ClothingRemoteKeys? {
return state.pages.lastOrNull { it.data.isNotEmpty() }?.data?.lastOrNull()
?.let { clothing ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothing.clothingId)
}
}
}
Remote Mediator 2:
class OuterwearRemoteMediator #Inject constructor(
private val clothingApi: ClothingApi,
private val clothingDatabase: ClothingDatabase
) : RemoteMediator<Int, Clothing>() {
private val clothingDao = clothingDatabase.clothingDao()
private val clothingRemoteKeysDao = clothingDatabase.clothingRemoteKeysDao()
override suspend fun initialize(): InitializeAction {
val currentTime = System.currentTimeMillis()
val lastUpdated = clothingRemoteKeysDao.getRemoteKeys(clothingId = 1)?.lastUpdated ?: 0L
val cacheTimeout = 1440
val diffInMinutes = (currentTime - lastUpdated) / 1000 / 60
return if (diffInMinutes.toInt() <= cacheTimeout) {
// Log.d("RemoteMediator", "UP TO DATE")
InitializeAction.SKIP_INITIAL_REFRESH
} else {
// Log.d("RemoteMediator", "REFRESH")
InitializeAction.LAUNCH_INITIAL_REFRESH
}
}
override suspend fun load(loadType: LoadType, state: PagingState<Int, Clothing>): MediatorResult {
return try {
val page = when (loadType) {
LoadType.REFRESH -> {
val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
remoteKeys?.nextPage?.minus(1) ?: 1
}
LoadType.PREPEND -> {
val remoteKeys = getRemoteKeyForFirstItem(state)
val prevPage = remoteKeys?.prevPage
?: return MediatorResult.Success(
endOfPaginationReached = remoteKeys != null
)
prevPage
}
LoadType.APPEND -> {
val remoteKeys = getRemoteKeyForLastItem(state)
val nextPage = remoteKeys?.nextPage
?: return MediatorResult.Success(endOfPaginationReached = remoteKeys != null)
nextPage
}
}
val response = clothingApi.getOuterwear(page = page)
if (response.outerwear.isNotEmpty()) {
clothingDatabase.withTransaction {
if (loadType == LoadType.REFRESH) {
clothingDao.deleteAllClothing()
clothingRemoteKeysDao.deleteAllRemoteKeys()
}
val prevPage = response.prevPage
val nextPage = response.nextPage
val keys = response.outerwear.map { clothing ->
ClothingRemoteKeys(
clothingId = clothing.clothingId,
prevPage = prevPage,
nextPage = nextPage,
lastUpdated = response.lastUpdated
)
}
// the same thing here
// When i debug this code , it works fine and the last line is executed
// the issue data sometimes gets saved , sometimes not
clothingRemoteKeysDao.addAllRemoteKeys(clothingRemoteKeys = keys)
clothingDao.addClothing(clothing = response.outerwear)
}
}
MediatorResult.Success(endOfPaginationReached = response.nextPage == null)
} catch (e: Exception) {
return MediatorResult.Error(e)
}
}
private suspend fun getRemoteKeyClosestToCurrentPosition(
state: PagingState<Int, Clothing>): ClothingRemoteKeys? {
return state.anchorPosition?.let { position ->
state.closestItemToPosition(position)?.clothingId?.let { clothingId ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothingId)
}
}
}
private suspend fun getRemoteKeyForFirstItem(
state: PagingState<Int, Clothing>): ClothingRemoteKeys? {
return state.pages.firstOrNull { it.data.isNotEmpty() }?.data?.firstOrNull()
?.let { clothing ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothing.clothingId)
}
}
private suspend fun getRemoteKeyForLastItem(
state: PagingState<Int, Clothing>
): ClothingRemoteKeys? {
return state.pages.lastOrNull { it.data.isNotEmpty() }?.data?.lastOrNull()
?.let { clothing ->
clothingRemoteKeysDao.getRemoteKeys(clothingId = clothing.clothingId)
}
}

Can't mock com.github.kittinunf.fuel.Fuel with mockk

I need your help in the following problem: I can not get mocked the Fuel.get call.
What I've tried:
This is the service class, where Fuel will be called.
class JiraService {
private val logger: Logger = LoggerFactory.getLogger(JiraService::class.java)
fun checkIfUsersExistInJira(usernames: List<String>, jiraConfig: Configuration): Boolean {
var checkResultOk = true;
val encodedCredentials =
usernames.forEach {
Fuel.get("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=${it}&maxResults=1")
.appendHeader("Authorization", "Basic ...")
.appendHeader("Content-Type", "application/json")
.responseObject(UserInfoDeserializer)
.third
.fold(
failure = { throwable ->
logger.error(
"Can't check users in jira for user $it",
throwable
)
},
success = { userExists ->
checkResultOk = checkResultOk && userExists
}
)
}
return checkResultOk
}
}
The test:
#ExtendWith(MockKExtension::class)
class JiraServiceTest {
private val jiraConfig = createJiraConf()
private val fuelRequest = mockk<Request>()
private val fuelResponse = mockk<Response>()
private val fuelMock = mockk<Fuel>()
#BeforeEach
fun setUp() {
mockkStatic(FuelManager::class)
every {
fuelMock.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_1#test.com&maxResults=1"))
.appendHeader(eq("Authorization"), any())
.appendHeader(eq("Content-Type"), eq("application/json"))
.responseObject(UserInfoDeserializer)
} returns ResponseResultOf(first = fuelRequest, second = fuelResponse, third = Result.success(false))
}
#Test
fun `will return FALSE for user not existing in jira`() {
// given
val usernames = listOf("user_1#test.com", "user_3#test.com", "user_4#test.com")
// when
JiraService().checkIfUsersExistInJira(usernames, jiraConfig)
// then
}
}
And I always see the error:
Caused by: some.url.net
com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
com.github.kittinunf.fuel.toolbox.HttpClient.executeRequest(HttpClient.kt:39)
Caused by: java.net.UnknownHostException: some.url.net
...
So it always makes a real Fuel call ignoring fueMock.
What am I doing wrong?
Thanks for help!
If somebody need, following test works:
#ExtendWith(MockKExtension::class)
class JiraServiceTest {
private val jiraConfig = createJiraConf()
private val fuelRequest1 = mockk<DefaultRequest>()
private val fuelRequest3 = mockk<DefaultRequest>()
private val fuelRequest4 = mockk<DefaultRequest>()
private val fuelResponse = mockk<Response>()
#BeforeEach
fun setUp() {
mockkObject(Fuel)
every {
Fuel.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_1#test.com&maxResults=1"))
} returns fuelRequest1
every {
Fuel.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_3#test.com&maxResults=1"))
} returns fuelRequest3
every {
Fuel.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_4#test.com&maxResults=1"))
} returns fuelRequest4
every { fuelRequest1.appendHeader(any<String>(), any()) } returns fuelRequest1
every { fuelRequest3.appendHeader(any<String>(), any()) } returns fuelRequest3
every { fuelRequest4.appendHeader(any<String>(), any()) } returns fuelRequest4
every { fuelRequest1.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
first = fuelRequest1,
second = fuelResponse,
third = Result.success(false)
)
every { fuelRequest3.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
first = fuelRequest3,
second = fuelResponse,
third = Result.success(true)
)
every { fuelRequest4.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
first = fuelRequest4,
second = fuelResponse,
third = Result.success(true)
)
}
#Test
fun `will return FALSE for user not existing in jira`() {
// given
val usernames = listOf("user_1#test.com", "user_3#test.com", "user_4#test.com")
// when
val result = JiraService().checkIfUsersExistInJira(usernames, jiraConfig)
// then
assertThat(result, equalTo(false))
}
#Test
fun `will return TRUE for users existing in jira`() {
// given
val usernames = listOf("user_3#test.com", "user_4#test.com")
// when
val result = JiraService().checkIfUsersExistInJira(usernames, jiraConfig)
// then
assertThat(result, equalTo(true))
}
}