Lab3 finish
This commit is contained in:
parent
21c085917a
commit
1423f5da0b
@ -1,6 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
<option name="id" value="jpab" />
|
||||
</component>
|
||||
</project>
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
92
src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java
Normal file
92
src/test/java/ru/ulstu/is/sbapp/JpaCommunityTest.java
Normal file
@ -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<Community> 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());
|
||||
}
|
||||
}
|
76
src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java
Normal file
76
src/test/java/ru/ulstu/is/sbapp/JpaMusicTest.java
Normal file
@ -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<Music> 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()));
|
||||
}
|
||||
}
|
118
src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java
Normal file
118
src/test/java/ru/ulstu/is/sbapp/JpaUserTest.java
Normal file
@ -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<UserModel> 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<UserModel> 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());
|
||||
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user