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

178 - Allow users to input CHT URLs with leading or trailing spaces and or "/" #233

Merged
merged 8 commits into from
Dec 2, 2021

Conversation

mrsarm
Copy link
Contributor

@mrsarm mrsarm commented Nov 25, 2021

User may enter the URL with blank spaces at the beginning or the end while copy & pasting from a message or from the browser. Same with the "/" char at the end, if the user copy & paste the URL from the the URL bar, it may include the "/" at the end that doesn't change the actual resource it wanted to reach.

Issue: #178

@mrsarm mrsarm requested a review from jkuester November 25, 2021 19:31
@mrsarm mrsarm marked this pull request as ready for review November 25, 2021 19:31
User may enter the URL with blank spaces at the beginning or the end while copy & pasting from a message or from the browser
Copy link
Contributor

@jkuester jkuester left a comment

Choose a reason for hiding this comment

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

Looks good. Just a few minor comments. +1 for a full range of unit tests!

mrsarm and others added 2 commits November 29, 2021 15:40
…vity.java

Co-authored-by: Joshua Kuestersteffen <jkuester@kuester7.com>
Co-authored-by: Joshua Kuestersteffen <jkuester@kuester7.com>
@mrsarm mrsarm requested a review from jkuester November 30, 2021 15:40
}

@Test
public void testUrlMalformed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this test different from the previous one (testAllMistakesUrl)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrong copy & paste, removing...

@Config(sdk=28)
public class AppUrlVerifierTest {

private AppUrlVerifier buildAppUrlVerifier(JSONObject jsonResponse) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When possible I like to avoid having to hack like this with protected methods. Java does not make this easy, but I think in this case we could use Mockito to inject a mock SimpleJsonClient2 into the AppUrlVerifier via a second constructor instead of overwriting the protected methods. The first thing we would have to do is make the json client a class-level variable in AppUrlVerifier (and get rid of getJsonClient()) that is set by the constructors:

	private final SimpleJsonClient2 jsonClient;

	AppUrlVerifier(SimpleJsonClient2 jsonClient) {
		this.jsonClient = jsonClient;
	}

	public AppUrlVerifier() {
		this(new SimpleJsonClient2());
	}

Then, this test code can be updated to look something like this:

	private AppUrlVerifier buildAppUrlVerifier(JSONObject jsonResponse) {
		try {
			SimpleJsonClient2 mockJsonClient = Mockito.mock(SimpleJsonClient2.class);
			Mockito.when(mockJsonClient.get((String) ArgumentMatchers.any())).thenReturn(jsonResponse);
			return new AppUrlVerifier(mockJsonClient);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private AppUrlVerifier buildAppUrlVerifierWithException(Exception e) {
		try {
			SimpleJsonClient2 mockJsonClient = Mockito.mock(SimpleJsonClient2.class);
			Mockito.when(mockJsonClient.get((String) ArgumentMatchers.any())).thenThrow(e);
			return new AppUrlVerifier(mockJsonClient);
		}  catch (Exception exception) {
			throw new RuntimeException(exception);
		}
	}

	private AppUrlVerifier buildAppUrlVerifierOk() {
		try {
			return buildAppUrlVerifier(new JSONObject("{\"ready\":true,\"handler\":\"medic-api\"}"));
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea of injecting the Json client (Spring style 😉 ). I don't like Mockito too much, I think it overrides behaviors that the language already offer (like overriding behavior and injecting objects), allowing developers to write code with anti-patterns that the other way is less possible, because that kind of code is less testable unless... mockito.

Anyway, we already have Mockito in the project and it makes the code more compact in many cases (much appreciated when you write in Java), so I'll use it... against my will 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, Mockito always makes me cringe too. Every time I use it I just get to thinking "there has to be a better way". But, I have never been able to find one (unless your production code is already structured to leverage some kind of dependency injection or service provider framework).

In this case it feels like we are using Mockito the "good" way in that we are just using it to construct objects with mocked behavior that we can inject (and not doing any kind of deep manipulation on the implementation code...).

mrsarm and others added 4 commits November 30, 2021 21:06
….java

Co-authored-by: Joshua Kuestersteffen <jkuester@kuester7.com>
….java

Co-authored-by: Joshua Kuestersteffen <jkuester@kuester7.com>
@mrsarm mrsarm requested a review from jkuester December 1, 2021 16:00
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.

Allow users to set cht-core URLs with leading or trailing spaces (and trailing slash) on unbranded app
2 participants