Json REST api Test with Java

 

I recently put together this simple program that parses JSONinput from either a file or a url. It showcases the use of REST apis to a read data and store it as a JSON object, JSON array and other Java objects for further manipulation.

It uses the example website: jsonplaceholder.typicode.com/albums and reads the data.

It will also read a local JSON file.  For the program to complete successfully you need to either create a JSON file named  “C:\jasondata2.json” or use any name and change the name inside the program. To easily create a JSON file you can download the data contained in the file url above and save it.

The secret to easily reading a URL and storing it’s contents is really just a few lines.  You want to use a BufferedReader to experience efficient reading of characters, arrays, and lines.

 


 

 

Once you have an entire JSON string (starting with “[” ) you can turn it into a Collection, JSONArray, ArrayList or Map like this:

 

 

Here’s the entire program.

 

 

Transferring to a New Domain Registrar

Transferring your domain to a new registrar can be frustrating if you miss any of the necessary steps. Here I will outline steps that should apply to most registrars. I’ve used the same steps with GoDaddy, Name.com and Namesilo.com.  The whole thing can go very quickly – in as little as 10 minutes or so assuming both registrars respond quickly.

  1. Go to the “winning” registrar:
    a) Setup an account (if you haven’t already).
    b) Find and record their recommended name servers.
  2.  Log into the “losing” registrar (the one you’re leaving) and:
    a) Make sure you like the email address that the Registrant, Billing, Technical and Adminstrative email addresses are set to because they’ll need to communicate with one or more of these during your transfer. To be safe, switch them all to your current email address.
    b) Turn off Forwarding (if it’s on)
    c) Turn off Privacy (if it’s on)
    d) Unlock the domain
    e) Ask for a transfer “key” which may be called a key, transfer secret or authorization code. You’ll need this at the winning registrar. The loser provides it to you on their website or emails it to you.
  3. Go back to the winning registrar:
    a) Request the transfer. You’ll input the domain address and the authorization key-code-secret.
  4. Watch your email. The winner will send you an email asking you to verify that you want to transfer in. Watch for this email and verify yes.
  5. Next you need to log back into the losing registrar and ask them to expedite your transfer. GoDaddy has a way to do this online, where Name.com asks you to email their support team. In any case, most of them are very quick to respond because if they aren’t – they are leaving your domain in a dangerous state of no privacy, no forwarding, etc. I’ve seen this happen in a matter of a few minutes after making this request.Here’s the GoDaddy instructions for getting them to accept a transfer out: GoDaddy Accepting-or-declining-a-transfer
    If you’ve chosen a decent registrar, they should have a progress page you can go to to see your progress. They may also email you some kind of confirmation of progress.
  6. After the transfer, go into the settings and remove the old nameservers. Make sure all the dns settings are correct. Reset up your forwarding and privacy settings.

As far as which registrar to choose – I ended up at Namesilo.com because they offer great service, are super inexpensive, and offer free Whois privacy with every domain.  Name.com was also very good – just not as inexpensive. GoDaddy is fine – just very pricey.

In closing, here are some great dns tools you can use to check your domain:
network-tools.com/nslook
www.dnsinspect.com

Eclipse with Java, Maven and Git setup

It took me awhile to perfect my setting up of Java-based projects in Eclipse that also utilize Maven and Git. I wasn’t sure in which order to do it, and if it should all be done inside Eclipse or not. So I thought I’d share my final process here. My version of Eclipse is Luna, although the general steps should work with other versions as well.

First you need Eclipse set up with the m2e (Maven to Eclipse) plugin. You will also need to install the EGit plugin. You need to have a regular “Workspace” set up, and optionally a Git local respository set up as well (NOT inside the Workspace – should be a separate directory). Or – you can let the EGit plugin create a Git repository for you.

In Eclipse, from the “File” menu, select “New” then “Other”. In the wizard that pops up, open “Maven” then select “Maven Project”. Click “Next”, then select the “Create a simple project” box.

In the next window, for Group Id – put your package name, and for Artifact Id – put your package name. Add a name for the project itself and other items if you like. Click finish.

The new project will be created in your regular workspace. For Git – remember you need to have the EGit Eclipse plugin installed.

To add your project to your Git repo, open up the “Java Browsing” perspective, and right click on the new project you just created. Look for the word “Team” in the menulist that pops up. Select “Team” (in my list it was 8th from the bottom), then “Share Project…”. Follow the prompts from there to use an existing repository or create a new one.

Now you should have a new Java Maven-based project in your Git repository. Notice the pom.xml file that is create. You will have to add your own dependencies to this.

Below is an example of the simple pom.xml file file that is created:

Logging with Log4j2

Log4j2 is a successor to the original Log4j — a Java utility library used to output log files from a program. It writes messages to various endpoints can include all sorts of destinations like emails, webpages, databases and more. I tried a few other newer loggers like SLF4J, and came back to this one because the others were so difficult to set up.  Log4j2 is simple, lightweight and powerful.  Here we will set up Log4j2 in a Java project in Eclipse.

