Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider ee06b2c0 rédigé par Zomzog's avatar Zomzog
Parcourir les fichiers

init

parent 385fd3ca
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 425 ajouts et 0 suppression
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Zeppelin ignored files
/ZeppelinRemoteNotebooks/
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Retails Stock" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/kotlin" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.9.25" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ScalaCompilerConfiguration">
<option name="separateProdTestSources" value="false" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>iut.nantes</groupId>
<artifactId>td1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>exo1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>1.13.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package iut.nantes
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class AppConfig {
@Bean
fun database() = ListDatabase()
@Bean
fun userService(@Qualifier("database") db: Database) = UserService(db)
@Bean
fun superUserService(@Qualifier("database") db: Database) = SuperUserService(database())
}
\ No newline at end of file
package iut.nantes
import java.util.UUID
interface Database {
/**
* Save a user in the database
*/
fun save(user: User)
/**
* Delete a user from the database
*/
fun delete(user: User)
/**
* Update a user in the database
*/
fun update(user: User)
/**
* Find a user by its id, return null if not found
*/
fun findOne(id: UUID): User?
/**
* Find all users with the given name, return all users if name is null
*/
fun findAll(name: String?): List<User>
}
\ No newline at end of file
package iut.nantes
import java.util.*
import org.springframework.stereotype.Repository
@Repository
class HashDatabase : Database {
override fun save(user: User) {
TODO("Not yet implemented")
}
override fun delete(user: User) {
TODO("Not yet implemented")
}
override fun update(user: User) {
TODO("Not yet implemented")
}
override fun findOne(id: UUID): User? {
TODO("Not yet implemented")
}
override fun findAll(name: String?): List<User> {
TODO("Not yet implemented")
}
}
\ No newline at end of file
package iut.nantes
import java.util.UUID
class ListDatabase : Database {
private val users = mutableListOf<User>()
override fun save(user: User) {
users.add(user)
}
override fun delete(user: User) {
users.remove(user)
}
override fun update(user: User) {
val index = users.indexOfFirst { it.id == user.id }
if (index != -1) {
users[index] = user
}
}
override fun findOne(id: UUID): User? {
return users.find { it.id == id }
}
override fun findAll(name: String?): List<User> {
return if (name != null) {
users.filter { it.name == name }
} else {
users
}
}
}
\ No newline at end of file
package iut.nantes
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
open class MyApp
fun main(args: Array<String>) {
runApplication<MyApp>(*args)
}
package iut.nantes
class SuperUserService(val database: Database) {
fun findAll(): List<User> {
return database.findAll(null)
}
}
\ No newline at end of file
package iut.nantes
import java.util.UUID
data class User(val id: UUID, val name: String, val email: String, val age: Int?)
\ No newline at end of file
package iut.nantes
import java.util.*
class UserService(val database: Database) {
fun save(user: User) {
database.save(user)
}
fun delete(user: User) {
database.delete(user)
}
fun update(user: User) {
TODO()
}
fun findOne(id: UUID): User? {
return database.findOne(id)
}
fun findAll(name: String?): List<User> {
TODO()
}
}
\ No newline at end of file
package iut.nantes
import assertk.assertThat
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import java.util.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import kotlin.NoSuchElementException
class Exercies {
@Test
fun exo1_1() {
val context = AnnotationConfigApplicationContext(AppConfig::class.java)
val userService = context.getBean(UserService::class.java)
userService.save(user())
val user = userService.findOne(user().id)
assertThat(user).isEqualTo(user())
}
@Test
fun exo1_2() {
val context = AnnotationConfigApplicationContext(AppConfig::class.java)
val userService = context.getBean(UserService::class.java)
val superUserService = context.getBean(SuperUserService::class.java)
userService.save(user())
assertThat(superUserService.findAll()).isEqualTo(listOf(user()))
}
@Test
fun exo1_3() {
val context = AnnotationConfigApplicationContext(AppConfig::class.java)
val userService = context.getBean(UserService::class.java)
val superUserService = context.getBean(SuperUserService::class.java)
userService.save(user())
assertThat(superUserService.findAll()).isEmpty()
}
}
@SpringBootTest
class Exo8 {
@MockkBean
private lateinit var database: ListDatabase
@Autowired
private lateinit var userService: UserService
@Test
fun exo_9() {
// GIVEN TODO
every { database.delete(any()) } returns Unit
every { database.delete(user()) } throws NoSuchElementException()
// THEN
assertThrows<NoSuchElementException> { userService.delete(user()) }
userService.delete(user(UUID.randomUUID()))
}
}
private fun user(uuid: UUID = UUID(0, 1)) = User(uuid, "John Doe", "email@noop.pony", 42)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>iut.nantes</groupId>
<artifactId>td1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>exo1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>1.13.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter