From 1423f5da0b659b61169ccab13b8ad93c5f9abf16 Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Fri, 12 May 2023 01:49:31 +0400 Subject: [PATCH] Lab3 finish --- .idea/misc.xml | 4 + src/main/java/SocialNetwork/model/User.java | 70 ----------- .../ru/ulstu/is/sbapp/JpaCommunityTest.java | 92 ++++++++++++++ .../java/ru/ulstu/is/sbapp/JpaMusicTest.java | 76 +++++++++++ .../java/ru/ulstu/is/sbapp/JpaUserTest.java | 118 ++++++++++++++++++ .../ulstu/is/sbapp/SbappApplicationTests.java | 2 - 6 files changed, 290 insertions(+), 72 deletions(-) delete mode 100644 src/main/java/SocialNetwork/model/User.java create mode 100644 src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java create mode 100644 src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java create mode 100644 src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java diff --git a/.idea/misc.xml b/.idea/misc.xml index dd53264..d5bf1b7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,10 @@ + + + \ No newline at end of file diff --git a/src/main/java/SocialNetwork/model/User.java b/src/main/java/SocialNetwork/model/User.java deleted file mode 100644 index 20201d5..0000000 --- a/src/main/java/SocialNetwork/model/User.java +++ /dev/null @@ -1,70 +0,0 @@ -package SocialNetwork.model; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import java.util.Objects; - - -@Entity -public class User { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - @Column() - private String firstName; - private String lastName; - - public User() { - } - - public User(String firstName, String lastName) { - this.firstName = firstName; - this.lastName = lastName; - } - - public Long getId() { - return id; - } - - 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; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - User user = (User) o; - return Objects.equals(id, user.id); - } - - @Override - public int hashCode() { - return Objects.hash(id); - } - - @Override - public String toString() { - return "Student{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - '}'; - } - -} diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java new file mode 100644 index 0000000..dcfb133 --- /dev/null +++ b/src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java @@ -0,0 +1,92 @@ +package ru.ulstu.is.sbapp; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.is.sbapp.socialNetwork.models.Community; +import ru.ulstu.is.sbapp.socialNetwork.models.Music; +import ru.ulstu.is.sbapp.socialNetwork.models.UserModel; +import ru.ulstu.is.sbapp.socialNetwork.services.CommunityService; +import ru.ulstu.is.sbapp.socialNetwork.services.UserService; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +@SpringBootTest +public class JpaCommunityTest { + private static final Logger log = LoggerFactory.getLogger(JpaUserTest.class); + @Autowired + UserService userService; + + @Autowired + CommunityService communityService; + + @Test + void testUserCreate() { + communityService.deleteAllCommunitys(); + final Community community = communityService.addCommunity("CodeGPT"); + log.info(community.toString()); + Assertions.assertNotNull(community.getId()); + } + + @Test + void testUserReadNotFound() { + communityService.deleteAllCommunitys(); + Assertions.assertThrows(EntityNotFoundException.class, () -> communityService.findCommunity(-1L)); + } + + @Test + void findUser() { + communityService.deleteAllCommunitys(); + final Community community = communityService.addCommunity("CodeGPT"); + log.info(community.toString()); + final Community findCommunity = communityService.findCommunity(community.getId()); + log.info(findCommunity.toString()); + Assertions.assertEquals(community.toString(), findCommunity.toString()); + } + + @Test + void testReadAll() { + communityService.deleteAllCommunitys(); + final Community community = communityService.addCommunity("CodeGPT"); + log.info(community.toString()); + final List communitys = communityService.findAllCommunitys(); + Assertions.assertEquals(communitys.size(), 1); + } + + @Test + void updateCommunity() { + communityService.deleteAllCommunitys(); + final Community community = communityService.addCommunity("CodeGPT 2"); + communityService.updateCommunity(community.getId(), "CodeGPT 3"); + final Community findCommunity = communityService.findCommunity(community.getId()); + log.info(findCommunity.toString()); + Assertions.assertEquals(findCommunity.getName(), "CodeGPT 3"); + } + + @Test + void deleteCommunity() { + communityService.deleteAllCommunitys(); + final Community community = communityService.addCommunity("CodeGPT"); + log.info(community.toString()); + communityService.deleteCommunity(community.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> communityService.findCommunity(community.getId())); + } + + + @Test + void addUser() { + communityService.deleteAllCommunitys(); + userService.findAllUsers(); + final Community community = communityService.addCommunity("CodeGPT"); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + + userService.addCommunity(user.getId(), community.getId()); + + final Community findCommunity = communityService.findCommunity(community.getId()); + Assertions.assertEquals(findCommunity.getUsers().get(0).getId(), user.getId()); + } +} diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java new file mode 100644 index 0000000..cac174f --- /dev/null +++ b/src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java @@ -0,0 +1,76 @@ +package ru.ulstu.is.sbapp; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.is.sbapp.socialNetwork.models.Music; +import ru.ulstu.is.sbapp.socialNetwork.services.MusicService; +import ru.ulstu.is.sbapp.socialNetwork.services.UserService; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +@SpringBootTest +public class JpaMusicTest { + private static final Logger log = LoggerFactory.getLogger(JpaUserTest.class); + @Autowired + UserService userService; + + @Autowired + MusicService musicService; + + @Test + void testMusicCreate() { + musicService.deleteAllMusics(); + final Music music = musicService.addMusic("Song"); + log.info(music.toString()); + Assertions.assertNotNull(music.getId()); + } + + @Test + void testMusicReadNotFound() { + musicService.deleteAllMusics(); + Assertions.assertThrows(EntityNotFoundException.class, () -> musicService.findMusic(-1L)); + } + + @Test + void findMusic() { + musicService.deleteAllMusics(); + final Music music = musicService.addMusic("Song"); + log.info(musicService.toString()); + final Music findMusic = musicService.findMusic(music.getId()); + log.info(findMusic.toString()); + Assertions.assertEquals(music.toString(), findMusic.toString()); + } + + @Test + void testReadAll() { + musicService.deleteAllMusics(); + final Music music = musicService.addMusic("Song"); + log.info(music.toString()); + final List musics = musicService.findAllMusics(); + Assertions.assertEquals(musics.size(), 1); + } + + @Test + void updateMusic() { + musicService.deleteAllMusics(); + final Music music = musicService.addMusic("Song"); + musicService.updateMusic(music.getId(), "Song 2"); + final Music findMusic = musicService.findMusic(music.getId()); + log.info(findMusic.toString()); + Assertions.assertEquals(findMusic.getName(), "Song 2"); + } + + @Test + void deleteMusic() { + musicService.deleteAllMusics(); + final Music music = musicService.addMusic("Song"); + log.info(music.toString()); + musicService.deleteMusic(music.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(music.getId())); + } +} diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java b/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java new file mode 100644 index 0000000..feefc84 --- /dev/null +++ b/src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java @@ -0,0 +1,118 @@ +package ru.ulstu.is.sbapp; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.is.sbapp.socialNetwork.models.Community; +import ru.ulstu.is.sbapp.socialNetwork.models.Music; +import ru.ulstu.is.sbapp.socialNetwork.models.UserModel; +import ru.ulstu.is.sbapp.socialNetwork.services.CommunityService; +import ru.ulstu.is.sbapp.socialNetwork.services.MusicService; +import ru.ulstu.is.sbapp.socialNetwork.services.UserService; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +@SpringBootTest +public class JpaUserTest { + private static final Logger log = LoggerFactory.getLogger(JpaUserTest.class); + @Autowired + UserService userService; + + @Autowired + MusicService musicService; + + @Autowired + CommunityService communityService; + + @Test + void testUserCreate() { + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + log.info(user.toString()); + Assertions.assertNotNull(user.getId()); + } + + @Test + void testUserReadNotFound() { + userService.deleteAllUsers(); + Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(-1L)); + } + + @Test + void findUser() { + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + log.info(user.toString()); + final UserModel findUser = userService.findUser(user.getId()); + log.info(findUser.toString()); + Assertions.assertEquals(user.toString(), findUser.toString()); + } + + @Test + void updateUser() { + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + userService.updateUser(user.getId(), "its_mailov 2"); + final UserModel findUser = userService.findUser(user.getId()); + log.info(findUser.toString()); + Assertions.assertEquals(findUser.getName(), "its_mailov 2"); + + } + + @Test + void testReadAll() { + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + log.info(user.toString()); + final List users = userService.findAllUsers(); + Assertions.assertEquals(users.size(), 1); + } + + @Test + void deleteUser() { + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + log.info(user.toString()); + userService.deleteUser(user.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(user.getId())); + } + + + @Test + void findUserByCity(){ + userService.deleteAllUsers(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + List users = userService.findUserByCity("Moscow"); + Assertions.assertEquals(users.size(), 1); + } + + @Test + void addMusic() { + userService.deleteAllUsers(); + musicService.deleteAllMusics(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + final Music music = musicService.addMusic("Song"); + + userService.addMusic(user.getId(), music.getId()); + + final UserModel findUser = userService.findUser(user.getId()); + Assertions.assertEquals(findUser.getMusics().get(0).toString(), music.toString()); + } + + @Test + void addGroup() { + userService.deleteAllUsers(); + musicService.deleteAllMusics(); + final UserModel user = userService.addUser("its_mailov", "Moscow"); + final Community community = communityService.addCommunity("GPT"); + + userService.addCommunity(user.getId(), community.getId()); + + final UserModel findUser = userService.findUser(user.getId()); + Assertions.assertEquals(findUser.getGroups().get(0).getId(), community.getId()); + + } +} \ No newline at end of file diff --git a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java index 261b6be..83489f8 100644 --- a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.ApplicationContext; -import ru.ulstu.is.sbapp.calculator.domain.Calculator; import ru.ulstu.is.sbapp.calculator.service.CalculatorService;