Maven is the build configurator. Here are the lines from the Maven pom.xml file that are necessary for Log4j2 to work. You need both of these as a minimum. Be sure the check for the latest version and update yours accordingly.

Log4j2 Config File

The configuration or preferences file is usually an xml file, although you can also use JSON or a plain text properties file. This file is critical and can have a number of different names. I chose log4j2-test.xml. It needs to reside in the Java classpath. Ours today is here in src/test/resources. This is very bare-bones setup except for the RollingFile appender which gives files datestamps.

The Logger is the actor who is doing the logging. Each logger can have any number of appenders. You can have an number of loggers configured in many different ways and callled on at different times. In this case we only have one logger called Root. There is always at least the root logger, and it’s the top of the hierarchy. We’ll just use that one since we’re trying to keep this simple.

An Appender is actually the location that the information is going to – like the console. You can specify reports, files, emails, network locations, sockets, databases, etc. Within the Appender section is where you set up what levels of output you want and to which locations, as well and other settings. The first one we have set up is for the console.
The RollingFile appender sets up our log files so that when a new one is created, the last log file is renamed with a datestamp so it’s not overwritten. Notice each appender has a PatternLayout which is self-explanatory.

Here are the Java imports which won’t work unless your maven log4j statements are set up correctly.

Here is the Setup code in the the Java program where it actually connects to it’s xml configuration file. I call the root logger using a special method called “getRootLogger” – for all other loggers you would use the method “getLogger”.

And here’s where we actually use the logger. Just replace your normal System.out.println with yourLogger.level – as in this case it’s gLogger.info – for the info level (see levels below).

Log4j Log Levels

Each level prints itself plus the levels that came before it. So if you choose WARN you also get ERROR and FATAL messages. Each level enables a finer granularity of detail. DEBUG and TRACE are intended for development only.

OFF No logging
FATAL Severe errors
ERROR non-fatal errors
WARN misc warnings
INFO runtime events
DEBUG Detailed info
TRACE Most detailed
TTCC is a message format used by log4j -an acronym for Time Thread Category Component.

See the entire project at:
https://github.com/nmschorr/git2/tree/master/SMedia

Java JNA to Dismiss Windows 7 Dialog

This video uses Java JNA to dismiss a Windows 7 native dialog that pops up while Firefox is launching during a Selenium test. The same technique can be used to manage any Windows 7 native dialog or window from java in many other situations.

In this case, the alert declares that Firefox has crashed, and waits to be manually dismissed. Until dismissed somehow, it blocks Firefox and the test from continuing.

This workaround addresses these logged bugs:
Firefox Bug 1167511 – When running selenium automated test, a window pops up saying “Firefox has stopped working”
Firefox Bug 1157672 – Firefox crashes on shutdown when invoked with -silent and empty profile directory
Selenium Bug #437 – Firefox 38 doesn’t work with WebDriver 2.45.1

This workaround may work in other situations where something within a Selenium script causes a Windows alert dialog window to appear.

The alert can’t be dismissed normally without manual intervention. Selenium can’t see it because it’s put up by by Windows and is not part of the Firefox..The alert needs to go away before the thread that caused it can continue.

Simple scripts run as a single program thread, each step executing after the previous step is done.
We need to start a second program thread to go around the outside of our normal set of steps to dismiss that dialog.

First create a new thread by creating a class that is an extension of the Thread class. Then we add our alert-dismiss code to the run method of this 2nd thread. When the thread starts, the dismiss code will execute.

JNA stands for Java Native Access. It’s a community developed library that supports many languages and platforms like MacOS and Linux. JNA provides a simple set of methods that access more complex lower-level operating system calls. It makes programming system calls easy.

Here’s the snippet of JNA code that closes the alert window:

Here’s the code to create the 2nd thread:

Here’s the code for starting the 2nd thread

See the entire project at:
https://git.io/vC7wp

Selenium with Java Testing a Website

This video shows the Eclipse IDE running a web test written with Selenium, Java, JUnit, Log4j2, Maven and other packages. It tests against my own website: schorrmedia.com which is also known as schorrmedia.com/wordpress

The tests are run with JUnit, so each test stands on its own and can be run independently of the others. Each test launches a fresh instance of FireFox with Selenium WebDriver. The tests use Maven and a Maven pom.xml file to specify dependencies. The entire suite was written in and runs with Eclipse on a Windows 7 PC.

The source code can be viewed on GitHub at: https://git.io/vC7wp

This demo was done with inverted color on a Windows 7 machine. I’m using inverted color a lot lately because it’s easier on the eyes.

The script also includes full Maven Surefire site test result reporting which I hope to share in a future video. To read more about Selenium, see: www.seleniumhq.org

Thank you Guru99.com for helping me learn Selenium. Their Selenium Course was very useful and free.