Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Wendys Racing Horses
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
Wendys Racing Horses
Commits
dda711f3
Commit
dda711f3
authored
Mar 17, 2020
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
US01, TS13: Add tests to the horse backend
parent
707487c2
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
178 additions
and
10 deletions
+178
-10
backend/pom.xml
backend/pom.xml
+3
-0
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java
...ien/sepm/assignment/individual/endpoint/dto/HorseDto.java
+5
-5
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java
...at/ac/tuwien/sepm/assignment/individual/entity/Horse.java
+3
-3
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java
.../assignment/individual/persistence/impl/HorseJdbcDao.java
+1
-2
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java
.../assignment/individual/integration/HorseEndpointTest.java
+47
-0
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java
...ignment/individual/unit/persistence/HorseDaoJdbcTest.java
+40
-0
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java
...ignment/individual/unit/persistence/HorseDaoTestBase.java
+36
-0
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java
.../assignment/individual/unit/service/HorseServiceTest.java
+43
-0
No files found.
backend/pom.xml
View file @
dda711f3
...
...
@@ -77,6 +77,9 @@
<plugin>
<artifactId>
maven-surefire-plugin
</artifactId>
<version>
${maven.surefire-plugin.version}
</version>
<configuration>
<trimStackTrace>
false
</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>
...
...
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java
View file @
dda711f3
...
...
@@ -11,7 +11,7 @@ public class HorseDto extends BaseDto {
private
String
description
;
private
short
score
;
private
Date
birthday
;
private
l
ong
owner
;
private
L
ong
owner
;
public
HorseDto
()
{}
...
...
@@ -91,10 +91,10 @@ public class HorseDto extends BaseDto {
DateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
return
super
.
fieldsString
()
+
", name='"
+
name
+
'\''
+
"description='"
+
description
+
'\''
+
"score='"
+
score
+
'\''
+
"birthday='"
+
df
.
format
(
birthday
)
+
'\''
+
"owner_id='"
+
owner
+
'\''
;
"
,
description='"
+
description
+
'\''
+
"
,
score='"
+
score
+
'\''
+
"
,
birthday='"
+
df
.
format
(
birthday
)
+
'\''
+
"
,
owner_id='"
+
owner
+
'\''
;
}
@Override
...
...
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java
View file @
dda711f3
...
...
@@ -101,9 +101,9 @@ public class Horse extends BaseEntity {
return
super
.
fieldsString
()
+
", name='"
+
name
+
'\''
+
"description='"
+
description
+
'\''
+
"score='"
+
score
+
'\''
+
"birthday='"
+
df
.
format
(
birthday
)
+
'\''
+
"owner='"
+
owner
+
'\''
;
"
,
score='"
+
score
+
'\''
+
"
,
birthday='"
+
df
.
format
(
birthday
)
+
'\''
+
"
,
owner='"
+
owner
+
'\''
;
}
@Override
...
...
backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java
View file @
dda711f3
...
...
@@ -66,7 +66,7 @@ public class HorseJdbcDao implements HorseDao {
ps
.
setInt
(
3
,
horse
.
getScore
());
ps
.
setDate
(
4
,
horse
.
getBirthday
());
if
(
horse
.
getOwner
()
==
0
)
if
(
horse
.
getOwner
()
==
null
||
horse
.
getOwner
()
==
0
)
ps
.
setNull
(
5
,
Types
.
NULL
);
else
ps
.
setObject
(
5
,
horse
.
getOwner
());
...
...
@@ -98,7 +98,6 @@ public class HorseJdbcDao implements HorseDao {
horse
.
setScore
(
resultSet
.
getShort
(
"score"
));
horse
.
setBirthday
(
resultSet
.
getDate
(
"birthday"
));
horse
.
setOwner
(
resultSet
.
getLong
(
"owner_id"
));
return
horse
;
}
}
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java
0 → 100644
View file @
dda711f3
package
at.ac.tuwien.sepm.assignment.individual.integration
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.web.client.RestTemplate
;
import
java.sql.Date
;
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ActiveProfiles
(
"test"
)
public
class
HorseEndpointTest
{
private
static
final
RestTemplate
REST_TEMPLATE
=
new
RestTemplate
();
private
static
final
String
BASE_URL
=
"http://localhost:"
;
private
static
final
String
HORSE_URL
=
"/horses"
;
@LocalServerPort
private
int
port
;
@Test
@DisplayName
(
"Adding a new horse with the correct parameters will return HTTP 201 and the new HorseDto"
)
public
void
addingNewHorse_correctParameters_shouldReturnStatus201AndHorse
()
{
String
birthday
=
"2020-01-01"
;
HorseDto
newHorse
=
new
HorseDto
(
"Zephyr"
,
"Nice horse"
,
(
short
)
4
,
Date
.
valueOf
(
birthday
),
null
);
HttpEntity
<
HorseDto
>
request
=
new
HttpEntity
<>(
newHorse
);
ResponseEntity
<
HorseDto
>
response
=
REST_TEMPLATE
.
exchange
(
BASE_URL
+
port
+
HORSE_URL
,
HttpMethod
.
POST
,
request
,
HorseDto
.
class
);
// Compare everything except ids and timestamps
assertEquals
(
response
.
getStatusCode
(),
HttpStatus
.
CREATED
);
assertEquals
(
newHorse
.
getName
(),
response
.
getBody
().
getName
());
assertEquals
(
newHorse
.
getDescription
(),
response
.
getBody
().
getDescription
());
assertEquals
(
newHorse
.
getScore
(),
response
.
getBody
().
getScore
());
assertEquals
(
newHorse
.
getBirthday
().
toString
(),
response
.
getBody
().
getBirthday
().
toString
());
assertEquals
(
newHorse
.
getOwner
(),
response
.
getBody
().
getOwner
());
}
}
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java
0 → 100644
View file @
dda711f3
package
at.ac.tuwien.sepm.assignment.individual.unit.persistence
;
import
static
org
.
junit
.
jupiter
.
api
.
Assumptions
.
assumeTrue
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.BeforeEach
;
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.ActiveProfiles
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
org.springframework.transaction.PlatformTransactionManager
;
import
org.springframework.transaction.TransactionDefinition
;
import
org.springframework.transaction.TransactionStatus
;
import
org.springframework.transaction.support.DefaultTransactionDefinition
;
@ExtendWith
(
SpringExtension
.
class
)
@SpringBootTest
@ActiveProfiles
(
"test"
)
public
class
HorseDaoJdbcTest
extends
HorseDaoTestBase
{
@Autowired
PlatformTransactionManager
txm
;
TransactionStatus
txstatus
;
@BeforeEach
public
void
setupDBTransaction
()
{
DefaultTransactionDefinition
def
=
new
DefaultTransactionDefinition
();
def
.
setPropagationBehavior
(
TransactionDefinition
.
PROPAGATION_REQUIRES_NEW
);
txstatus
=
txm
.
getTransaction
(
def
);
assumeTrue
(
txstatus
.
isNewTransaction
());
txstatus
.
setRollbackOnly
();
}
@AfterEach
public
void
tearDownDBData
()
{
txm
.
rollback
(
txstatus
);
}
}
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java
0 → 100644
View file @
dda711f3
package
at.ac.tuwien.sepm.assignment.individual.unit.persistence
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
at.ac.tuwien.sepm.assignment.individual.entity.Horse
;
import
at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.dao.DataAccessException
;
import
java.sql.Date
;
public
abstract
class
HorseDaoTestBase
{
@Autowired
HorseDao
horseDao
;
@Test
@DisplayName
(
"Adding a new horse with the correct parameters should return the horse"
)
public
void
addingNewHorse_correctParameters_shouldReturnHorse
()
{
String
birthday
=
"2020-01-01"
;
Horse
newHorse
=
new
Horse
(
"Zephyr"
,
"Nice horse"
,
(
short
)
4
,
Date
.
valueOf
(
birthday
),
null
);
Horse
savedHorse
=
horseDao
.
addHorse
(
newHorse
);
assertEquals
(
newHorse
,
savedHorse
);
}
@Test
@DisplayName
(
"Adding a new horse with the incorrect parameters should throw DataAccessException"
)
public
void
addingNewHorse_incorrectParameters_shouldThrowDataAccess
()
{
String
birthday
=
"2020-01-01"
;
Horse
newHorse
=
new
Horse
(
"Zephyr"
,
"Nice horse"
,
(
short
)
80
,
Date
.
valueOf
(
birthday
),
null
);
assertThrows
(
DataAccessException
.
class
,
()
->
horseDao
.
addHorse
(
newHorse
));
}
}
backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java
0 → 100644
View file @
dda711f3
package
at.ac.tuwien.sepm.assignment.individual.unit.service
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
at.ac.tuwien.sepm.assignment.individual.entity.Horse
;
import
at.ac.tuwien.sepm.assignment.individual.service.HorseService
;
import
at.ac.tuwien.sepm.assignment.individual.util.ValidationException
;
import
org.junit.jupiter.api.DisplayName
;
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.ActiveProfiles
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
java.sql.Date
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
@ExtendWith
(
SpringExtension
.
class
)
@SpringBootTest
@ActiveProfiles
(
"test"
)
public
class
HorseServiceTest
{
@Autowired
HorseService
horseService
;
@Test
@DisplayName
(
"Adding a new horse with the correct parameters will return the new horse"
)
public
void
addingNewHorse_correctParameters_shouldReturnHorse
()
{
String
birthday
=
"2020-01-01"
;
Horse
newHorse
=
new
Horse
(
"Zephyr"
,
"Nice horse"
,
(
short
)
4
,
Date
.
valueOf
(
birthday
),
null
);
Horse
savedHorse
=
horseService
.
addHorse
(
newHorse
);
assertEquals
(
newHorse
,
savedHorse
);
}
@Test
@DisplayName
(
"Adding a new horse with the incorrect parameters will throw a ValidationException"
)
public
void
addingNewHorse_incorrectParameters_shouldThrowValidation
()
{
String
birthday
=
"2020-01-01"
;
Horse
newHorse
=
new
Horse
(
"Zephyr"
,
"Nice horse"
,
(
short
)
80
,
Date
.
valueOf
(
birthday
),
null
);
assertThrows
(
ValidationException
.
class
,
()
->
horseService
.
addHorse
(
newHorse
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment