From a22f42407773b6e5e08c885ea197189ae5633133 Mon Sep 17 00:00:00 2001 From: dsellarsnr Date: Mon, 4 Mar 2024 13:04:43 -0500 Subject: [PATCH] Add better error scenario --- .gitignore | 1 - WebPortal/Java/pom.xml | 43 ++- .../src/main/java/acme/storefront/MyMain.java | 41 +++ .../action/report/UserDataManager.java | 255 ++++++++++++++++++ .../action/report/UserDataManagerTest.java | 12 + WebPortal/Java/web.iml | 56 ---- 6 files changed, 348 insertions(+), 60 deletions(-) create mode 100644 WebPortal/Java/src/main/java/acme/storefront/MyMain.java create mode 100644 WebPortal/Java/src/main/java/acme/storefront/action/report/UserDataManager.java create mode 100644 WebPortal/Java/src/test/java/acme/storefront/action/report/UserDataManagerTest.java delete mode 100644 WebPortal/Java/web.iml diff --git a/.gitignore b/.gitignore index a1c2a23..9ca7801 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ .mtj.tmp/ # Package Files # -*.jar *.war *.nar *.ear diff --git a/WebPortal/Java/pom.xml b/WebPortal/Java/pom.xml index 0d6befd..3c2e980 100644 --- a/WebPortal/Java/pom.xml +++ b/WebPortal/Java/pom.xml @@ -10,7 +10,6 @@ 1.8 1.8 - true @@ -22,7 +21,23 @@ UTF-8 - + + maven-surefire-plugin + 3.2.5 + + + maven-assembly-plugin + + + + acme.storefront.MyMain + + + + jar-with-dependencies + + + @@ -31,6 +46,17 @@ + + + + org.junit + junit-bom + 5.10.2 + pom + import + + + com.fasterxml.jackson.core @@ -102,7 +128,18 @@ com.newrelic.agent.java newrelic-api - 8.0.0 + 8.9.1 + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-inline + 4.10.0 + test diff --git a/WebPortal/Java/src/main/java/acme/storefront/MyMain.java b/WebPortal/Java/src/main/java/acme/storefront/MyMain.java new file mode 100644 index 0000000..2d5c9d2 --- /dev/null +++ b/WebPortal/Java/src/main/java/acme/storefront/MyMain.java @@ -0,0 +1,41 @@ +package acme.storefront; + +import acme.storefront.action.report.UserDataManager; +import com.newrelic.api.agent.Trace; + +import java.util.List; + +public class MyMain { + public static void main(String args[]) { + System.out.println("hello"); + MyMain main = new MyMain(); + + Thread thread = new Thread(() -> { + while (true) { + try { + main.runApp(); + } catch (Exception e) { + System.err.println("Error: " + e); + // Handle interruption here or re-interrupt the thread + } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }); + + thread.start(); + } + + private UserDataManager usrmgr = new UserDataManager(); + + @Trace(dispatcher = true) + public void runApp() { + UserDataManager.UserView user1 = usrmgr.getUserById("abcd1"); + System.out.println("user 1 " + user1); + List userViewList = usrmgr.getUserViewByState("MA"); + System.out.println(userViewList); + } +} diff --git a/WebPortal/Java/src/main/java/acme/storefront/action/report/UserDataManager.java b/WebPortal/Java/src/main/java/acme/storefront/action/report/UserDataManager.java new file mode 100644 index 0000000..1e5bb71 --- /dev/null +++ b/WebPortal/Java/src/main/java/acme/storefront/action/report/UserDataManager.java @@ -0,0 +1,255 @@ +package acme.storefront.action.report; + +import com.newrelic.api.agent.Trace; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class UserDataManager { + + public static class Address { + private String street1; + private String zip; + private String city; + private String state; + + public Address(String street1, String zip, String city, String state) { + this.street1 = street1; + this.zip = zip; + this.city = city; + this.state = state; + } + + public String getStreet1() { + return street1; + } + + public void setStreet1(String street1) { + this.street1 = street1; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @Override + public String toString() { + return "Address{" + + "street1='" + street1 + '\'' + + ", zip='" + zip + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + '}'; + } + } + + public static class Phone { + private String countryCode; + private String number; + + public Phone(String countryCode, String number) { + this.countryCode = countryCode; + this.number = number; + } + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + @Override + public String toString() { + return "Phone{" + + "countryCode='" + countryCode + '\'' + + ", number='" + number + '\'' + + '}'; + } + } + + public static class User { + private String firstName; + private String lastName; + private Phone phone; + private Address address; + + public User(String firstName, String lastName, Phone phone, Address address) { + this.firstName = firstName; + this.lastName = lastName; + this.phone = phone; + this.address = address; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Phone getPhone() { + return phone; + } + + public void setPhone(Phone phone) { + this.phone = phone; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + @Override + public String toString() { + return "User{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", phone=" + phone + + ", address=" + address + + '}'; + } + } + + public static class UserView { + private String firstName; + private String lastName; + private String phoneNumber; + private String state; + + public UserView(String firstName, String lastName, String phoneNumber, String state) { + this.firstName = firstName; + this.lastName = lastName; + this.phoneNumber = phoneNumber; + this.state = state; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @Override + public String toString() { + return "UserView{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", phoneNumber='" + phoneNumber + '\'' + + ", state='" + state + '\'' + + '}'; + } + } + + static class UserData { + Map userDb; + + UserData() { + userDb = new HashMap<>(); + userDb.put("abcd1", + new User("Joe", + "Malaci", + new Phone("1", "888-444-2222"), + new Address("1234 W. Here Pl", "33232", "Boston", "MA"))); + userDb.put("abcd2", + new User("Jane", "Todd", null, new Address("1234 W. Here Pl", "33232", "Boston", "MA"))); + userDb.put("abcd3", + new User("Jane", "Todd", null, new Address("1234 E. There Pl", "13131", "Boulder", "CO"))); + } + } + + private final UserData userData = new UserData(); + + @Trace + public List getUserViewByState(String state) { + List userViewList = new ArrayList<>(); + for (User user : userData.userDb.values()) { + if (user.getAddress().getState().equals(state)) { + userViewList.add(new UserView(user.getFirstName(), + user.getLastName(), + "+" + user.getPhone().getCountryCode() + " " + user.getPhone().getNumber(), + user.getAddress().getState())); + } + } + return userViewList; + } + + @Trace + public UserView getUserById(String userId) { + User user = userData.userDb.get(userId); + return new UserView(user.getFirstName(), + user.getLastName(), + "+" + user.getPhone().getCountryCode() + " " + user.getPhone().getNumber(), + user.getAddress().getState()); + } +} diff --git a/WebPortal/Java/src/test/java/acme/storefront/action/report/UserDataManagerTest.java b/WebPortal/Java/src/test/java/acme/storefront/action/report/UserDataManagerTest.java new file mode 100644 index 0000000..ef69a6c --- /dev/null +++ b/WebPortal/Java/src/test/java/acme/storefront/action/report/UserDataManagerTest.java @@ -0,0 +1,12 @@ +package acme.storefront.action.report; + +import org.junit.jupiter.api.Test; + +class UserDataManagerTest { + + @Test + void purchaseProduct() { +// UserDataManager userDataManager = new UserDataManager(); +// userDataManager.getUserViewByState("MA"); + } +} diff --git a/WebPortal/Java/web.iml b/WebPortal/Java/web.iml deleted file mode 100644 index a56a382..0000000 --- a/WebPortal/Java/web.iml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file