I'm writing a JUnit test that check messages (one message per line) inside one unified String. The format is as follows:
[* Message for Alice *]
Hey, first message
Second message
[* Message for Jim *]
Holler
Are you there?
[* General Messages *]
Welcome everyone!
This is yet another message.
The problem is that the actual string's order that I receive may change (except for the General Messages that always comes at the end of the string). For example: one time I can get Jim's messages first, so when I try to use assertEquals() the test fails. Unfortunately I don't have access to the code that generate the messages, so I can't make any modifications.
What is the best way to compare these strings and validate that they're the same?
You should re-organize your tests to address arbitrary re-ordering, for example, like this:
Split the string into individual messages
Separate general messages and all other messages
Order expected and actual messages in the same order (e.g. alphabetical)
Compare ordered lists of expected and actual messages. Now that they are ordered the same, they should equal item-by-item
Check that the general messages come after all other messages in the actual message stream.
You'd better compare Sets of messages, as the fuzzy string comparison you're after is going to be too tricky...
Related
i am looking to find an example of Java-diff-utils finding the difference between two strings e.g.
String s1 = "{\"type\": \"message_typing\",\"requestId\": \"requestid\",\"clientMutationId\": \"mutationid\",\"chatRoomId\": \"roomid\",\"conversationId\": \"conversationid\"}";
String s2 = "{\"type\": \"type_2\",\"requestId\": \"1\",\"clientMutationId\": \"2\",\"chatRoomId\": \"dev/test\",\"conversationId\": \"aa2344ceDsea1\"}";
so the first is the base message the second is the one i would like to compare against the base message and get the different values (e.g. type_2,1,2,dev/test,aa2344ceDsea1) however i would like to be able to reassemble the string correctly if given only the base message and the values of the diffs.
I can only find examples using two files online and no examples using two strings if anyone could give me a code example that would be very helpful. I have already did it using google-diff-patch-match however the returned diffs are too large for what i need. as i will be sending the diffs over MQTT in order to keep payload size down, so i need something that can extract the values but still be able to reassemble on the other side when given the values and base message.
I have a loop in my code which goes over a set of strings. Said strings are then passed along to several other functions.
In my tests, I'm basically emulating the flow of code and asserting at the start of each method I expect it to visit, if the string is correct.
But I can only write the test method once. Meaning that I have to do the following to catch all the different strings:
assertTrue(string.equals("test1") || string.equals("test2") || string.equals("test3") || ...);
However, there's a problem with this if one or more of those strings are not successfully passed to the list that is looped over. Since this is a chain of OR statements, it will succeed as long as there is 1 correct string, regardless of whether any of the other strings are missing. Which is a problem.
I can't emulate the loop, I can only emulate the functions receiving the data each time.
Is there a way to deal with this problem?
EDIT: some clarification.
I start out with a list of strings.
This list gets looped over, meaning every single string instance will go through a bunch of functions. And then the next string. And so on.
In the test, I can write dummies for the methods the string goes through. This means I override the behavior of the actual code and send my own custom return. This has to be correct though, since the function following that has to properly process what I just send to it.
But, when I start the test with the dummy data, it will do the loop, meaning the same function gets called multiple times, each time with a different string. I can't just do 1 test for one of the strings, because the next loop will fail on the next string.
I have a class that analyzes and interprets GPS strings. The incoming strings are formatted in a very specific way (15 comma separated strings/values). Right now, if I receive a string that is not expected (like if it doesn't have 14 commas), I am throwing an IllegalArgumentException. This is for the cases when the incoming string is truly broken and this event should rarely, if ever happen.
However, if my GPS unit has not connected to the satellite yet, I will receive blank values between the commas. How can I return something that tells the user "The GPS isn't turned on yet," without returning Null? In my particular case, returning Null is something that is already needed. I thought of returning some insane value that can be checked against, but this seems like really sloppy program design to me. What would a good method for a situation like this be?
I have requirement to split a input string in to output string/s (with some order)
by applying a set of regexs on the input string. I thinking to implement
this functionality with cluster of akka actors in such a way I scatter the
regex and input string and gather the string. However I would like to know
while collecting back the processed string, how to keep track of the order. Iam not
certain if "Scatter-Gather" is the correct approach , if there is any other that
can be suited please suggest.
I guess you will have to provide hints to the gatherer on how to assemble the string in order. You don't mention how the order is established: whether the initial split can define the order or whether the regex processing will define it.
In both cases, you need to keep track of 3 things:
- the initial source,
- the order of each individual piece
- the total amount of pieces
You could use a message like:
case class StringSegment(id:String, total:Int, seqNr:Int, payload:String)
The scatterer produces StringSegments based on the input:
def scatter(s:String):List[StringSegment] ...
scatter(input).foreach(msg => processingActor ! msg)
and the gatherer will assemble them together, using the seqNr to know the order and total to know when all the pieces are present.
I am having problems implementing the Crash Consensus Algorithm.
Here is some pseudo code. Can someone please give me a an explanation on what it means.
Crash-Consensus-Receive-Message(m)
if m is proposal message then
▷add the received values to set of all known proposed values
add[knownValues,values[m]]
note that sender[m]has not crashed during this round
else ▷ordinary message received
process message m
Crash-Consensus-Reach-Consensus()
knownValues←{ownValue} ▷start with just own proposed value
sentValues←0 ▷no values sent yet
for i←0 to f do ▷f+1 rounds of multicasts
▷determine which known values have not yet been sent
newValues←knownValues -ssentValues
multicast proposal message with newValues to alive processes
wait until next round
use pre-agreed strategy with knownValues to get consensus value
What does it mean by sender[m] and values[m] as the m means message here? Also what does it mean by knownValues.
In the most of programming languages the square brackets mean an array of values, in your case an array of knowed messages or an array of messeges to send. I'm not sure if this is also true in pseudo code but I think maybe can help you in all case.