From f207ecf677d192e76673b01fefaaad151fe3d820 Mon Sep 17 00:00:00 2001 From: jgiroso Date: Wed, 4 Aug 2021 16:20:06 -0400 Subject: [PATCH 1/4] added MainApp and Potions class --- .idea/compiler.xml | 1 + .idea/jarRepositories.xml | 20 +++++ .idea/vcs.xml | 6 ++ pom.xml | 35 ++++++-- src/main/java/MainApplication.java | 111 ++++++++++++++++++++++++++ src/main/java/models/PotionPouch.java | 78 ++++++++++++++++++ 6 files changed, 243 insertions(+), 8 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/MainApplication.java create mode 100644 src/main/java/models/PotionPouch.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f006a55..5a76f28 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,6 +6,7 @@ + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d97c43d..636ce5e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,15 +4,34 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.zipcoder.lab + org.example jdbcdao 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + + + + - - - junit - junit - 4.10 - - + + + + com.fasterxml.jackson.core + jackson-databind + 2.12.3 + + + mysql + mysql-connector-java + 8.0.18 + + \ No newline at end of file diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java new file mode 100644 index 0000000..c4def4b --- /dev/null +++ b/src/main/java/MainApplication.java @@ -0,0 +1,111 @@ + +import com.mysql.cj.jdbc.Driver; +import daos.PokemonRepository; +import models.Pokemon; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.StringJoiner; + +/** + * @author git-leon + * @version 1.0.0 + * @date 8/2/21 9:49 AM + */ +public class MainApplication { + + public static void main(String[] args) { + registerJDBCDriver(); + Connection mysqlDbConnection = getConnection("mysql"); + PokemonRepository pokemonRepository = new PokemonRepository(mysqlDbConnection); + executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS databaseName;"); + executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS databaseName;"); + executeStatement(mysqlDbConnection, "USE databaseName;"); + executeStatement(mysqlDbConnection, new StringBuilder() + .append("CREATE TABLE IF NOT EXISTS databaseName.pokemonTable(") + .append("id int auto_increment primary key,") + .append("name text not null,") + .append("primary_type int not null,") + .append("secondary_type int null);") + .toString()); + + pokemonRepository.create(new Pokemon(12L, "Ivysaur", 3, 7)); + pokemonRepository.create(new Pokemon(13L, "Ivysaurr", 3, 7)); + System.out.println(pokemonRepository.readAll()); + + } + + static ResultSet executeQuery(Connection connection, String sqlQuery) { + try { + Statement statement = getScrollableStatement(connection); + return statement.executeQuery(sqlQuery); + } catch (SQLException e) { + throw new Error(e); + } + } + + static void printResults(ResultSet resultSet) { + try { + for (int rowNumber = 0; resultSet.next(); rowNumber++) { + String firstColumnData = resultSet.getString(1); + String secondColumnData = resultSet.getString(2); + String thirdColumnData = resultSet.getString(3); + System.out.println(new StringJoiner("\n") + .add("Row number = " + rowNumber) + .add("First Column = " + firstColumnData) + .add("Second Column = " + secondColumnData) + .add("Third column = " + thirdColumnData)); + } + } catch (SQLException e) { + throw new Error(e); + } + } + + static void executeStatement(Connection connection, String sqlStatement) { + try { + Statement statement = getScrollableStatement(connection); + statement.execute(sqlStatement); + } catch (SQLException e) { + throw new Error(e); + } + } + + static Statement getScrollableStatement(Connection connection) { + int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE; + int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY; + try { // scrollable statements can be iterated more than once without closing + return connection.createStatement(resultSetType, resultSetConcurrency); + } catch (SQLException e) { + throw new Error(e); + } + } + + static Connection getConnection(String dbVendor) { + String username = "root"; + String password = ""; + String url = new StringBuilder() + .append("jdbc:") + .append(dbVendor) + .append("://127.0.0.1/") + .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") + .toString(); + try { + return DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + throw new Error(e); + } + } + + static void registerJDBCDriver() { + // Attempt to register JDBC Driver + try { + DriverManager.registerDriver(Driver.class.newInstance()); + } catch (InstantiationException | IllegalAccessException | SQLException e1) { + throw new RuntimeException(e1); + } + } + +} diff --git a/src/main/java/models/PotionPouch.java b/src/main/java/models/PotionPouch.java new file mode 100644 index 0000000..9ebecaa --- /dev/null +++ b/src/main/java/models/PotionPouch.java @@ -0,0 +1,78 @@ +package models; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class PotionPouch { + + private Long id; + private String name; + private String ingredient1; + private String ingredient2; + private String effect; + + public PotionPouch() {} + + public PotionPouch(Long id, String name, String ingredient1, String ingredient2, String effect) { + this.id = id; + this.name = name; + this.ingredient1 = ingredient1; + this.ingredient2 = ingredient2; + this.effect = effect; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIngredient1() { + return ingredient1; + } + + public void setIngredient1(String ingredient1) { + this.ingredient1 = ingredient1; + } + + public String getIngredient2() { + return ingredient2; + } + + public void setIngredient2(String ingredient2) { + this.ingredient2 = ingredient2; + } + + public String getEffect() { + return effect; + } + + public void setEffect(String effect) { + this.effect = effect; + } + + @Override + public String toString() { + try { + return new ObjectMapper().writeValueAsString(this); + } catch (JsonProcessingException e) { + return "PotionPouch {" + + "ID = " + id + + ", Name = " + name + + ", First Ingredient = " + ingredient1 + + ", Second Ingredient = " + ingredient2 + + ", Effects = " + effect + + "}"; + } + } +} From fe7c2318864e94d5e75b4087222010bd40db0800 Mon Sep 17 00:00:00 2001 From: jgiroso Date: Wed, 4 Aug 2021 19:32:11 -0400 Subject: [PATCH 2/4] potion repository complete. data added to sql table --- src/main/java/MainApplication.java | 29 +++--- src/main/java/daos/PotionRepository.java | 93 ++++++++++++++++++ src/main/java/daos/Repository.java | 44 +++++++++ .../models/{PotionPouch.java => Potion.java} | 6 +- target/classes/MainApplication.class | Bin 0 -> 4964 bytes target/classes/daos/PotionRepository.class | Bin 0 -> 4701 bytes target/classes/daos/Repository.class | Bin 0 -> 1317 bytes target/classes/models/Potion.class | Bin 0 -> 2337 bytes 8 files changed, 155 insertions(+), 17 deletions(-) create mode 100644 src/main/java/daos/PotionRepository.java create mode 100644 src/main/java/daos/Repository.java rename src/main/java/models/{PotionPouch.java => Potion.java} (91%) create mode 100644 target/classes/MainApplication.class create mode 100644 target/classes/daos/PotionRepository.class create mode 100644 target/classes/daos/Repository.class create mode 100644 target/classes/models/Potion.class diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java index c4def4b..1dc92a6 100644 --- a/src/main/java/MainApplication.java +++ b/src/main/java/MainApplication.java @@ -1,7 +1,7 @@ import com.mysql.cj.jdbc.Driver; -import daos.PokemonRepository; -import models.Pokemon; +import daos.PotionRepository; +import models.Potion; import java.sql.Connection; import java.sql.DriverManager; @@ -20,21 +20,22 @@ public class MainApplication { public static void main(String[] args) { registerJDBCDriver(); Connection mysqlDbConnection = getConnection("mysql"); - PokemonRepository pokemonRepository = new PokemonRepository(mysqlDbConnection); - executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS databaseName;"); - executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS databaseName;"); - executeStatement(mysqlDbConnection, "USE databaseName;"); + PotionRepository pokemonRepository = new PotionRepository(mysqlDbConnection); + executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS potions;"); + executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS potions;"); + executeStatement(mysqlDbConnection, "USE potions;"); executeStatement(mysqlDbConnection, new StringBuilder() - .append("CREATE TABLE IF NOT EXISTS databaseName.pokemonTable(") + .append("CREATE TABLE IF NOT EXISTS potions.potionsTable(") .append("id int auto_increment primary key,") .append("name text not null,") - .append("primary_type int not null,") - .append("secondary_type int null);") + .append("ingredient1 text not null,") + .append("ingredient2 text not null,") + .append("effect text not null);") .toString()); - pokemonRepository.create(new Pokemon(12L, "Ivysaur", 3, 7)); - pokemonRepository.create(new Pokemon(13L, "Ivysaurr", 3, 7)); - System.out.println(pokemonRepository.readAll()); + pokemonRepository.create(new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen")); + pokemonRepository.create(new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen")); + System.out.println(pokemonRepository.findAll()); } @@ -84,8 +85,8 @@ static Statement getScrollableStatement(Connection connection) { } static Connection getConnection(String dbVendor) { - String username = "root"; - String password = ""; + String username = "jen"; + String password = "zipcode0"; String url = new StringBuilder() .append("jdbc:") .append(dbVendor) diff --git a/src/main/java/daos/PotionRepository.java b/src/main/java/daos/PotionRepository.java new file mode 100644 index 0000000..1faa60e --- /dev/null +++ b/src/main/java/daos/PotionRepository.java @@ -0,0 +1,93 @@ +package daos; + +import models.Potion; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class PotionRepository implements Repository{ + + private Connection connection; + + public PotionRepository(Connection connection) { + this.connection = connection; + } + public Connection getConnection() { + return connection; + } + + public void create(Potion potion) { + executeStatement(String.format(new StringBuilder() + .append("INSERT INTO potions.potionsTable(") + .append("id, name, ingredient1, ingredient2, effect)") + .append("VALUES (%s, '%s', '%s', '%s', '%s');") + .toString(), + potion.getId(), + potion.getName(), + potion.getIngredient1(), + potion.getIngredient2(), + potion.getEffect())); + } + + public List findAll() { + ResultSet resultSet = executeQuery("SELECT * FROM potions.potionsTable;"); + List list = new ArrayList<>(); + try { + while (resultSet.next()) { + String id = resultSet.getString(1); + String name = resultSet.getString(2); + String ingredient1 = resultSet.getString(3); + String ingredient2 = resultSet.getString(4); + String effect = resultSet.getString(5); + list.add(new Potion( + Long.parseLong(id), + name, + ingredient1, + ingredient2, + effect)); + + } + } catch (SQLException throwables) { + throw new RuntimeException(throwables); + } + return list; + } + + public Potion findById(Long id) { + return findAll() + .stream() + .filter(potion -> potion.getId().equals(id)) + .findAny() + .get(); + } + + public void update(Long id, Potion newPotionData) { + executeStatement(String.format(new StringBuilder() + .append("UPDATE potions.potionsTable") + .append("SET name = %s, ingredient1 = %s, ingredient2 = %s, effect = %s WHERE id = %s;") + .toString(), + newPotionData.getName(), + newPotionData.getIngredient1(), + newPotionData.getIngredient2(), + newPotionData.getEffect(), + id)); + } + + public void delete(Long id) { + Potion potion = findById(id); + executeStatement(String.format(new StringBuilder() + .append("DELETE FROM potions.potionsTable WHERE id = %s") + .toString(), + id)); + System.out.printf("%s has been deleted", potion.toString()); + } + + public void delete(Potion potion) { + Long id = potion.getId(); + delete(id); + } +} diff --git a/src/main/java/daos/Repository.java b/src/main/java/daos/Repository.java new file mode 100644 index 0000000..7671c5b --- /dev/null +++ b/src/main/java/daos/Repository.java @@ -0,0 +1,44 @@ +package daos; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * @author git-leon + * @version 1.0.0 + * @date 8/4/21 3:15 PM + */ +public interface Repository { + + default void executeStatement(String sqlStatement) { + try { + Statement statement = getScrollableStatement(); + statement.execute(sqlStatement); + } catch (SQLException e) { + throw new Error(e); + } + } + + default Statement getScrollableStatement() { + int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE; + int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY; + try { // scrollable statements can be iterated more than once without closing + return getConnection().createStatement(resultSetType, resultSetConcurrency); + } catch (SQLException e) { + throw new Error(e); + } + } + + default ResultSet executeQuery(String sqlQuery) { + try { + Statement statement = getScrollableStatement(); + return statement.executeQuery(sqlQuery); + } catch (SQLException e) { + throw new Error(e); + } + } + + Connection getConnection(); +} diff --git a/src/main/java/models/PotionPouch.java b/src/main/java/models/Potion.java similarity index 91% rename from src/main/java/models/PotionPouch.java rename to src/main/java/models/Potion.java index 9ebecaa..ec16b11 100644 --- a/src/main/java/models/PotionPouch.java +++ b/src/main/java/models/Potion.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -public class PotionPouch { +public class Potion { private Long id; private String name; @@ -11,9 +11,9 @@ public class PotionPouch { private String ingredient2; private String effect; - public PotionPouch() {} + public Potion() {} - public PotionPouch(Long id, String name, String ingredient1, String ingredient2, String effect) { + public Potion(Long id, String name, String ingredient1, String ingredient2, String effect) { this.id = id; this.name = name; this.ingredient1 = ingredient1; diff --git a/target/classes/MainApplication.class b/target/classes/MainApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..9e7239699f58595638cdd2f6d31ab8a2ae56fec7 GIT binary patch literal 4964 zcmbVP33wc38GdJXlbLKb4cVqm+Z0)_)ZE*aLZNM1(j=t`o3zcQg@6i^-AOX(?96s{ zwr%j>5K#^VtD<;;XH`53YLXHKycO>g@B0?TTOS`E@%?9JcPCBp@iE67-~avJ|9$WK zec%7jWB-2WVE~))k0|!w#waesO$uHl?l;SLaTHa!MZrtN`%4wvs^B&mw?|Qn8x`Ck z?k`jDas_uPxJ$t+6dV__SIT%*6tBkJ3SOh&wF>T0@VY2g;Pt}Z8>+aV_z5X^qk=an zxL3h_HFz`LB8J}@#oO?9vEVyuaRTp@@h%zfj-nO!*J2CaBQEci@jeCbkD?PN6+9r; zd{Df6K*0wUd`Q8pm^vq8z80tOkb(sTA6D=Y1rIBDB#KAzQ5heL;^X*)j8BU9Ps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kd{umPe9au1XR+25H{xtDa85iC zKR14!H}D{y*)fZCvpByJ!Z4Q8wH0)5C7o_U9IN=d8qdHQtV1)puof5KY+Ou9H_pK> zB#6#r4v3#7T!U+AQROq)8K@`WI0LFh*5|Z4r78(VcMA@$=4E_ z5bol$&eiYs@fBrpAEt4LcY*3MME-@4^iSNmi{RD}T^_hSbh)lab#s~Oi$b`9fmPV{ z0;|JW<-pd^DPT9Ly^M4Za%2x0Set8DTuW(h2+-bKro9_7pA-0K!s4-d5-zVvY-5_V z@jt#@T)Mra8SZTFn8%KjTO-2t)e+zKbu5tfvhQ6?6tD`zOpa=nOg)QcISXbLOXh6) znqZ-|tm58n# z5gFH&|4Kpf3(pVo7_~(@t&Z=U#ifs;XCAwl9G5Ske~gWHH?uy>;acWcuYd3J@1-j6 rD4C{Cj#`HpmchkM*L~5U>q*J;@dC~gZZQ>xWxTM41K0C4gd6??C5H&R literal 0 HcmV?d00001 diff --git a/target/classes/daos/PotionRepository.class b/target/classes/daos/PotionRepository.class new file mode 100644 index 0000000000000000000000000000000000000000..50a017431c64167e34d6b5d60a50c3d06afbc09a GIT binary patch literal 4701 zcmbVPiGLGm8Ga^BGif@|Qmz&dhNXoxDFkp;y5(#eY%y)2DPRGQX) zt?RAp?TWJMy0YtTxny^}bzSfK*1yA_$Melhl1UrTpTGY4ee+%K@x0IbeqWyb*UhH@ z+>5_Q5J9F1JCT*4N6>&w7}KJk31c=4Lwuf) zmnrI+EfhslbF?8>me(iD3KbmL!m!#!3G%d&&mPL<#7R$h$u@F{lwsMlAYf#L@eU$6 zEkBb;+eITk!K4LQ zDrB8l-m~QVqT?dA%kug~mpX%*t?@L2uL~xuTyk1$Uy;-1rm|XR*~-qH68^iMQ;cES zEEO~QVMEY)^$;f5(B2gJ0O((}E)Zx*IG1VoewZrQj z_@fFwhleD@jcku9Sk|NZNai2x?onNebxY_xaVT{>nO37+R*$-^%i6ZI?e0_Xd0Zyw zRpZ8{&XS@EzJM<(xPr?H9v01)MDt}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K13P9v4a(MZWYAb<|k&4!^j{zGP02! z85v^NB8a(!l`--W1U5oQTL@+wVcb9fw-CY@LEA;xdbxgppb0BgSAWIIgPxVNi2*xE zOeFC_^z41Wv$r?Kl!%U0tnPLV4!W7-8TB|~ZE<&s=Juo)u>E8;eKs<-V2nZ-C%@8+ z)C7}e9Gwiz9tNgrTyMp=UeCA?E00JT*SK=3ALCen*-YD-M`F~uqpHp=KBq{s)5PYC zPkeJle6yE{jWkNcD$p7c_lxKIK8g=ee3KAQmLwkGz+Z|EI6~cu*5nA_2C~;p?*`_D g*gu6ou@_bJ5BLlINXopGW5L|F(fYUJ9eC$|0L)^f4*&oF literal 0 HcmV?d00001 diff --git a/target/classes/daos/Repository.class b/target/classes/daos/Repository.class new file mode 100644 index 0000000000000000000000000000000000000000..33e27b4fa2bbe46c8f2cb6be623b585352599d47 GIT binary patch literal 1317 zcmZ{jT~8B16o%hvOSh%1r3Ffhpr|0_vwq+w;0?iqq(Gnqqj$?PfyJ_0w_Bx`{sC|O z3EtwBK@+|Ax0v`3)OXf(ccYk1W@qP|dCxQNnf?0X<7WU1Sk9mq)eNREtzkyPY%da+ z(=e~rYZ=_YO%=SQ;kJeafj)U6jgBiDuI|cXX}JP})%ubCQm-|2>!8+f9n(5koZ1yg ztl0ZfAYV5vxzRb^lg^I5*W^mEZX0@YS9eVH?JuU=q2j=A!S;(L_53=E0)l&oBB8DN z>WLv+u4!A0Cfq}_El{ZM>vp@gEn9ZmbZw{0P?k!8kgER=uRx9o#`AT(HO<*8cTd^&p<2ceXy0nv6zL#4a>2w9E z=lK}c+5RKV1EqK%e^-u>6t<+He4s&5wlKg=y$!qL81k{HdXNtdnpbXQaS zsugiu^R4>NZF?q=Trw@weI!th+PHJ**soOgyc^jcV1~w!%5WqvS)^LIy0&(1hm&*> z8U8rnU&00=$MFKc-XP8g7@awzDZ4JX|ezKiD z+gp}PFgR9-e_JZVzb}(fW5UQ8QgNJaD84zx$NZEhtIneEO6cKO;qGBZ6qG7o!!x#+`q+|+j1xIDzYtIuFix`-W;WTuQiC}6z uFt6yHYMh6j;Occ!O$KD4X?+pXDuHPgTnkJ~LTnNzA=Hg!4M8oCdh!>U{ussp literal 0 HcmV?d00001 diff --git a/target/classes/models/Potion.class b/target/classes/models/Potion.class new file mode 100644 index 0000000000000000000000000000000000000000..d0e85d7799550d6d867cc18d406053ba38f5d0d4 GIT binary patch literal 2337 zcmbW3ZFAd15Xb-LC)tkdrp^2)&w$+(Xj+7v7Kh%*>Z1$2G45QmJ=)s#AYi3IqUe?G6Pc9 zZO0Dk0_pjM13GWhZP9r*8n$EYop+8b@40c*mJwXjXdf7!>q66TGl}w(gKu+t>Hq zz;+$N`e8l>BvE7Y2=M={Fl6?`3U($SmhV%cw_lppS(N=!Ga$pK#+fV=wVOb(>7IM0wPSNF zN5VQSaW|=WADW7HFsCcW%sgEeundPbY7T*c1 zB)j|^vjgkEXrEgT{i~wQ z#d{{ST#NT;)JF8|IAIR;@M7E}+J4(3N(Q~GKKr2&uIw>OU!N)ZLS>QS8k3gdGRu)F zEk`Q09I4(kmLRw#NY2tsr%}+Dt-gZzLz;a+GA9?yxI?-c8sJ0Rr5QWVk!0uJC|Hh0 zjh(Cax=2-PU8JjvU1X}$nJ%3=o$VsmJ9mb@kH9E0xQPNPC}WI%;{-lI=T6Fa_3+Fn z9-S^P#*mu4T1sksxwO>ydKszl1+&EV5#~uUVefi>B6UFvDfF&4op`+kR3qm)S#z+Q zyjt%PMY(#LLe0n%CPS-h;9LYuj|;T~kn|wPY$C`amXbiygCKVaWNruuw*nVfPXx&f zf-EG0e4GrD83b7*kfk9YTrLJ#P6Wvgf-EP3tR#bE2SM%=$frX Z;AhhQFly}2`6)m&6g;5yFKCv++8b|jrJw)+ literal 0 HcmV?d00001 From d747d0a5036668136624e14d976579717452da01 Mon Sep 17 00:00:00 2001 From: jgiroso Date: Wed, 4 Aug 2021 19:58:34 -0400 Subject: [PATCH 3/4] potion tests complete. I know I should have done this earlier --- pom.xml | 6 + src/main/java/daos/DELETEME.txt | 0 src/main/java/models/DELETEME.txt | 0 src/main/java/models/Potion.java | 18 +- src/test/java/daos/DELETEME.txt | 0 src/test/java/daos/PotionRepositoryTest.java | 4 + src/test/java/models/DELETEME.txt | 0 src/test/java/models/PotionTest.java | 158 ++++++++++++++++++ target/classes/models/Potion.class | Bin 2337 -> 2021 bytes .../daos/PotionRepositoryTest.class | Bin 0 -> 295 bytes target/test-classes/models/PotionTest.class | Bin 0 -> 3592 bytes 11 files changed, 175 insertions(+), 11 deletions(-) delete mode 100644 src/main/java/daos/DELETEME.txt delete mode 100644 src/main/java/models/DELETEME.txt delete mode 100644 src/test/java/daos/DELETEME.txt create mode 100644 src/test/java/daos/PotionRepositoryTest.java delete mode 100644 src/test/java/models/DELETEME.txt create mode 100644 src/test/java/models/PotionTest.java create mode 100644 target/test-classes/daos/PotionRepositoryTest.class create mode 100644 target/test-classes/models/PotionTest.class diff --git a/pom.xml b/pom.xml index 636ce5e..5a25858 100644 --- a/pom.xml +++ b/pom.xml @@ -33,5 +33,11 @@ mysql-connector-java 8.0.18 + + junit + junit + RELEASE + test + \ No newline at end of file diff --git a/src/main/java/daos/DELETEME.txt b/src/main/java/daos/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/models/DELETEME.txt b/src/main/java/models/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/models/Potion.java b/src/main/java/models/Potion.java index ec16b11..e090d99 100644 --- a/src/main/java/models/Potion.java +++ b/src/main/java/models/Potion.java @@ -63,16 +63,12 @@ public void setEffect(String effect) { @Override public String toString() { - try { - return new ObjectMapper().writeValueAsString(this); - } catch (JsonProcessingException e) { - return "PotionPouch {" + - "ID = " + id + - ", Name = " + name + - ", First Ingredient = " + ingredient1 + - ", Second Ingredient = " + ingredient2 + - ", Effects = " + effect + - "}"; - } + return "Potion {" + + "ID = " + id + + ", Name = " + name + + ", First Ingredient = " + ingredient1 + + ", Second Ingredient = " + ingredient2 + + ", Effects = " + effect + + "}"; } } diff --git a/src/test/java/daos/DELETEME.txt b/src/test/java/daos/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/daos/PotionRepositoryTest.java b/src/test/java/daos/PotionRepositoryTest.java new file mode 100644 index 0000000..ab91c66 --- /dev/null +++ b/src/test/java/daos/PotionRepositoryTest.java @@ -0,0 +1,4 @@ +package daos; + +public class PotionRepositoryTest { +} diff --git a/src/test/java/models/DELETEME.txt b/src/test/java/models/DELETEME.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/models/PotionTest.java b/src/test/java/models/PotionTest.java new file mode 100644 index 0000000..e5a315d --- /dev/null +++ b/src/test/java/models/PotionTest.java @@ -0,0 +1,158 @@ +package models; + +import org.junit.Assert; +import org.junit.Test; + + +public class PotionTest { + + @Test + public void testConstructor() { + //given + Long expectedId = 1L; + String expectedName = "Restore Magicka"; + String expectedIngredient1 = "briar heart"; + String expectedIngredient2 = "red mountain flower"; + String expectedEffect = "magicka regen"; + //when + Potion potion = new Potion(expectedId, expectedName, expectedIngredient1, expectedIngredient2, expectedEffect); + Long actualID = potion.getId(); + String actualName = potion.getName(); + String actualIngredient1 = potion.getIngredient1(); + String actualIngredient2 = potion.getIngredient2(); + String actualEffect = potion.getEffect(); + //then + Assert.assertEquals(expectedId, actualID); + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedIngredient1, actualIngredient1); + Assert.assertEquals(expectedIngredient2, actualIngredient2); + Assert.assertEquals(expectedEffect, actualEffect); + + } + + @Test + public void testGetId() { + //given + Long expected = 3L; + //when + Potion potion = new Potion(expected, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + Long actual = potion.getId(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetId() { + //given + Long expected = 2L; + //when + Potion potion = new Potion(); + potion.setId(expected); + Long actual = potion.getId(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetName() { + //given + String expected = "Restore Magicka"; + //when + Potion potion = new Potion(1L, expected, "briar heart", "red mountain flower", "magicka regen"); + String actual = potion.getName(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetName() { + //given + String expected = "Restore Magicka"; + //when + Potion potion = new Potion(); + potion.setName(expected); + String actual = potion.getName(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetIngredient1() { + //given + String expected = "briar heart"; + //when + Potion potion = new Potion(1L, "Restore Magicka", expected, "red mountain flower", "magicka regen"); + String actual = potion.getIngredient1(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetIngredient1() { + //given + String expected = "briar heart"; + //when + Potion potion = new Potion(); + potion.setIngredient1(expected); + String actual = potion.getIngredient1(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetIngredient2() { + //given + String expected = "red mountain flower"; + //when + Potion potion = new Potion(1L, "Restore Magicka", "briar heart", expected, "magicka regen"); + String actual = potion.getIngredient2(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetIngredient2() { + //given + String expected = "red mountain flower"; + //when + Potion potion = new Potion(); + potion.setIngredient2(expected); + String actual = potion.getIngredient2(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetEffect() { + //given + String expected = "magicka regen"; + //when + Potion potion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", expected); + String actual = potion.getEffect(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetEffect() { + //given + String expected = "magicka regen"; + //when + Potion potion = new Potion(); + potion.setEffect(expected); + String actual = potion.getEffect(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testToString() { + //given + String expected = "Potion {ID = 1, Name = Restore Magicka, First Ingredient = briar heart, Second Ingredient = red mountain flower, Effects = magicka regen}"; + //when + Potion potion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + String actual = potion.toString(); + //then + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/target/classes/models/Potion.class b/target/classes/models/Potion.class index d0e85d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_pjM13GWhZP9r*8n$EYop+8b@40c*mJwXjXdf7!>q66TGl}w(gKu+t>Hq zz;+$N`e8l>BvE7Y2=M={Fl6?`3U($SmhV%cw_lppS(N=!Ga$pK#+fV=wVOb(>7IM0wPSNF zN5VQSaW|=WADW7HFsCcW%sgEeundPbY7T*c1 zB)j|^vjgkEXrEgT{i~wQ z#d{{ST#NT;)JF8|IAIR;@M7E}+J4(3N(Q~GKKr2&uIw>OU!N)ZLS>QS8k3gdGRu)F zEk`Q09I4(kmLRw#NY2tsr%}+Dt-gZzLz;a+GA9?yxI?-c8sJ0Rr5QWVk!0uJC|Hh0 zjh(Cax=2-PU8JjvU1X}$nJ%3=o$VsmJ9mb@kH9E0xQPNPC}WI%;{-lI=T6Fa_3+Fn z9-S^P#*mu4T1sksxwO>ydKszl1+&EV5#~uUVefi>B6UFvDfF&4op`+kR3qm)S#z+Q zyjt%PMY(#LLe0n%CPS-h;9LYuj|;T~kn|wPY$C`amXbiygCKVaWNruuw*nVfPXx&f zf-EG0e4GrD83b7*kfk9YTrLJ#P6Wvgf-EP3tR#bE2SM%=$frX Z;AhhQFly}2`6)m&6g;5yFKCv++8b|jrJw)+ diff --git a/target/test-classes/daos/PotionRepositoryTest.class b/target/test-classes/daos/PotionRepositoryTest.class new file mode 100644 index 0000000000000000000000000000000000000000..11c1a73d0f30f723a3f94c8d2941a25488816cba GIT binary patch literal 295 zcmZ{f&1%9x6ot=?`KdA1;I4Glow_g^cb0AhSD}hZ_LF32N79TWW5LJjN^sE!@S&tP z;?mv=hdCeT9+>mx^b25u&_@eh504&t1ou^y%DfR;gJD9jXF8XJUaU&_zW+*PwGnB- zq#x@{6p5%*<8RP5pQ z0cHo2$}ZOvpdSVnJXV)6_~3yxxV{~< L;j<%jSS zM@MHIM;(3R=;(*=4KO<62k=8V{?FdBX~LVdGtcgx=k`0lJKDR0cGUNzN>V=hlH3cH_a_I%3#Ma>jW(r8k+so6=y8>K>jZIS z4)WxVAn)i0kwoW61pD1Jz0~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JZsl|3PHnd?OFP=KOz%Pf&<1{15Uo Bfg%6^ literal 0 HcmV?d00001 From 3977d646e84f2002b2a0ca542d86612ce5db9f42 Mon Sep 17 00:00:00 2001 From: jgiroso Date: Wed, 4 Aug 2021 21:54:22 -0400 Subject: [PATCH 4/4] lab complete --- src/main/java/daos/PotionRepository.java | 4 +- src/main/java/{ => main}/MainApplication.java | 35 +----- src/main/java/main/SQLConnect.java | 34 +++++ src/test/java/daos/PotionRepositoryTest.java | 119 ++++++++++++++++++ target/classes/MainApplication.class | Bin 4964 -> 0 bytes target/classes/daos/PotionRepository.class | Bin 4701 -> 4710 bytes target/classes/main/MainApplication.class | Bin 0 -> 3960 bytes target/classes/main/SQLConnect.class | Bin 0 -> 1681 bytes .../daos/PotionRepositoryTest.class | Bin 295 -> 3452 bytes 9 files changed, 159 insertions(+), 33 deletions(-) rename src/main/java/{ => main}/MainApplication.java (74%) create mode 100644 src/main/java/main/SQLConnect.java delete mode 100644 target/classes/MainApplication.class create mode 100644 target/classes/main/MainApplication.class create mode 100644 target/classes/main/SQLConnect.class diff --git a/src/main/java/daos/PotionRepository.java b/src/main/java/daos/PotionRepository.java index 1faa60e..d27abe7 100644 --- a/src/main/java/daos/PotionRepository.java +++ b/src/main/java/daos/PotionRepository.java @@ -67,8 +67,8 @@ public Potion findById(Long id) { public void update(Long id, Potion newPotionData) { executeStatement(String.format(new StringBuilder() - .append("UPDATE potions.potionsTable") - .append("SET name = %s, ingredient1 = %s, ingredient2 = %s, effect = %s WHERE id = %s;") + .append("UPDATE potions.potionsTable ") + .append("SET name = '%s', ingredient1 = '%s', ingredient2 = '%s', effect = '%s' WHERE id = %s;") .toString(), newPotionData.getName(), newPotionData.getIngredient1(), diff --git a/src/main/java/MainApplication.java b/src/main/java/main/MainApplication.java similarity index 74% rename from src/main/java/MainApplication.java rename to src/main/java/main/MainApplication.java index 1dc92a6..064785b 100644 --- a/src/main/java/MainApplication.java +++ b/src/main/java/main/MainApplication.java @@ -1,10 +1,9 @@ +package main; -import com.mysql.cj.jdbc.Driver; import daos.PotionRepository; import models.Potion; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -18,8 +17,8 @@ public class MainApplication { public static void main(String[] args) { - registerJDBCDriver(); - Connection mysqlDbConnection = getConnection("mysql"); + SQLConnect.registerJDBCDriver(); + Connection mysqlDbConnection = SQLConnect.getConnection("mysql"); PotionRepository pokemonRepository = new PotionRepository(mysqlDbConnection); executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS potions;"); executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS potions;"); @@ -83,30 +82,4 @@ static Statement getScrollableStatement(Connection connection) { throw new Error(e); } } - - static Connection getConnection(String dbVendor) { - String username = "jen"; - String password = "zipcode0"; - String url = new StringBuilder() - .append("jdbc:") - .append(dbVendor) - .append("://127.0.0.1/") - .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") - .toString(); - try { - return DriverManager.getConnection(url, username, password); - } catch (SQLException e) { - throw new Error(e); - } - } - - static void registerJDBCDriver() { - // Attempt to register JDBC Driver - try { - DriverManager.registerDriver(Driver.class.newInstance()); - } catch (InstantiationException | IllegalAccessException | SQLException e1) { - throw new RuntimeException(e1); - } - } - -} +} \ No newline at end of file diff --git a/src/main/java/main/SQLConnect.java b/src/main/java/main/SQLConnect.java new file mode 100644 index 0000000..c5748d4 --- /dev/null +++ b/src/main/java/main/SQLConnect.java @@ -0,0 +1,34 @@ +package main; + +import com.mysql.cj.jdbc.Driver; + +import java.sql.*; +import java.util.StringJoiner; + +public class SQLConnect { + + static void registerJDBCDriver() { + // Attempt to register JDBC Driver + try { + DriverManager.registerDriver(Driver.class.newInstance()); + } catch (InstantiationException | IllegalAccessException | SQLException e1) { + throw new RuntimeException(e1); + } + } + + public static Connection getConnection(String dbVendor) { + String username = "jen"; + String password = "zipcode0"; + String url = new StringBuilder() + .append("jdbc:") + .append(dbVendor) + .append("://127.0.0.1/") + .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") + .toString(); + try { + return DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + throw new Error(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/daos/PotionRepositoryTest.java b/src/test/java/daos/PotionRepositoryTest.java index ab91c66..4351639 100644 --- a/src/test/java/daos/PotionRepositoryTest.java +++ b/src/test/java/daos/PotionRepositoryTest.java @@ -1,4 +1,123 @@ package daos; +import models.Potion; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.sql.Connection; +import java.util.ArrayList; +import java.util.List; + + public class PotionRepositoryTest { + + + @Test + public void testCreate() { + //given + Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + String expected = potion.toString(); + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion); + Potion potion1 = potionRepository.findById(1L); + String actual = potion1.toString(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + } + + @Test + public void testFindAll() { + //given + Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + List list = new ArrayList<>(); + list.add(potion1); + list.add(potion2); + String expected = list.toString(); + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion1); + potionRepository.create(potion2); + String actual = potionRepository.findAll().toString(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + potionRepository.delete(2L); + } + + @Test + public void testFindById() { + //given + Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + String expected = potion.toString(); + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion); + Potion potion1 = potionRepository.findById(1L); + String actual = potion1.toString(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + + } + + @Test + public void testUpdate() { + //given + Potion potion = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + Potion newPotion = new Potion(1L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + String expected = newPotion.toString(); + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion); + potionRepository.update(1L, newPotion); + Potion potion1 = potionRepository.findById(1L); + String actual = potion1.toString(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + } + + @Test + public void testDeleteByID() { + //given + Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + int expected = 1; + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion1); + potionRepository.create(potion2); + potionRepository.delete(2L); + int actual = potionRepository.findAll().size(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + } + + @Test + public void testDeleteByObject() { + //given + Potion potion1 = new Potion(1L, "Restore Health", "blue mountain flower", "daedra heart", "health regen"); + Potion potion2 = new Potion(2L, "Restore Magicka", "briar heart", "red mountain flower", "magicka regen"); + int expected = 1; + //when + Connection connection = main.SQLConnect.getConnection("mysql"); + PotionRepository potionRepository = new PotionRepository(connection); + potionRepository.create(potion1); + potionRepository.create(potion2); + potionRepository.delete(potion2); + int actual = potionRepository.findAll().size(); + //then + Assert.assertEquals(expected, actual); + potionRepository.delete(1L); + } } diff --git a/target/classes/MainApplication.class b/target/classes/MainApplication.class deleted file mode 100644 index 9e7239699f58595638cdd2f6d31ab8a2ae56fec7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4964 zcmbVP33wc38GdJXlbLKb4cVqm+Z0)_)ZE*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qOn|+IqGD7I$e< z_kqz~8GjU2`6mghGFC3>mT59GofIA=yK|Z3g zCihOz&6dI76fw*RR|j2iWlQ(2|4$-zylS>D<_$BrCAFxDxF@9m=;;~m80^Dgt4Qem z6OnEu_lZLQW7gChos}naGuvYr!k!#_N~OT*eZ9wJ0HReb1EnC` zW*Rt@c%8CR+e50UPKr)B6m-tF{=ei`0QyQf((gde?lpKA;hte?N4>_85od`~qPXPv zq_a*+hm_I*rP24R;xhTex%`yb<9_fckl5h|dA*$PN!}&itJ>xuJs`e^km9x46~-~d z=a{E}VeI8w4fbI_C7diZKtY(Y+vgFAhvyMVEudu`FIME@z2LgSf=SWjQaiXryk(0#=N1(#m*Kd{umPe9au1XR+25H{xtDa85iC zKR14!H}D{y*)fZCvpByJ!Z4Q8wH0)5C7o_U9IN=d8qdHQtV1)puof5KY+Ou9H_pK> zB#6#r4v3#7T!U+AQROq)8K@`WI0LFh*5|Z4r78(VcMA@$=4E_ z5bol$&eiYs@fBrpAEt4LcY*3MME-@4^iSNmi{RD}T^_hSbh)lab#s~Oi$b`9fmPV{ z0;|JW<-pd^DPT9Ly^M4Za%2x0Set8DTuW(h2+-bKro9_7pA-0K!s4-d5-zVvY-5_V z@jt#@T)Mra8SZTFn8%KjTO-2t)e+zKbu5tfvhQ6?6tD`zOpa=nOg)QcISXbLOXh6) znqZ-|tm58n# z5gFH&|4Kpf3(pVo7_~(@t&Z=U#ifs;XCAwl9G5Ske~gWHH?uy>;acWcuYd3J@1-j6 rD4C{Cj#`HpmchkM*L~5U>q*J;@dC~gZZQ>xWxTM41K0C4gd6??C5H&R diff --git a/target/classes/daos/PotionRepository.class b/target/classes/daos/PotionRepository.class index 50a017431c64167e34d6b5d60a50c3d06afbc09a..e6889090513a673b2ad5374ab566961bd174949a 100644 GIT binary patch delta 98 zcmcbs@=Rp|BOAL+Xn>1ji0kAAHZe~|hR|Tw5QV(N+*Acy1$EV8bsdGwy!4{fl+4t; Y5<@IvMsTszw6xUZ5-4|b9NT|x0EiGA*8l(j delta 106 zcmaE+a#v*oBU`L=Xn>1jh^s1|nF@lPurlqAOmw-8&)7k!W0|09eBFg{( diff --git a/target/classes/main/MainApplication.class b/target/classes/main/MainApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..fa1bd0dfaa3f88250891a2c27d41846304ddc207 GIT binary patch literal 3960 zcmbVP`Fk5z6+I)#p0PZ(BiXSNJ8dUjW!a9~G;Pzw33yBEATP1pv~GYhmd5tP(u^`1 zB{5q|DTStmmOuh!YgofhQou_M4f|5~Q}{l9=i{TCH=|`7JN!WM<<026`|i8k@3+eK8BAgcviu43O=FW`6zbclOo;=b=*+=2`TuLf)^EhTES-;@DeVF z>6fE;1)migey$O(;x!qcmvJ$QZhWB;kKl`9xFqAUf-gnUhc7F5UF`W4@#CuszNX;o z3cewhz9HjEBfg1m$@sR6??|X0FfG$PDj}TcJ}DtGY-bG#Eh*D7CQA7^!oB7UJLeOpQm6H^dVfy07W&h! zV_FM`z1?(Yf$LkhnU)aCFBi|`M&^cX%Q7+|qJ*7(9NPP9W^NQK*h@yA;nu3HT#?35^>jjSkDuMMBh^XtQ zL^wMF_0;kdc(=@(PSG8Ngc! z+lz6_5LZEvkPM_1Aa84pB2$$p1lG1M7;ZY_*twigUtpqV`=H$ctOGPORTH0CE|8M8 z>Tf2LDLIZ|WtL@pS3)#xmz<1o%oN&hyGhymgqT$v#(<3Pskn*(33Z-YReT>mpiH+_ zo>=ii3EIf?$)}j%lM$jiqPOTESCl@vw>y;u#6|4o{B`&WvvIoS2-s z*_&8B!7!D@r2polHdHcmS;LWVe>oy|PI((|4Y#ix#}ijV#gFh~33r-VjRMp3l50O> zS{cU^yGGUJb!S;yGM0OJh%G&DXs&VI)hwI;O1T`ZojmG}ku`ZZ9{69A`?pNC8}su# z_M7e91m`FCDNjM3eJWQu-75x{0;4KL(jR3eU+~0K;*IMIW@d>erg4rf(5WnJWpP`h zH|2eAKc0+Vs#w9QiZ!gucuU0wex*XkoQz+qIF7fe7>_j3*V2LB!2N4vjyjI*h}>?d z_zixm;&=Exd#mSAa!s!E8tZYJt(z4}QQ`XsLN#rlBORibYezIe=^s`634dnrW1<~s z{^92wk8Rp(2SHQIjA#d1#%mg0fWP3cybr1g6CuA9^5Cd=2Y(~z+ua9@UMpsD?lj4z zYN}ysErE%7>xo3!$nD6CS#%BO_{h-kh-01=B{s1u1ebM}o57}Qrq&5+uI$LWZqYc{ zd+PN=H>U-@>NdKP4_6)@yxr_zbC~qL4E4c0OqVA-PGbP>{ez-l` zt*YrGtY@jJ{kIO`=fCAJ3uw#t=}jnA3TO5$K9>QAb|uCtMR<*+dJ$qTza$TN-<$Q2 z*d+!y9^vnPJ|#ZulBPE7fUfH2-EiR8ba~# z8X~C;)Xny+qJF$5F0Vo9i8pv-RE!&FoLz+)Z(2cfyhRK<#1IohTnw!oR?tS@-VN-W z<)Zd@M|@X&cf4~IT`Sn*ncHxin7AXZ#rMYV^e5iLUA-%~dj_JDGGl z;^@FG{=FT$aW^`#4_$Z=dvFl9v6$Og)*W~Z8k_T8jB|bpcVU5^&*2_izeALCQJoxn6Bruk0x#%0WK z#S=K`CnHUHF@P)Kf#f=RB)pCb$;di-Ll{ridC!Stz4ts6A)XuPn~m>VMSq31`^gST zcz_J8V1LE>AgzR6@D{Khprvn%^&whzN2+GB^@1c_eYbcOO%zVI5Ki*Obq|$`xKu^u zC|X$40simiAyWDQtO+}+g>^D1P)T)EQ4`g(gH*<-qfTmQEA6oW?Oj#c zyYQhZ?J`1B<;?h{QO@ilGZU)niiQ6%5DWkFNV03sIu3?$Z9-t)R~Dke-DB0v+L;q5 zwg`o$aIYA}73V&ctm5Tdgt{xaL@2?G2cSZzvwRDAu*D#~%K%{+hQwYYbSYt;3qx4= EFN*v1V*mgE literal 0 HcmV?d00001 diff --git a/target/classes/main/SQLConnect.class b/target/classes/main/SQLConnect.class new file mode 100644 index 0000000000000000000000000000000000000000..d5d2bbccb93e213f353ce476b52123ab52ef3a91 GIT binary patch literal 1681 zcmZuxU0WMP7(EjLyM%;5AP}lmsuUp<77#_Dw3g6n+dyptD3_bfG;G}L#?7X*{X^c` z8|Wj?W4-Pl@ZYFsviXz}GN0!?bKW!W4x`r=foP4F>>lilijfQVE zY-!lma65)!?C7|od5KXQ^Wfz?16^wV@P91!w(uB3Jl%0T|2lV zFtD(AED+6kB`J{1+paulREyF-G>Z;bQhCoZonzCt)x5PB4NmR4z<9oD+OAP}n$LNz zE3IIgcwd(7dLaErd%L+k-+m!|frwlaNari&3)65+w`|;hX~|k(doCjqg}}6a**9xV zat)8TRF*-Dk{E%-g`U_#;M;C_dof(9|LPcBDCq=6Pj~kyuJ_j@~TGlmC|Na zj7q6!8KD&#@^S3rfRRlz6r#QJB#s~PRA9Di(?ho&m~LR3%BoHp{Se2IWz)H5S+ZX5 zfuuY9y$%Rm>#{#rTTz_rXEY`#--Ry>h-tRVSr=;$6P^GpNM*)Yt zv@OF}Ti;w+UV!f_lo@l)H-u)2I-U2yV$)hRI6&E!e3!h#i; z=q`1P=mUl+_wnZ(R))ZcE6>6kvRDd}3$2Q5N(kv$E>lhZ?mh3Du31*zjkX_3`;knx zioA>6RxdNtTlFWLgS}usn7x2_!$AZevmI(K>bTBvJOtnqe9D=Q*-r4w zCK)i3ikxHMFGSyB@HZqLUt;)#e6e?L2mnSfO2V+$TD{I4b9F<)vG1G5Pye^b+Zlndk+k-jQYwSu8QNP}2(40F#`hse6iwrZGWP8Txje$;$n{?x~zI5dRVq)v(g} OhtQfgv&#Jl*8T%`ik1if literal 0 HcmV?d00001 diff --git a/target/test-classes/daos/PotionRepositoryTest.class b/target/test-classes/daos/PotionRepositoryTest.class index 11c1a73d0f30f723a3f94c8d2941a25488816cba..dd32fa3c342bbbd8dd897a5e2adb0b01ecb0aa31 100644 GIT binary patch literal 3452 zcmchZYgZFj6o%h{WG2QzFn~B-@g5M2(%LF0%0&toKrUiItq#c$29p_>Oa$!BUi{Sl znSKGSYxM{8FZ923^*v`MA#(Z9uC`>Ynar7e_T|~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