Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random SMSPeer generator. #49

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

ricdezen
Copy link

@ricdezen ricdezen commented Feb 10, 2020

Premise

This is not a class to be used at runtime, it's only useful for testing with consistent quantities of SMSPeers.
Due to special validity criteria of very few countries (Tavana, Norfolk Island, TA (?)) which I am still investigating, the class iterates until an appropriate number is generated.
Any other regions never returned invalid addresses after the first iteration, as long as I could tell.

Features

An SMSPeer generator that can generate numbers for any region, but is limited to 1000 for each, because it simply takes the example number from libphonenumber and changes the last three digits, in order not to mess up the national validity of the number.
I don't think there is any better logic for generating phone numbers without implementing the appropriate standards for every single country. If you do, please share.

Changes

  • Added RandomPeerGenerator as an interface.
  • Added RandomSMSPeerGenerator as an implementing class for SMSPeer.

Side Changes

  • Added SMSPeer.getDefaultRegion() because why in heavens would you want to just blindly set something.

I kept the classes in the main scope to avoid having to import the module with testImplementation when using Gradle, if you think moving it to the test scope is worth it, I'll do it.

Say something nice to who's going to review your code.
Something nice, also thanks for your time.

ricdezen and others added 11 commits December 1, 2019 23:04
They were reversed compared to the handler and it was bugging me immensely
* 'master' of https://github.com/Cogno-IDU/smslibrary:
  Feature: library working on app closed (Cogno-Marco#13)
  Created README.md instructions (Cogno-Marco#16)
# Conflicts:
#	smslibrary/src/main/java/com/eis/smslibrary/SMSMessageHandler.java
#	smslibrary/src/test/java/com/eis/smslibrary/SMSPeerTest.java
fix: added Nullable annotations.
feat: added getDefaultRegion static method.
feat: added RandomPeerGenerator as an interface.
feat: added RandomSMSPeerGenerator as a class to generate random SMSPeers.
* @param <P> the type of generated Peer.
* @author Riccardo De Zen.
*/
public interface RandomPeerGenerator<A, P extends Peer<A>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this class to the test folder since it's only useful in tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, I asked exactly because I couldn't decide, altough this means the library has to be imported as a test library too (I'm 99% sure of this, gotta check)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal since it's probably not compiled when the app is built, but I'd have to check how Proguard works to confirm it

*
* @author Riccardo De Zen
*/
public class RandomSMSPeerGenerator implements RandomPeerGenerator<String, SMSPeer> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this class to the test folder since it's only useful in tests

* @return the parsed SMSMessage if the string was correct, null otherwise
*/
@Override
public SMSMessage parseMessage(@NonNull final String channelData, @NonNull final SMSPeer channelPeer) {
public SMSMessage parseMessage(@NonNull final String channelData,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you use a keyboard shortcut to automatically wrap the text in this file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, happened when pulling from the main repo because I had the reformat option on, did you prefer it like it was before?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'd just like to know what shortcut you used

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the usual CTRL+ALT+L

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird, it doesn't automatically wrap text when I use it, maybe I have to change something in my settings

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have changed the settings but I've had Android Studio installed for around two years, so I can't really remember, sorry

Comment on lines 71 to 72
* Method to generate an invalid address. As a simple invalidity criteria, a random String is
* returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A random string is not always invalid, since you're returning a String with a predefined first character I would say that the first part of the String is not random, and that this is done to guarantee invalidity.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah to make sure the address is invalid I added a header to the generated strings, I should change these specs

Comment on lines 99 to 102
* @return a random String, made of between 1 and 16 characters. The String always starts
* with {@link RandomSMSPeerGenerator#INVALID_ADDRESS_HEADER}.
*/
private String randomString() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really random, the first part of the string is predetermined. I'd call it almostRandomString(), or partiallyRandomString(), or invalidRandomString().

maxDigits = 15,
minDigits = 2,
maxCharValue = 255;
int digits = Math.abs(random.nextInt() % maxDigits) + minDigits;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result could be bigger than maxDigits

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops my bad I changed this a couple times and forgot to fix it

@RunWith(Parameterized.class)
public class RandomSMSPeerGeneratorTest {

private static final RandomSMSPeerGenerator GENERATOR = new RandomSMSPeerGenerator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a constant. It shouldn't be all upper-case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reference document, I never really put much thought into this and assumed static final -> constant, but now I see, "all Object fields that are never followed by a dot", that is a fair point, method calls could modify the state of an Object and thus its value.
I can't probably track down all of my static final objects named like this, but I will fix at least this and avoid it in the future.

Comment on lines 40 to 47
@Parameterized.Parameters(name = "{index}: {0}")
public static Collection<String> data() {
String[] supportedCountries = PhoneNumberUtil
.getInstance()
.getSupportedRegions()
.toArray(new String[0]);
return Arrays.asList(supportedCountries);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I heard about parametrized tests in class, but had no idea of what they were.

fix: moved classes to test folder
fix: improvements to RandomSMSPeerGenerator specs
fix: moved event validity logging in RandomSMSPeerGeneratorTest to a separate method
@NonNull
@Override
public String generateInvalidAddress() {
return getInvalidRandomString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might as well move the body of getInvalidRandomString() in here since it's the only place where it's used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno, I feel like leaving it separated in case I need to modify the class later or do something in the address method that doesn't involve the String directly, feels cleaner this way to me

Copy link

@tonin-alessandra tonin-alessandra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I think this could be really useful for testing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants