-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from BloomTech-Labs/BL-904-bd-migrate-and-seed-…
…newsfeed-table Bl 904 bd migrate and seed newsfeed table
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/main/java/com/bloomtechlabs/coderheroesbea/entities/Newsfeed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.bloomtechlabs.coderheroesbea.entities; | ||
|
||
import javax.persistence.*; | ||
import java.util.Date; | ||
|
||
@Entity | ||
@Table(name = "newsfeed") | ||
public class Newsfeed { | ||
// newsfeed_id integer NOT NULL DEFAULT nextval('newsfeed_newsfeed_id_seq'::regclass), | ||
// title character varying(255) COLLATE pg_catalog."default" NOT NULL, | ||
// link character varying(255) COLLATE pg_catalog."default" NOT NULL, | ||
// description text COLLATE pg_catalog."default" NOT NULL, | ||
// posted_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
private int newsfeed_id; | ||
private String title; | ||
private String link; | ||
private String description; | ||
private Date posted_at; | ||
|
||
/** | ||
* Constructor. | ||
* Default constructor is required to have Hibernate initialize the entity. | ||
*/ | ||
public Newsfeed() { | ||
|
||
} | ||
|
||
/** | ||
* Constructor with newsfeed_id | ||
* @param newsfeed_id - primary key, auto-increments, generated by database | ||
*/ | ||
public Newsfeed(int newsfeed_id) { | ||
this.newsfeed_id = newsfeed_id; | ||
} | ||
|
||
/** | ||
* Constructor with all fields. | ||
* @param newsfeed_id - primary key, auto-increments, generated by database | ||
* @param title - string not null | ||
* @param link - string not null | ||
* @param description - string not null | ||
* @param posted_at - timestamp (autogenerated) | ||
*/ | ||
public Newsfeed(int newsfeed_id, String title, String link, String description, Date posted_at) { | ||
this.newsfeed_id = newsfeed_id; | ||
this.title = title; | ||
this.link = link; | ||
this.description = description; | ||
this.posted_at = posted_at; | ||
} | ||
@Id | ||
@Column(name = "newsfeed_id") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
public int getNewsfeed_id() { | ||
return newsfeed_id; | ||
} | ||
|
||
public void setNewsfeed_id(int newsfeed_id) { | ||
this.newsfeed_id = newsfeed_id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
@Column(name = "title", nullable = false) | ||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
@Column(name = "link", nullable = false) | ||
public String getLink() { | ||
return link; | ||
} | ||
|
||
public void setLink(String link) { | ||
this.link = link; | ||
} | ||
@Column(name = "description", nullable = false) | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
@Column(name = "posted_at", columnDefinition = "timestamp default CURRENT_TIMESTAMP") | ||
public Date getPosted_at() { | ||
return posted_at; | ||
} | ||
|
||
public void setPosted_at(Date posted_at) { | ||
this.posted_at = posted_at; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/bloomtechlabs/coderheroesbea/repositories/NewsfeedRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.bloomtechlabs.coderheroesbea.repositories; | ||
|
||
import com.bloomtechlabs.coderheroesbea.entities.Newsfeed; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface NewsfeedRepository extends JpaRepository<Newsfeed,Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/com/bloomtechlabs/coderheroesbea/migrationseed/MigrationSeedNewsfeedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.bloomtechlabs.coderheroesbea.migrationseed; | ||
|
||
import com.bloomtechlabs.coderheroesbea.entities.Admins; | ||
import com.bloomtechlabs.coderheroesbea.entities.Newsfeed; | ||
import com.bloomtechlabs.coderheroesbea.entities.Profiles; | ||
import com.bloomtechlabs.coderheroesbea.repositories.AdminsRepository; | ||
import com.bloomtechlabs.coderheroesbea.repositories.NewsfeedRepository; | ||
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 MigrationSeedNewsfeedTest { | ||
@Autowired | ||
NewsfeedRepository newsfeedRepository; | ||
|
||
@Test | ||
public void findAllData_thenAllAdminsCorrect() { | ||
// Given | ||
List<Newsfeed> newsfeeds = List.of( | ||
new Newsfeed(1, "Check out these coding camps!", "https://www.idtech.com/", | ||
"This is great way to learn more about the coding world!", new Date()), | ||
new Newsfeed(2, "Always check the docs!", "https://developer.mozilla.org/en-US/", | ||
"Make sure to check the documentations if you are looking for a method to use and are not sure what the precise name is!", | ||
new Date()), | ||
new Newsfeed(3, "Practice your algorithms!", "https://leetcode.com/", | ||
"Sign up and test your problem-solving skills!", new Date()), | ||
new Newsfeed(4, "Robot dog learns to walk in one hour", "https://www.sciencedaily.com/releases/2022/07/220718122229.htm", | ||
"Like a newborn animal, a four-legged robot stumbles around during its first walking attempts. But while a foal or a giraffe needs much longer to master walking, the robot learns to move forward fluently in just one hour. A computer program acts as the artificial presentation of the animal's spinal cord, and learns to optimize the robot's movement in a short time. The artificial neural network is not yet ideally adjusted at the beginning, but rapidly self-adjusts.", | ||
new Date()) | ||
); | ||
|
||
// When | ||
List<Newsfeed> foundNewsfeeds = newsfeedRepository.findAll(); | ||
|
||
// Then | ||
assertEquals(newsfeeds.size(), foundNewsfeeds.size()); | ||
for (int i = 0; i < newsfeeds.size(); i++) { | ||
assertTrue(compare(newsfeeds.get(i), foundNewsfeeds.get(i))); | ||
} | ||
|
||
} | ||
|
||
private boolean compare(Newsfeed newsfeeds, Newsfeed foundNewsfeeds) { | ||
return newsfeeds.getNewsfeed_id() == foundNewsfeeds.getNewsfeed_id() | ||
&& Objects.equals(newsfeeds.getTitle(), foundNewsfeeds.getTitle()) | ||
&& Objects.equals(newsfeeds.getLink(), foundNewsfeeds.getLink()) | ||
&& Objects.equals(newsfeeds.getDescription(), foundNewsfeeds.getDescription()) | ||
; | ||
} | ||
} |