From 274499ba7a5b2e15908a471394a2b043c7277862 Mon Sep 17 00:00:00 2001 From: razborovdb <104690270+razborovdb@users.noreply.github.com> Date: Tue, 1 Nov 2022 15:23:18 -0400 Subject: [PATCH] BL-897 created migrate and seed for inboxes-table --- .gitignore | 3 + build.gradle | 5 + .../entities/Conversations.java | 58 +++++++++ .../coderheroesbea/entities/Messages.java | 116 +++++++++++++++++ .../coderheroesbea/entities/Profiles.java | 118 ++++++++++++++++++ .../coderheroesbea/entities/Roles.java | 55 ++++++++ .../repositories/ConversationsRepository.java | 13 ++ .../repositories/MessagesRepository.java | 11 ++ .../repositories/ProfilesRepository.java | 12 ++ .../repositories/RolesRepository.java | 11 ++ src/main/resources/data.sql | 63 ++++++++++ src/main/resources/schema.sql | 81 ++++++++++++ .../MigrationSeedConversationsTest.java | 52 ++++++++ .../MigrationSeedMessagesTest.java | 68 ++++++++++ .../MigrationSeedProfilesTest.java | 72 +++++++++++ .../migrationseed/MigrationSeedRolesTest.java | 49 ++++++++ 16 files changed, 787 insertions(+) create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/entities/Conversations.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/entities/Messages.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/entities/Profiles.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/entities/Roles.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ConversationsRepository.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/repositories/MessagesRepository.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ProfilesRepository.java create mode 100644 src/main/java/com/bloomtechlabs/coderheroesbea/repositories/RolesRepository.java create mode 100644 src/main/resources/data.sql create mode 100644 src/main/resources/schema.sql create mode 100644 src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedConversationsTest.java create mode 100644 src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedMessagesTest.java create mode 100644 src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedProfilesTest.java create mode 100644 src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedRolesTest.java diff --git a/.gitignore b/.gitignore index c2065bc..0be8b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ out/ ### VS Code ### .vscode/ + +### application properties ### +application-dev.properties \ No newline at end of file diff --git a/build.gradle b/build.gradle index cef8dc2..9bf57c8 100644 --- a/build.gradle +++ b/build.gradle @@ -19,9 +19,14 @@ repositories { } dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' + runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Conversations.java b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Conversations.java new file mode 100644 index 0000000..14feccc --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Conversations.java @@ -0,0 +1,58 @@ +package com.bloomtechlabs.coderheroesbea.entities; + +import javax.persistence.*; + +@Entity +@Table(name = "conversations") + +public class Conversations { + private int conversation_id; + private Profiles profile; + + /** + * Constructor. + * Default constructor is required to have Hibernate initialize the entity. + */ + public Conversations() { + + } + + /** + * Constructor with conversation_id. + * @param conversation_id - primary key (auto-increments, generated by database) + */ + public Conversations(int conversation_id) { + this.conversation_id = conversation_id; + } + + /** + * Constructor with all fields. + * @param conversation_id - primary key (auto-increments, generated by database) + * @param profile - foreign key (table 'profiles') + */ + public Conversations(int conversation_id, Profiles profile) { + this.conversation_id = conversation_id; + this.profile = profile; + } + + @Id + @Column(name = "conversation_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getConversation_id() { + return conversation_id; + } + + public void setConversation_id(int conversation_id) { + this.conversation_id = conversation_id; + } + + @ManyToOne(cascade = CascadeType.REMOVE) + @JoinColumn(name = "profile_id", nullable = false) + public Profiles getProfile() { + return profile; + } + + public void setProfile(Profiles profile) { + this.profile = profile; + } +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Messages.java b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Messages.java new file mode 100644 index 0000000..692f9ee --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Messages.java @@ -0,0 +1,116 @@ +package com.bloomtechlabs.coderheroesbea.entities; + +import javax.persistence.*; +import java.util.Date; + +@Entity +@Table(name = "messages") +public class Messages { + private int messages_id; + private Date sent_at; + private String title; + private boolean read; + private String message; + private Profiles profile; + private Conversations conversation; + + /** + * Constructor. + * Default constructor is required to have Hibernate initialize the entity. + */ + public Messages() { + + } + + /** + * Constructor with messages_id + * @param messages_id - primary key, auto-increments, generated by database + */ + public Messages(int messages_id) { + this.messages_id = messages_id; + } + + /** + * Constructor with all fields. + * @param messages_id - primary key (auto-increments, generated by database) + * @param sent_at - timestamp (auto-generated) + * @param title - string (not nullable) + * @param read - boolean (default - false) + * @param message - string (not nullable) + * @param profile - foreign key (table 'profiles') + * @param conversation - foreign key (table 'conversations') + */ + public Messages(int messages_id, Date sent_at, String title, boolean read, String message, Profiles profile, + Conversations conversation) { + this.messages_id = messages_id; + this.sent_at = sent_at; + this.title = title; + this.read = read; + this.message = message; + this.profile = profile; + this.conversation = conversation; + } + @Id + @Column(name = "messages_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getMessages_id() { + return messages_id; + } + + public void setMessages_id(int messages_id) { + this.messages_id = messages_id; + } + + @Column(name = "sent_at", columnDefinition = "timestamp default CURRENT_TIMESTAMP") + public Date getSent_at() { + return sent_at; + } + + public void setSent_at(Date sent_at) { + this.sent_at = sent_at; + } + + @Column(name = "title", nullable = false) + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + @Column(name = "read", columnDefinition = "boolean default false") + public boolean getRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } + + @Column(name = "message", columnDefinition = "text", nullable = false) + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + @ManyToOne(cascade = CascadeType.REMOVE) + @JoinColumn(name ="sent_by_profile_id", referencedColumnName = "profile_id", nullable = false) + public Profiles getProfile() { + return profile; + } + + public void setProfile(Profiles profile) { + this.profile = profile; + } + @ManyToOne(cascade = CascadeType.REMOVE) + @JoinColumn(name ="conversation_id", referencedColumnName = "conversation_id", nullable = false) + public Conversations getConversation() { + return conversation; + } + + public void setConversation(Conversations conversation) { + this.conversation = conversation; + } +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Profiles.java b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Profiles.java new file mode 100644 index 0000000..6c9045a --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Profiles.java @@ -0,0 +1,118 @@ +package com.bloomtechlabs.coderheroesbea.entities; + +import javax.persistence.*; + +@Entity +@Table(name = "profiles") +public class Profiles { + private int profile_id; + private String email; + private String name; + private String okta_id; + private Roles role; + private String avatarUrl; + private String pending; + + /** + * Constructor. + * Default constructor is required to have Hibernate initialize the entity. + */ + public Profiles() { + + } + + /** + * Constructor with profile_id. + * @param profile_id - primary key (auto-increments, generated by database) + */ + public Profiles(int profile_id) { + this.profile_id = profile_id; + } + + /** + * Constructor with all fields. + * @param profile_id - primary key (auto-increments, generated by database) + * @param email - string + * @param name - string + * @param okta_id - string (unique) + * @param role - foreign key (table 'roles') + * @param avatarUrl - string (defaults to: 'https://i.stack.imgur.com/frlIf.png') + * @param pending - string + */ + public Profiles(int profile_id, String email, String name, String okta_id, Roles role, + String avatarUrl, String pending) { + this.profile_id = profile_id; + this.email = email; + this.name = name; + this.okta_id = okta_id; + this.role = role; + this.avatarUrl = avatarUrl; + this.pending = pending; + } + + @Id + @Column(name = "profile_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getProfile_id() { + return profile_id; + } + + public void setProfile_id(int profile_id) { + this.profile_id = profile_id; + } + + @Column(name = "email") + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Column(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Column(name = "okta_id", unique = true) + public String getOkta_id() { + return okta_id; + } + + public void setOkta_id(String okta_id) { + this.okta_id = okta_id; + } + + @ManyToOne(cascade = CascadeType.REMOVE) + @JoinColumn(name ="role_id", nullable = false) + public Roles getRole() { + return role; + } + + public void setRole(Roles role) { + this.role = role; + } + + @Column(name = "avatarurl", columnDefinition = "varchar(255) default 'https://i.stack.imgur.com/frlIf.png'") + public String getAvatarUrl() { + return avatarUrl; + } + + public void setAvatarUrl(String avatarUrl) { + this.avatarUrl = avatarUrl; + } + + @Column(name = "pending") + public String getPending() { + return pending; + } + + public void setPending(String pending) { + this.pending = pending; + } +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Roles.java b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Roles.java new file mode 100644 index 0000000..88edd2c --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/entities/Roles.java @@ -0,0 +1,55 @@ +package com.bloomtechlabs.coderheroesbea.entities; + +import javax.persistence.*; + +@Entity +@Table(name = "roles") +public class Roles { + private int role_id; + private String role_name; + + /** + * Constructor. + * Default constructor is required to have Hibernate initialize the entity. + */ + public Roles() { + + } + + /** + * Constructor with role_id. + * @param role_id - primary key (auto-increments, generated by database) + */ + public Roles(int role_id) { + this.role_id = role_id; + } + + /** + * Constructor with all fields. + * @param role_id - primary key (auto-increments, generated by database) + * @param role_name - string (not nullable, unique) + */ + public Roles(int role_id, String role_name) { + this.role_id = role_id; + this.role_name = role_name; + } + + @Id + @Column(name = "role_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getRole_id() { + return role_id; + } + + public void setRole_id(int role_id) { + this.role_id = role_id; + } + @Column(name = "role_name", nullable = false, unique = true) + public String getRole_name() { + return role_name; + } + + public void setRole_name(String role_name) { + this.role_name = role_name; + } +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ConversationsRepository.java b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ConversationsRepository.java new file mode 100644 index 0000000..146acc3 --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ConversationsRepository.java @@ -0,0 +1,13 @@ +package com.bloomtechlabs.coderheroesbea.repositories; + +import com.bloomtechlabs.coderheroesbea.entities.Conversations; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Provides data exchange with the table 'conversations' + */ +@Repository +public interface ConversationsRepository extends JpaRepository { + +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/MessagesRepository.java b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/MessagesRepository.java new file mode 100644 index 0000000..4d2efe6 --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/MessagesRepository.java @@ -0,0 +1,11 @@ +package com.bloomtechlabs.coderheroesbea.repositories; + +import com.bloomtechlabs.coderheroesbea.entities.Messages; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +/** + * Provides data exchange with the table 'messages' + */ +@Repository +public interface MessagesRepository extends JpaRepository { +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ProfilesRepository.java b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ProfilesRepository.java new file mode 100644 index 0000000..011ca9a --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/ProfilesRepository.java @@ -0,0 +1,12 @@ +package com.bloomtechlabs.coderheroesbea.repositories; + +import com.bloomtechlabs.coderheroesbea.entities.Profiles; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +/** + * Provides data exchange with the table 'profiles' + */ +@Repository +public interface ProfilesRepository extends JpaRepository { + +} diff --git a/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/RolesRepository.java b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/RolesRepository.java new file mode 100644 index 0000000..a215d30 --- /dev/null +++ b/src/main/java/com/bloomtechlabs/coderheroesbea/repositories/RolesRepository.java @@ -0,0 +1,11 @@ +package com.bloomtechlabs.coderheroesbea.repositories; + +import com.bloomtechlabs.coderheroesbea.entities.Roles; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +/** + * Provides data exchange with the table 'roles' + */ +@Repository +public interface RolesRepository extends JpaRepository { +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..49aec50 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,63 @@ +insert into roles (role_name) values ('super_admin'); +insert into roles (role_name) values ('admin'); +insert into roles (role_name) values ('instructor'); +insert into roles (role_name) values ('parent'); +insert into roles (role_name) values ('child'); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama001@maildrop.cc', 'Test001 User', '00ulthapbErVUwVJy4x6', 1, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama002@maildrop.cc', 'Test002 User', '00ultwew80Onb2vOT4x6', 2, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama003@maildrop.cc', 'Test003 User', '00ultx74kMUmEW8054x6', 3, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama004@maildrop.cc', 'Test004 User', '00ultwqjtqt4VCcS24x6', 4, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama005@maildrop.cc', 'Test005 User', '00ultwz1n9ORpNFc04x6', 5, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama006@maildrop.cc', 'Test006 User', '00u13omswyZM1xVya4x7', 1, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama007@maildrop.cc', 'Test007 User', '00u13ol5x1kmKxVJU4x7', 2, false); + +insert into profiles(email, name, okta_id, role_id, pending) + values('llama008@maildrop.cc', 'Test008 User', '00u13oned0U8XP8Mb4x7', 3, false); + + +insert into conversations (profile_id) values (1); + +insert into conversations (profile_id) values (2); + +insert into conversations (profile_id) values (3); + +insert into conversations (profile_id) values (4); + +insert into conversations (profile_id) values (5); + +insert into conversations (profile_id) values (6); + +insert into conversations (profile_id) values (7); + +insert into conversations (profile_id) values (8); + + +insert into messages (title, read, message, conversation_id, sent_by_profile_id) + values ('Help with Homework?', true, 'I need the answers to the assignment please.', 7, 1); + +insert into messages (title, read, message, conversation_id, sent_by_profile_id) + values ('What''s my grade?', true, 'Hey Ms. Teacher can you tell me my grade?', 7, 5); + +insert into messages (title, message, conversation_id, sent_by_profile_id) + values ('When is class?', 'I noticed the time was funky and had to ask.', 8, 4); + +insert into messages (title, message, conversation_id, sent_by_profile_id) + values ('Is this a yoga course?', 'How is yoga and coding taught together?', 4, 1); + +insert into messages (title, message, conversation_id, sent_by_profile_id) + values ('Where is my achievement?', 'my achievement didn''t pop up when I did course.', 5, 8); + diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000..3039747 --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,81 @@ +-- MIGRATION TABLE roles +DROP TABLE IF EXISTS roles CASCADE; +DROP SEQUENCE IF EXISTS roles_role_id_seq; +CREATE SEQUENCE IF NOT EXISTS roles_role_id_seq + start 1 + increment 1; +CREATE TABLE IF NOT EXISTS roles +( + role_id integer NOT NULL DEFAULT nextval('roles_role_id_seq'::regclass), + role_name character varying(255) NOT NULL, + CONSTRAINT roles_pkey PRIMARY KEY (role_id), + CONSTRAINT roles_role_name_unique UNIQUE (role_name) +); + +-- MIGRATION TABLE profiles +DROP TABLE IF EXISTS profiles CASCADE; +DROP SEQUENCE IF EXISTS profiles_profile_id_seq; +CREATE SEQUENCE IF NOT EXISTS profiles_profile_id_seq + start 1 + increment 1; + +CREATE TABLE IF NOT EXISTS profiles +( + profile_id integer NOT NULL DEFAULT nextval('profiles_profile_id_seq'::regclass), + email character varying(255) COLLATE pg_catalog."default", + name character varying(255) COLLATE pg_catalog."default", + okta_id character varying(255) COLLATE pg_catalog."default", + role_id integer NOT NULL, + avatarUrl character varying(255) COLLATE pg_catalog."default" DEFAULT 'https://i.stack.imgur.com/frlIf.png'::character varying, + pending character varying(255) COLLATE pg_catalog."default", + CONSTRAINT profiles_pkey PRIMARY KEY (profile_id), + CONSTRAINT profiles_okta_id_unique UNIQUE (okta_id), + CONSTRAINT profiles_role_id_foreign FOREIGN KEY (role_id) + REFERENCES roles (role_id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE CASCADE +); + +-- MIGRATION TABLE profiles +DROP TABLE IF EXISTS conversations CASCADE; +DROP SEQUENCE IF EXISTS conversations_profile_id_seq; +CREATE SEQUENCE IF NOT EXISTS conversations_profile_id_seq + start 1 + increment 1; +CREATE TABLE IF NOT EXISTS conversations +( + conversation_id integer NOT NULL DEFAULT nextval('conversations_profile_id_seq'::regclass), + profile_id integer NOT NULL, + CONSTRAINT conversations_pkey PRIMARY KEY (conversation_id), + CONSTRAINT conversations_profile_id_foreign FOREIGN KEY (profile_id) + REFERENCES profiles (profile_id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE CASCADE +); + +-- MIGRATION TABLE messages +DROP TABLE IF EXISTS messages CASCADE; +DROP SEQUENCE IF EXISTS messages_profile_id_seq; +CREATE SEQUENCE IF NOT EXISTS messages_profile_id_seq + start 1 + increment 1; +CREATE TABLE IF NOT EXISTS messages +( + messages_id integer NOT NULL DEFAULT nextval('messages_profile_id_seq'::regclass), + sent_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP, + title character varying(255) COLLATE pg_catalog."default" NOT NULL, + read boolean DEFAULT false, + message text COLLATE pg_catalog."default" NOT NULL, + sent_by_profile_id integer NOT NULL, + conversation_id integer NOT NULL, + CONSTRAINT messages_pkey PRIMARY KEY (messages_id), + CONSTRAINT messages_conversation_id_foreign FOREIGN KEY (conversation_id) + REFERENCES conversations (conversation_id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE CASCADE, + CONSTRAINT messages_sent_by_profile_id_foreign FOREIGN KEY (sent_by_profile_id) + REFERENCES profiles (profile_id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE CASCADE +); + diff --git a/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedConversationsTest.java b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedConversationsTest.java new file mode 100644 index 0000000..64b6a72 --- /dev/null +++ b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedConversationsTest.java @@ -0,0 +1,52 @@ +package com.bloomtechlabs.coderheroesbea.migrationseed; + +import com.bloomtechlabs.coderheroesbea.entities.Conversations; +import com.bloomtechlabs.coderheroesbea.entities.Profiles; +import com.bloomtechlabs.coderheroesbea.repositories.ConversationsRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class MigrationSeedConversationsTest { + @Autowired + ConversationsRepository conversationsRepository; + + @Test + public void findAllData_thenAllProfilesCorrect() { + // Given + List conversations = List.of( + new Conversations(1, new Profiles(1)), + new Conversations(2, new Profiles(2)), + new Conversations(3, new Profiles(3)), + new Conversations(4, new Profiles(4)), + new Conversations(5, new Profiles(5)), + new Conversations(6, new Profiles(6)), + new Conversations(7, new Profiles(7)), + new Conversations(8, new Profiles(8)) + ); + + // When + List foundConversations = conversationsRepository.findAll(); + + // Then + assertEquals(conversations.size(), foundConversations.size()); + for (int i = 0; i < conversations.size(); i++) { + assertTrue(compare(conversations.get(i), foundConversations.get(i))); + } + + } + + private boolean compare(Conversations conversations, Conversations foundConversations) { + return conversations.getConversation_id() == foundConversations.getConversation_id() + && conversations.getProfile().getProfile_id() == foundConversations.getProfile().getProfile_id(); + } +} diff --git a/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedMessagesTest.java b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedMessagesTest.java new file mode 100644 index 0000000..757fc80 --- /dev/null +++ b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedMessagesTest.java @@ -0,0 +1,68 @@ +package com.bloomtechlabs.coderheroesbea.migrationseed; + +import com.bloomtechlabs.coderheroesbea.entities.Conversations; +import com.bloomtechlabs.coderheroesbea.entities.Messages; +import com.bloomtechlabs.coderheroesbea.entities.Profiles; +import com.bloomtechlabs.coderheroesbea.repositories.MessagesRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.Date; +import java.util.List; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class MigrationSeedMessagesTest { + @Autowired + MessagesRepository messagesRepository; + + @Test + public void findAllData_thenAllProfilesCorrect() { + // Given + List messages = List.of( + new Messages(1, new Date(), "Help with Homework?", true, + "I need the answers to the assignment please.", new Profiles(1), + new Conversations(7)), + new Messages(2, new Date(), "What's my grade?", true, + "Hey Ms. Teacher can you tell me my grade?", new Profiles(5), + new Conversations(7)), + new Messages(3, new Date(), "When is class?", false, + "I noticed the time was funky and had to ask.", new Profiles(4), + new Conversations(8)), + new Messages(4, new Date(), "Is this a yoga course?", false, + "How is yoga and coding taught together?", new Profiles(1), + new Conversations(4)), + new Messages(5, new Date(), "Where is my achievement?", false, + "my achievement didn't pop up when I did course.", new Profiles(8), + new Conversations(5)) + ); + + // When + List foundMessages = messagesRepository.findAll(); + + // Then + assertEquals(messages.size(), foundMessages.size()); + for (int i = 0; i < messages.size(); i++) { + assertTrue(compare(messages.get(i), foundMessages.get(i))); + } + + } + + private boolean compare(Messages messages, Messages foundMessages) { + return messages.getMessages_id() == foundMessages.getMessages_id() + && messages.getRead() == foundMessages.getRead() + && Objects.equals(messages.getTitle(), foundMessages.getTitle()) + && Objects.equals(messages.getMessage(), foundMessages.getMessage()) + && messages.getProfile().getProfile_id() == foundMessages.getProfile().getProfile_id() + && messages.getConversation().getConversation_id() + == foundMessages.getConversation().getConversation_id(); + } + +} diff --git a/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedProfilesTest.java b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedProfilesTest.java new file mode 100644 index 0000000..b50e48a --- /dev/null +++ b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedProfilesTest.java @@ -0,0 +1,72 @@ +package com.bloomtechlabs.coderheroesbea.migrationseed; + +import com.bloomtechlabs.coderheroesbea.entities.Profiles; +import com.bloomtechlabs.coderheroesbea.entities.Roles; +import com.bloomtechlabs.coderheroesbea.repositories.ProfilesRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class MigrationSeedProfilesTest { + @Autowired + ProfilesRepository profilesRepository; + + @Test + public void findAllData_thenAllProfilesCorrect() { + // Given + List profiles = List.of( + new Profiles(1, "llama001@maildrop.cc", "Test001 User", + "00ulthapbErVUwVJy4x6", new Roles(1), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(2, "llama002@maildrop.cc", "Test002 User", + "00ultwew80Onb2vOT4x6", new Roles(2), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(3, "llama003@maildrop.cc", "Test003 User", + "00ultx74kMUmEW8054x6", new Roles(3), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(4, "llama004@maildrop.cc", "Test004 User", + "00ultwqjtqt4VCcS24x6", new Roles(4), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(5, "llama005@maildrop.cc", "Test005 User", + "00ultwz1n9ORpNFc04x6", new Roles(5), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(6, "llama006@maildrop.cc", "Test006 User", + "00u13omswyZM1xVya4x7", new Roles(1), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(7, "llama007@maildrop.cc", "Test007 User", + "00u13ol5x1kmKxVJU4x7", new Roles(2), + "https://i.stack.imgur.com/frlIf.png", "false"), + new Profiles(8, "llama008@maildrop.cc", "Test008 User", + "00u13oned0U8XP8Mb4x7", new Roles(3), + "https://i.stack.imgur.com/frlIf.png", "false") + ); + + // When + List foundProfiles = profilesRepository.findAll(); + + // Then + assertEquals(profiles.size(), foundProfiles.size()); + for (int i = 0; i < profiles.size(); i++) { + assertTrue(compare(profiles.get(i), foundProfiles.get(i))); + } + } + private boolean compare(Profiles profiles, Profiles foundProfiles) { + return profiles.getProfile_id()== foundProfiles.getProfile_id() + && Objects.equals(profiles.getEmail(), foundProfiles.getEmail()) + && Objects.equals(profiles.getName(), foundProfiles.getName()) + && Objects.equals(profiles.getOkta_id(), foundProfiles.getOkta_id()) + && profiles.getRole().getRole_id() == foundProfiles.getRole().getRole_id() + && Objects.equals(profiles.getAvatarUrl(), foundProfiles.getAvatarUrl()) + && Objects.equals(profiles.getPending(), foundProfiles.getPending()); + } +} diff --git a/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedRolesTest.java b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedRolesTest.java new file mode 100644 index 0000000..f0f306b --- /dev/null +++ b/src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedRolesTest.java @@ -0,0 +1,49 @@ +package com.bloomtechlabs.coderheroesbea.migrationseed; + +import com.bloomtechlabs.coderheroesbea.entities.Roles; +import com.bloomtechlabs.coderheroesbea.repositories.RolesRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class MigrationSeedRolesTest { + @Autowired + private RolesRepository rolesRepository; + + @Test + public void findAllData_thenAllRolesCorrect() { + // Given + List roles = List.of( + new Roles(1, "super_admin"), + new Roles(2, "admin"), + new Roles(3, "instructor"), + new Roles(4, "parent"), + new Roles(5, "child") + ); + + // When + List foundRoles = rolesRepository.findAll(); + + //Then + assertEquals(roles.size(), foundRoles.size()); + for (int i = 0; i < roles.size(); i++) { + assertTrue(compare(roles.get(i), foundRoles.get(i))); + } + + } + + private boolean compare(Roles roles, Roles foundRoles) { + return roles.getRole_id() == foundRoles.getRole_id() + && Objects.equals(roles.getRole_name(), foundRoles.getRole_name()); + } +}