Categories
app dev maven

Random EAR Projects

I had a similar experience and ran across https://wiki.eclipse.org/M2E-WTP/New_and_Noteworthy/1.1.0 .

m2e-wtp can also be disabled via the m2e.wtp.activation>false in the Maven section of your project pom;xml.

I create a new workspace and cloned a fresh copy of my code in a temp directory. I then updated the parent pom.xml with the following:

<properties>
<m2e.wtp.activation>false</m2e.wtp.activation>
<m2e.jaxrs.activation>false</m2e.jaxrs.activation>
<m2e.jpa.activation>false</m2e.jpa.activation>
<m2e.jsf.activation>false</m2e.jsf.activation>
<m2e.cdi.activation>true</m2e.cdi.activation>
</properties>

I then imported Existing Maven Projects and no extra EAR projects.

The  JAX-RS, JPA and JSF configurators can be enabled or disabled at a workspace level from Window > Preferences > Maven > Java EE Integration.

Categories
app dev

Behavior Driven Development using Cucumber and Test NG

I have started playing with Test NG and Behavior Driven Development using Cucumber. Here are my notes from playing with Cucumber and Test NG.

In Eclipse, Click Help > Eclipse Marketplace

Enter into the Find Box – Cucumber

Click Go

You should see Eclipse plugin.

Click Install

When presented with the Review License dialog, click I Accept

Click Finish

Click Install anyway

Note, I personally clicked details and found they are the Eclipse Cucumber features

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.editor_0.0.22.201806032001.jar /Applications/Eclipse.app/Contents/Eclipse/features/cucumber.eclipse.feature_0.0.22.201806032001

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.steps.integration_0.0.22.201806032001.jar

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.steps.jdt_0.0.22.201806032001.jar

You can always click on the job to see the status.  In the lower right, you are going to see the install progress

Click Restart Now

Be sure to save your work.

Once Eclipse has restarted, click Create > Maven Project

Click Create a simple project

Click Next

Click Advanced

Enter Group Id – cucumber

Enter Artifact Id – test

Enter Version – 99-SNAPSHOT

Click Finish

Right Click the Project – test

Select Configure -> Convert to Cucumber Project

Open the pom.xml and add a dependency for Cucumber and Test NG

     <dependencies>

          <dependency>

               <groupId>io.cucumber</groupId>

               <artifactId>cucumber-java</artifactId>

               <version>4.7.1</version>

               <scope>test</scope>

          </dependency>

          <dependency>

               <groupId>io.cucumber</groupId>

               <artifactId>cucumber-testng</artifactId>

               <version>4.7.1</version>

               <scope>test</scope>

               <exclusions>

                     <exclusion>

                          <groupId>junit</groupId>

                          <artifactId>junit</artifactId>

                     </exclusion>

               </exclusions>

          </dependency>

          <dependency>

               <groupId>org.testng</groupId>

               <artifactId>testng</artifactId>

               <version>6.14.3</version>

               <scope>test</scope>

          </dependency>

     </dependencies>

 

While in the pom.xml, make sure the version of the project is Java 1.8.

     <properties>

          <maven.compiler.source>1.8</maven.compiler.source>

          <maven.compiler.target>1.8</maven.compiler.target>

     </properties>

Click File > Save

Right Click New > Class

Enter Name – RunCucumberTest

Click Browse

Find AbstractTestNGCucumberTests

Click OK

Click OK

Annotate the class with

@CucumberOptions(plugin = "pretty", features = "src/test/resources/TestMe.feature")

It’s a good reminder of where you can go to modify Cucumber.

Right Click the Project > New > Other

Select Cucumber Step Definition File

Enter the Package Name – cucumber

Enter the Class Name – TestMe

Select a few of the annotations@Given @Then @And

Click Finish

Note, I changed the packageio.cucumber.” to the newest approaches.

Replace the content of TestMe.java

package cucumber;

import org.testng.Assert;

import io.cucumber.java.en.And;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

public class TestMe {

     private int value;

     private int percentage;

     @Given("^I have \"([^\"]*)\"$")

     public void given(String start) throws Throwable {

          this.value = Integer.parseInt(start);

     }

     @And("^I want to add a \"([^\"]*)\"$")

     public void and(String increase) throws Throwable {

          this.percentage = Integer.parseInt(increase);

     }

     @Then("^I should have \"([^\"]*)\"$")

     public void then(String target) throws Throwable {

          Assert.assertEquals(value * percentage, Integer.parseInt(target));

     }

}

The class tests the given, and, then predicates with a simple Assert.  You can always use the TestNG Assert.

Right Click on the TestMe file > Run Configuration

Select Cucumber Feature

Enter Name – TestMe.feature

Replace the Contents with the following:

Feature: TestMe

  Scenario Outline: use the API

    Given I have "<start>"

    And I want to add a "<increase>"

    Then I should have "<total>"

  Examples:

    | start | increase | total |

    |    100 |   100 |    10000 |

Right Click on the Project

Select Run As

Select maven test

You should see

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< cucumber:test>----------------------------
[INFO] Building test 99-SNAPSHOT
[INFO] --------------------------------[ jar]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---
[INFO] Surefire report directory: /Users/paulbastide/eclipse-workspace/test/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Running TestSuite

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@123772c4

Feature: TestMe
  Scenario Outline: use the API with Java7 style # src/test/resources/TestMe.feature:2

    Given I have "<start>"
    And I want to add a "<increase>"
    Then I should have "<total>"

    Examples:

  Scenario Outline: use the API with Java7 style # src/test/resources/TestMe.feature:9
    Given I have "100"                           # TestMe.given(String)
    And I want to add a "100"                    # TestMe.and(String)
    Then I should have "10000"                   # TestMe.then(String)

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.336 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.644 s
[INFO] Finished at: 2019-08-04T13:03:15-04:00
[INFO] ------------------------------------------------------------------------

This is the scenario which you can use, mix and match with other inputs to drive an optimal testing experience.

 

Next step, to figure out how I can use this approach in my healthcare API projects and increase efficacy of my products.

References

Cucumber Eclipse https://cucumber.github.io/cucumber-eclipse/

Cucumber Test NG – https://github.com/cucumber/cucumber-jvm/tree/master/testng

Auto Populated Feature file – https://github.com/cucumber/cucumber-eclipse/wiki/Auto-Generation-of-Sample-Feature-Definition-Template

Step Definition https://github.com/cucumber/cucumber-eclipse/wiki/New-Step-Definition-File-Wizard

Maven Standard Directory Layout http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Test NG and Cucumber – https://medium.com/agile-vision/cucumber-bdd-part-2-creating-a-sample-java-project-with-cucumber-testng-and-maven-127a1053c180

Test https://cucumber.io/docs/guides/10-minute-tutorial/

Examples https://cucumber.io/docs/bdd/example-mapping/

Categories
raspberry pi

Raspberry Pi – Part III – Setting up Jetty

The Raspberry Pi is a device that has so many options open: You can extend the hardware. You can extend the software. I choose to tackle extending the software to start, so I can get the experience that I want setup, and running on the device.  The experience I am after is the web interface to control my Raspberry Pi.

For a Java developer, I conclude that a lightweight interface hosted on a Jetty server is probably easiest.  The Jetty server is a 12.9M download and 30M expanded. It’s also used frequently in devices, and why reinvent the wheel, use the approach that Industry is using.  Jetty is designed for a small memory footprint.   (I did consider using Tomcat Embedded, and came to the conclusion I’d probably want to many features.)

I downloaded the Jetty archive to my local user’s directory. Note, since I want to just use jetty.zip I’m renaming the output file with wget -O.

wget -O jetty.zip http://download.eclipse.org/jetty/stable-9/dist/jetty-distribution-9.2.10.v20150310.zip

I make a directory for the application server, and move over jetty.zip to that directory.

sudo mkdir -p /opt/jetty
sudo mv jetty.zip /opt/jetty

Next I launched into the sudoers root shell, and extract the Jetty archive.  I decided to move it into the runtime folder, so I can have an easy place to backup from.  (Remove the extra space from the .zip)

sudo -s 
cd /opt/jetty
unzip jetty.zip
mv jetty-distribution-9.2.10.v20150310/ runtime
rm jetty.zip

Next, I wanted to setup Jetty as a service, and check that Jetty starts

cp runtime/bin/jetty.sh /etc/init.d/jetty
echo JETTY_HOME=`pwd`/runtime > /etc/default/jetty
service jetty start
Starting Jetty: . . . OK Sun Apr 12 15:38:40 UTC 2015
service jetty stop
Stopping Jetty: OK

Next, I want to automatically start Jetty with the right runlevels

update-rc.d jetty defaults

I want to configure Jetty to run with a set user jetty.

mkdir -p /opt/jetty/web/bbq
mkdir -p /opt/jetty/temp
useradd --user-group --shell /bin/false --home-dir /opt/jetty/temp jetty

There is a base configuration that needs to be setup the base site.

cd /opt/jetty/web/bbq
java -jar /opt/jetty/runtime/start.jar --add-to-start=deploy,http,logging

Next, edit the default port value.

vi start.ini

## HTTP port to listen on
jetty.port=80

Finally, I wrap the configuration of the jetty service and app.

chown -R jetty:jetty /opt/jetty
echo "JETTY_HOME=/opt/jetty/runtime" > /etc/default/jetty
echo "JETTY_BASE=/opt/jetty/web/bbq" >> /etc/default/jetty
echo "TMPDIR=/opt/jetty/temp" >> /etc/default/jetty

A good check is to look at service jetty status, and confirm the settings, and then restart your Raspberry Pi. I did find that the startup time was significantly effected by the additional service.  ( +25 seconds from the original 15)

Navigate to http://192.168.1.200/ (or whatever IP you have used) and confirm the page loads Jetty.  If you see a 404, you’re off and ready for the next phase. (as am I) 🙂

Jetty Starting Point
Jetty Starting Point