Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw27test #6

Open
wants to merge 1 commit into
base: hw26
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- &lt;!&ndash; https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.example.hw23.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.sql.DataSource;


@Configuration
@EnableWebSecurity
public class MyStudyAuthenticationConfig extends WebSecurityConfigurerAdapter {

private final DataSource dataSource;

public MyStudyAuthenticationConfig(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.inMemoryAuthentication()
.withUser("Max")
.password("{noop}1111")
.authorities("ROLE_ADMIN")
.and()
.withUser("Smith")
.password("{noop}hammer")
.authorities("ROLE_USER");
}
}
//package com.example.hw23.config;
//
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//
//import javax.sql.DataSource;
//
//
//@Configuration
//@EnableWebSecurity
//public class MyStudyAuthenticationConfig extends WebSecurityConfigurerAdapter {
//
// private final DataSource dataSource;
//
// public MyStudyAuthenticationConfig(DataSource dataSource) {
// this.dataSource = dataSource;
// }
//
// @Override
// protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
// authenticationManagerBuilder
// .inMemoryAuthentication()
// .withUser("Max")
// .password("{noop}1111")
// .authorities("ROLE_ADMIN")
// .and()
// .withUser("Smith")
// .password("{noop}hammer")
// .authorities("ROLE_USER");
// }
//}
31 changes: 21 additions & 10 deletions src/main/java/com/example/hw23/service/AnimalService.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
package com.example.hw23.service;

import com.example.hw23.entity.Animal;
import com.example.hw23.repository.AnimalRepository;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;

