Convert Files.walk to rxjava Flowable - rx-java2

How to convert
Stream<Path> s = Files.walk(root) to Flowable from rxjava?
Note that we cannot simply use Flowable.fromIterable(()->s.iterator()) because Files.walk throws IOException. Easiest way is to collect results from Files.walk and then create Flowable from iterable collection, but it does not seem to be an idiomatic way.

I think that I have found a nice way to do this. Flowable.using() allows to create Flowable with some "underlying" resource. If Files.walk() fails with IOException, subsriber's onError is called, and after completing "resource" is closed (in this case java.util.stream.Stream with paths).
import io.reactivex.Flowable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
final Flowable<Path> pathFlowable = Flowable.using(
() -> Files.walk(Path.of("/nofile")),
stream -> Flowable.fromIterable(stream::iterator),
Stream::close
);
pathFlowable.subscribe(System.out::println, t -> System.out.println("Error " + t.toString()));

Related

Is there a way to create a "Verify Element Text is Not" keyword?

I need to verify an element had been randomized from a fixed starting state. I already have a test step that verifies the fixed state is working that uses "Verify Element Text" is "inserttexthere".
So I need a way to verify that the text is not "inserttexthere" after I click the randomizing button.
Is there anyway to do this that wouldn't require too much programming knowledge? (I use katalon studio because I have limited tech knowledge)
or is there an if else statement I can use that would pass the step only if the text is different?
Background and other methods I do know of or tried:
I can create another verify text command and just accept it as an intended to fail step. However that's harder to be aware of, especially if the text doesnt change because the test step will pass and I have to remember thats bad.
The other commands that are available only cover if the element no longer has text or if the element is no longer visible/clickable. There is nothing that lets me verify an attribute as "!=" or "NOT"
This is how you need to create a custom keyword in Katalon: Click
Code:
class help_keyword_elemtnotPresent {
#Keyword
def isElemetNotPresent() {
WebUI.verifyElementNotPresent(findTestObject(‘someobject’, timeout, FailureHandling.OPTIONAL)
}
for : if/else
More example
Here's a short beginners' practical tutorial:
Create a keyword in Keywords (1) (right-click, new keyword).
Create a package (2) (right-click, new package) called examplePackage.
Create a new class called ExampleClass inside that package:
public class ExampleClass {
#Keyword
public static verifyElementTextIsNot(String text1, String text2){
assert text1 != text2
}
}
Example test case showing how you can call the above keyword (Keyword is Katalon's name for method):
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import examplePackage.ExampleClass
String someText = "It is a rainy day"
String someOtherText = "It is a sunny day"
ExampleClass.verifyElementTextIsNot(someText, someOtherText)

MultiMap in Flutter

I would like to have a multimap or ListMultiMap in flutter, since I require multiple values with same key, when I use dart's quiver collection as per the dart document, MultiMap showing error, saying no such method. Can anyone help me please.
imported quiver collection and then tried using Multimap class
import 'package:quiver/collection.dart';
https://pub.dev/documentation/quiver/latest/quiver.collection/Multimap-class.html
tried using Multimap as per above documentation, but showing error
Multimap<String, String> multimap = new MultiMap<String,String>();
You might have a typo. Note that you are saying new MultiMap with the M of map capitalized. (Note that the new keyword isn't needed and should be dropped.)
The following works as expected:
import 'package:quiver/collection.dart';
void main() {
var myMap = Multimap<String, String>();
myMap.add('a', 'a1');
myMap.add('a', 'a2');
myMap.forEach((key, value) => print('[$key->$value]'));
}
and prints:
[a->a1]
[a->a2]

How to create a Observable from a Vert.x Future?

I have a instace of io.vertx.core.Future, because I need to set multiples handlers into the same Future, according this issue https://github.com/eclipse/vert.x/issues/1920 the way to go is with Observables.
But I found no way to get a Observable from a Future.
I tried rx.Observable.from( Future ) but it does not work because it is not a Java Future.
I look through RxHelper and there is no toObservable method that takes a Future as parameter.
What am I missing? Any help is appreciated.
You can create an ObservableFuture and use its handler as the handler of your original Future
import io.vertx.core.Future
import io.vertx.rx.java.RxHelper
def myFuture = Future.<String> future()
def obsFut = RxHelper.observableFuture()
myFuture.setHandler(obsFut.toHandler())
obsFut.subscribe({ s ->
println "Hello $s"
})
myFuture.complete("John")
prints
Hello John

Playframework Scala Async controller for JSON request

I am trying to write an async PlayFramework controller that receives a POST request and creates a new object in the database:
def register = Action(BodyParsers.parse.json) { request =>
val businessInfoResult = request.body.validate[BusinessInfo]
businessInfoResult.fold(errors =>{
BadRequest(Json.obj("status"-> "Error", "message"->JsError.toJson(errors))) //Error on this line
}, businessInfo=> {
//save the object
Ok(Json.obj("status" ->"OK", "message" -> ("Place '"+ businessInfo.businessName +"' saved.") )) //Error on this line
})
}
However, it keeps throwing the error below:
reference to Json is ambiguous; it is imported twice in the same scope by import play.libs.Json and import play.mvc.BodyParser.Json AsyncController.scala
The errors are thrown at line 108 and 105 which correspond to lines commented with //Error on this line above (lines with BadRequest(..) and Ok(..))
How do I fix this issue? I can using new JsValue(Map(..)) but was wondering if there's any other way.
Thank you so much for your help.
Rather than Json, you probably want to call play.libs.Json. The problem here is that, considering the imports in your file, you have two objects / classes called Json and the compiler can't choose which one it should use. Calling play.libs.Json, you'll give the compiler enough information.
You could use one or more aliases in your imports:
import play.libs.Json
import play.mvc.BodyParser.{Json => JsonParser}
JsonParser is just an example. You can use anything you like as long as it's unique within the file.
Instead of writing Json (for play.mvc.BodyParser.Json) you could now use the alias JsonParser.
But are you sure you even need to import play.mvc.BodyParser.Json? Because you don't seem to use it.

Call a template expression from another in XTend

i have a question about calling template expression methods from another template expression.
The example below didn't work, ie. it doesn't expand and 'prints' the code on the spot it was called. How can I modify this code to print the result of ResourceGenerator().generate(resource) on the spot where it is called? Note that ResourceGenerator().generate(resource) is in itself a template expression.
class ServerGenerator extends RESTServiceGenerator {
def generate(Server it) '''
package nl.sytematic.projects.RESTServiceServlet;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("«it.baseURI»")
public class «it.name» {
«it.resources.forEach[ resource |new ResourceGenerator().generate(resource)]»
}
'''
}
Hope I am clear in my question. An example would be great! (Again: ResourceGenerator().generate returns a CharSequence).
forEach always returns null and is just about doing side-effects. What you want is a map and a join.
it.resources.map(resource |new ResourceGenerator().generate(resource)).join
But there's an even better way:
«FOR resource : resources»
«new ResourceGenerator().generate(resource)»
«ENDFOR»
I suggest to hold and reuse a single instance of ResourceGenerator as a field (do you use dependency injection?) or make ResourceGenerator::generate static.