@Service
@EnableCaching
public class AnimalService {

private final AnimalRepository animalRepository;
// private final EntityManager entityManager;
private final SessionFactory factory;

@Autowired
public AnimalService(AnimalRepository animalRepository) {
this.animalRepository = animalRepository;
public AnimalService(SessionFactory factory) {
this.factory = factory;
// this.entityManager = entityManager;
}

@Cacheable(value = "animalId")
public Animal getAnimal(int id) {
System.out.println("Cache animal`s is working");
return animalRepository.findById(id).get();
return factory.unwrap(Session.class).get(Animal.class, id);
}

public List<Animal> getAllAnimals() {
return (List<Animal>) animalRepository.findAll();
return factory.unwrap(Session.class)
.createQuery("from Animal", Animal.class).getResultList();

}

public Animal saveAnimal(Animal animal) {
return this.animalRepository.save(animal);
factory.unwrap(Session.class).save(animal);
return animal;
}

public void deleteAnimal(int id) {
animalRepository.deleteById(id);
factory.unwrap(Session.class)
.createQuery("DELETE from Animal " + "WHERE id =: animalId")
.setParameter("animalId", id)
.executeUpdate();
}

public Animal updateAnimal(Animal animal) {
return animalRepository.save(animal);
factory.unwrap(Session.class).saveOrUpdate(animal);
return animal;
}

}
26 changes: 18 additions & 8 deletions src/main/java/com/example/hw23/service/PersonService.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
package com.example.hw23.service;

import com.example.hw23.entity.Animal;
import com.example.hw23.entity.Person;
import com.example.hw23.repository.PersonRepository;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;


@Service
@EnableCaching
public class PersonService {
private final PersonRepository personRepository;
//private final PersonRepository personRepository;
private final EntityManager entityManager;

@Autowired
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
public PersonService(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Cacheable(value = "personId")
public Person getPerson(int id) {
System.out.println("cache person`s is working");
return personRepository.findById(id).get();
return entityManager.unwrap(Session.class).get(Person.class, id);
}

public List<Person> getAllPersons() {
return (List<Person>) personRepository.findAll();
return entityManager.unwrap(Session.class)
.createQuery("from Person ", Person.class).getResultList();
}

public Person savePerson(Person person) {
return this.personRepository.save(person);
entityManager.unwrap(Session.class).save(person);
return person;
}

public void deletePerson(int id) {
personRepository.deleteById(id);
entityManager.unwrap(Session.class)
.createQuery("DELETE from Person where id =:personId")
.setParameter("personId", id)
.executeUpdate();
}

public Person updatePerson(Person person) {
return personRepository.save(person);
entityManager.unwrap(Session.class).saveOrUpdate(person);
return person;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ spring.datasource.password=hr
server.port=3333
spring.main.allow-bean-definition-overriding=true

hibernate.dialect=org.hibernate.dialect.Oracle12cDialect


spring.redis.host=localhost
spring.redis.port=4444
Expand Down
124 changes: 73 additions & 51 deletions src/test/java/com/example/hw23/service/AnimalServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,95 @@
import com.example.hw23.entity.Animal;
import com.example.hw23.entity.Person;
import com.example.hw23.repository.AnimalRepository;
import org.aspectj.lang.annotation.Before;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.jupiter.api.*;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;

import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


class AnimalServiceTest {
private final AnimalRepository animalRepository = Mockito.mock(AnimalRepository.class);

private final AnimalService animalService = new AnimalService(animalRepository);

@Test
void saveAnimal() {
Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
Animal sheep = Animal.builder().animalId(200).name("sheep").ownerId(2).build();
Mockito.when(this.animalRepository.save(Mockito.any()))
.thenReturn(horse);
Assertions.assertEquals(horse, animalService.saveAnimal(sheep));
Mockito.verify(this.animalRepository).save(sheep);
}

@Test
void getAnimal() {
Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
Mockito
.when(this.animalRepository.findById(Mockito.anyInt()))
.thenReturn(Optional.of(horse));

Assertions.assertEquals(horse, animalService.getAnimal(Mockito.anyInt()));
Mockito.verify(this.animalRepository).findById(Mockito.anyInt());
}

@Test
void updateAnimal() {
Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
Animal sheep = Animal.builder().animalId(200).name("sheep").ownerId(2).build();
class AnimalServiceTest {
@InjectMocks
private final SessionFactory mockFactory = Mockito.mock(SessionFactory.class);
// private final Session mockSession = Mockito.mock(Session.class);
// private final Transaction mockTransaction = Mockito.mock(Transaction.class);

Mockito
.when(this.animalRepository.save(Mockito.any()))
.thenReturn(horse);
private final AnimalService animalService = new AnimalService(this.mockFactory);

Assertions.assertEquals(horse, this.animalService.updateAnimal(sheep));
Mockito.verify(this.animalRepository).save(sheep);
}
// @Before("")
// public void setUp() {
//// factory = Mockito.mock(SessionFactory.class);
//// mockedSession = Mockito.mock(Session.class);
//// mockedTransaction = Mockito.mock(Transaction.class);
// Mockito.when(mockFactory.openSession()).thenReturn(mockSession);
// Mockito.when(mockSession.beginTransaction()).thenReturn(mockTransaction);
// //loginDAO.setSessionFactory(this.mockedSessionFactory);
// }

@Test
void getAllAnimals() {
void saveAnimal() {
Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
Animal sheep = Animal.builder().animalId(200).name("sheep").ownerId(2).build();
Animal rabbit = Animal.builder().animalId(300).name("rabbit").ownerId(1).build();
List<Animal> animals = new ArrayList<>();
animals.add(horse);
animals.add(sheep);
animals.add(rabbit);

Mockito.when(this.animalRepository.findAll()).thenReturn(animals);
Mockito.when(this.mockFactory.unwrap(Session.class).save(Mockito.any())).thenReturn((Serializable) horse);

Assertions.assertEquals(animals, animalService.getAllAnimals());
Mockito.verify(this.animalRepository).findAll();
Assertions.assertEquals(horse, animalService.saveAnimal(sheep));
Mockito.verify(this.mockFactory).unwrap(Session.class).save(sheep);
}

@Test
void deleteAnimal() {
Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
animalService.deleteAnimal(horse.getAnimalId());
Mockito.verify(this.animalRepository).deleteById(Mockito.anyInt());
}
// @Test
// void getAnimal() {
// Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
// Mockito
// .when(this.animalRepository.findById(Mockito.anyInt()))
// .thenReturn(Optional.of(horse));
//
// Assertions.assertEquals(horse, animalService.getAnimal(Mockito.anyInt()));
// Mockito.verify(this.animalRepository).findById(Mockito.anyInt());
// }
//
// @Test
// void updateAnimal() {
// Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
// Animal sheep = Animal.builder().animalId(200).name("sheep").ownerId(2).build();
//
// Mockito
// .when(this.animalRepository.save(Mockito.any()))
// .thenReturn(horse);
//
// Assertions.assertEquals(horse, this.animalService.updateAnimal(sheep));
// Mockito.verify(this.animalRepository).save(sheep);
// }
//
// @Test
// void getAllAnimals() {
// Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
// Animal sheep = Animal.builder().animalId(200).name("sheep").ownerId(2).build();
// Animal rabbit = Animal.builder().animalId(300).name("rabbit").ownerId(1).build();
// List<Animal> animals = new ArrayList<>();
// animals.add(horse);
// animals.add(sheep);
// animals.add(rabbit);
//
// Mockito.when(this.animalRepository.findAll()).thenReturn(animals);
//
// Assertions.assertEquals(animals, animalService.getAllAnimals());
// Mockito.verify(this.animalRepository).findAll();
// }
//
// @Test
// void deleteAnimal() {
// Animal horse = Animal.builder().animalId(100).name("horse").ownerId(1).build();
// animalService.deleteAnimal(horse.getAnimalId());
// Mockito.verify(this.animalRepository).deleteById(Mockito.anyInt());
// }
}
Loading