Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Lukas Wiest
unified-ticketing
Commits
7443f5c5
Verified
Commit
7443f5c5
authored
Dec 21, 2020
by
Lukas Wiest
🚂
Browse files
refactor(test): add tests for github
parent
a941ff86
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubFilterTest.java
0 → 100644
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
de.hftstuttgart.unifiedticketing.exceptions.DeserializationException
;
import
de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException
;
import
okhttp3.*
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentCaptor
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.logging.Level
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
public
class
GithubFilterTest
{
public
GithubFilter
instance
;
public
Call
call
;
public
ArgumentCaptor
<
Request
>
requestCaptor
;
public
Response
.
Builder
responseBuilder
;
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@BeforeEach
public
void
initBeforeEach
()
{
GithubTicketSystem
parent
=
spy
(
new
GithubTicketSystem
(
"someHeader"
,
"https://api.github.com"
));
instance
=
spy
(
new
GithubFilter
(
parent
));
OkHttpClient
client
=
mock
(
OkHttpClient
.
class
);
call
=
mock
(
Call
.
class
);
requestCaptor
=
ArgumentCaptor
.
forClass
(
Request
.
class
);
responseBuilder
=
new
Response
.
Builder
()
.
request
(
new
Request
.
Builder
().
url
(
"http://test.some.tld/"
).
build
())
.
protocol
(
Protocol
.
HTTP_1_1
)
.
message
(
"some message"
);
doReturn
(
client
).
when
(
instance
).
getHttpClient
();
doReturn
(
call
).
when
(
client
).
newCall
(
requestCaptor
.
capture
());
}
@Test
public
void
testGetServerError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
500
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
get
());
}
@Test
public
void
testGetClientError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
400
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
get
());
}
@Test
public
void
testGetNullBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
get
());
}
@Test
public
void
testGetNoJsonBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"somestrangething"
,
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
assertThrows
(
DeserializationException
.
class
,
()
->
instance
.
get
());
}
@Test
public
void
testGetRequestParamsPart1
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"[]"
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
String
[]
values
=
new
String
[]
{
"bug"
,
"label1"
,
"2"
,
"50"
,
};
instance
.
withLabel
(
values
[
0
])
.
withLabel
(
values
[
1
])
.
setPage
(
Integer
.
parseInt
(
values
[
2
]))
.
setPageSize
(
Integer
.
parseInt
(
values
[
3
]))
.
isOpen
()
.
get
();
HttpUrl
url
=
requestCaptor
.
getValue
().
url
();
assertAll
(
()
->
assertEquals
(
String
.
format
(
"%s,%s"
,
values
[
0
],
values
[
1
]),
url
.
queryParameter
(
"labels"
)),
()
->
assertEquals
(
values
[
2
],
url
.
queryParameter
(
"page"
)),
()
->
assertEquals
(
values
[
3
],
url
.
queryParameter
(
"per_page"
)),
()
->
assertEquals
(
"open"
,
url
.
queryParameter
(
"state"
))
);
}
@Test
public
void
testGetRequestParamsPart2
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"[]"
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
instance
.
isClosed
()
.
get
();
HttpUrl
url
=
requestCaptor
.
getValue
().
url
();
assertAll
(
()
->
assertEquals
(
"closed"
,
url
.
queryParameter
(
"state"
))
);
}
@Test
public
void
testGetLocalFilter
()
throws
IOException
{
GithubTicketResponse
res
=
new
GithubTicketResponse
();
res
.
number
=
5
;
res
.
title
=
"some title"
;
res
.
body
=
"descriptive text"
;
res
.
assignees
=
new
HashSet
<>();
res
.
state
=
"open"
;
res
.
labels
=
Arrays
.
stream
(
new
String
[]{
"unifiedticketing"
,
"bug"
})
.
map
(
l
->
{
GithubTicketResponse
.
Label
label
=
new
GithubTicketResponse
.
Label
();
label
.
name
=
l
;
return
label
;
})
.
collect
(
Collectors
.
toSet
());
ObjectMapper
mapper
=
new
ObjectMapper
();
ArrayNode
arrayNode
=
mapper
.
createArrayNode
();
arrayNode
.
add
(
mapper
.
valueToTree
(
res
));
res
.
number
=
8
;
res
.
title
=
"some special title"
;
arrayNode
.
add
(
mapper
.
valueToTree
(
res
));
res
.
number
=
94
;
res
.
body
=
"description with @username marked"
;
arrayNode
.
add
(
mapper
.
valueToTree
(
res
));
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
arrayNode
.
toString
().
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
List
<
GithubTicket
>
result
=
instance
.
withTitleMatch
(
"^.*special.*$"
)
.
withDescriptionMatch
(
"^.*@username.*$"
)
.
get
();
assertAll
(
()
->
assertEquals
(
1
,
result
.
size
()),
()
->
Assertions
.
assertEquals
(
"94"
,
result
.
get
(
0
).
getId
())
);
}
@Test
public
void
testGetDeserialization
()
throws
IOException
{
GithubTicketResponse
res
=
new
GithubTicketResponse
();
res
.
number
=
99
;
res
.
title
=
"some title"
;
res
.
body
=
"descriptive text"
;
res
.
assignees
=
new
HashSet
<>();
res
.
state
=
"open"
;
res
.
labels
=
Arrays
.
stream
(
new
String
[]{
"unifiedticketing"
,
"feature-request"
})
.
map
(
l
->
{
GithubTicketResponse
.
Label
label
=
new
GithubTicketResponse
.
Label
();
label
.
name
=
l
;
return
label
;
})
.
collect
(
Collectors
.
toSet
());
ObjectMapper
mapper
=
new
ObjectMapper
();
ArrayNode
arrayNode
=
mapper
.
createArrayNode
();
arrayNode
.
add
(
mapper
.
valueToTree
(
res
));
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
arrayNode
.
toString
().
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
List
<
GithubTicket
>
expected
=
new
LinkedList
<>(
Collections
.
singleton
(
GithubTicket
.
fromTicketResponse
(
instance
.
parent
,
res
)));
List
<
GithubTicket
>
actual
=
instance
.
get
();
assertEquals
(
expected
,
actual
);
assertTrue
(
expected
.
get
(
0
).
deepEquals
(
actual
.
get
(
0
)));
}
}
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubTicketBuilderTest.java
0 → 100644
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
de.hftstuttgart.unifiedticketing.exceptions.AssertionException
;
import
de.hftstuttgart.unifiedticketing.exceptions.DeserializationException
;
import
de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException
;
import
okhttp3.*
;
import
okio.Buffer
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentCaptor
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.LinkedList
;
import
java.util.logging.Level
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
public
class
GithubTicketBuilderTest
{
public
GithubTicketBuilder
instance
;
public
Call
call
;
public
ArgumentCaptor
<
Request
>
requestCaptor
;
public
Response
.
Builder
responseBuilder
;
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@BeforeEach
public
void
initBeforeEach
()
{
GithubTicketSystem
parent
=
spy
(
new
GithubTicketSystem
(
"something"
,
"https://example.org"
));
instance
=
spy
(
new
GithubTicketBuilder
(
parent
));
OkHttpClient
client
=
mock
(
OkHttpClient
.
class
);
call
=
mock
(
Call
.
class
);
requestCaptor
=
ArgumentCaptor
.
forClass
(
Request
.
class
);
responseBuilder
=
new
Response
.
Builder
()
.
request
(
new
Request
.
Builder
().
url
(
"http://test.some.tld/"
).
build
())
.
protocol
(
Protocol
.
HTTP_1_1
)
.
message
(
"some message"
);
doReturn
(
client
).
when
(
instance
).
getHttpClient
();
doReturn
(
call
).
when
(
client
).
newCall
(
requestCaptor
.
capture
());
}
@Test
public
void
testSaveNoCreationWithoutMinimumRequirements
()
{
assertThrows
(
AssertionException
.
class
,
()
->
instance
.
create
());
verify
(
instance
,
never
()).
getHttpClient
();
}
@Test
public
void
testSaveServerError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
500
).
build
()).
when
(
call
).
execute
();
instance
.
title
(
"we have a new title"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
create
());
}
@Test
public
void
testSaveClientError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
400
).
build
()).
when
(
call
).
execute
();
instance
.
title
(
"we have another new title"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
create
());
}
@Test
public
void
testSaveNullBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
).
build
()).
when
(
call
).
execute
();
instance
.
title
(
"we have no body this time"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
create
());
}
@Test
public
void
testSaveNoJsonBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"somestrangething"
,
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
instance
.
title
(
"this body is no json"
);
assertThrows
(
DeserializationException
.
class
,
()
->
instance
.
create
());
}
@Test
public
void
testSaveSuccessfulUpdate
()
throws
IOException
{
GithubTicketResponse
ticketResponse
=
new
GithubTicketResponse
();
ticketResponse
.
title
=
"title of ticket"
;
ticketResponse
.
body
=
"description"
;
ticketResponse
.
number
=
5
;
ticketResponse
.
labels
=
Arrays
.
stream
(
new
String
[]{
"unifiedticketing"
,
"feature-request"
})
.
map
(
l
->
{
GithubTicketResponse
.
Label
label
=
new
GithubTicketResponse
.
Label
();
label
.
name
=
l
;
return
label
;
})
.
collect
(
Collectors
.
toSet
());
GithubTicketResponse
.
Assignee
assignee
=
new
GithubTicketResponse
.
Assignee
();
assignee
.
id
=
234
;
assignee
.
login
=
"username"
;
ticketResponse
.
assignees
=
new
HashSet
<>(
Collections
.
singleton
(
assignee
));
GithubTicket
expected
=
GithubTicket
.
fromTicketResponse
(
instance
.
parent
,
ticketResponse
);
String
responseJson
=
new
ObjectMapper
().
writeValueAsString
(
ticketResponse
);
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
responseJson
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
GithubTicket
actual
=
instance
.
title
(
"some title"
).
create
();
assertEquals
(
expected
,
actual
);
assertTrue
(
expected
.
deepEquals
(
actual
));
}
@Test
public
void
testSaveRequestJson
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"{}"
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
instance
.
title
(
"title of ticket"
)
.
description
(
"description"
)
.
labels
(
new
HashSet
<>(
Arrays
.
asList
(
"bug"
,
"unifiedticketing"
)))
.
assignees
(
"somebody"
)
.
create
();
Buffer
buffer
=
new
Buffer
();
ObjectMapper
mapper
=
new
ObjectMapper
();
requestCaptor
.
getValue
().
body
().
writeTo
(
buffer
);
String
expectedJson
=
"{\"assignees\":[\"somebody\"],\"body\":\"description\","
+
"\"title\":\"title of ticket\",\"labels\":[\"bug\",\"unifiedticketing\"]}"
;
assertEquals
(
mapper
.
readTree
(
expectedJson
),
mapper
.
readTree
(
buffer
.
readUtf8
()));
}
}
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubTicketResponseTest.java
0 → 100644
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.Test
;
import
java.io.IOException
;
import
java.util.logging.Level
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
public
class
GithubTicketResponseTest
{
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@Test
public
void
testDeserialization
()
throws
IOException
{
ObjectMapper
mapper
=
new
ObjectMapper
()
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
String
[]
values
=
new
String
[]
// title descr iid label label userid username
{
"title of ticket"
,
"description"
,
"5"
,
"bug"
,
"unifiedticketing"
,
"234"
,
"username"
};
String
responseJson
=
String
.
format
(
"{\"title\":\"%s\",\"body\":\"%s\",\"number\":%s,"
+
"\"labels\":[{\"name\":\"%s\"},{\"name\":\"%s\"}],\"assignees\":[{\"id\":%s,\"login\":\"%s\"}]}"
,
(
Object
[])
values
);
GithubTicketResponse
response
=
mapper
.
readValue
(
responseJson
.
getBytes
(),
GithubTicketResponse
.
class
);
assertAll
(
()
->
assertEquals
(
values
[
0
],
response
.
title
),
()
->
assertEquals
(
values
[
1
],
response
.
body
),
()
->
assertEquals
(
values
[
2
],
String
.
valueOf
(
response
.
number
)),
()
->
assertTrue
(
response
.
labels
.
stream
().
map
(
l
->
l
.
name
).
collect
(
Collectors
.
toSet
()).
contains
(
values
[
3
])),
()
->
assertTrue
(
response
.
labels
.
stream
().
map
(
l
->
l
.
name
).
collect
(
Collectors
.
toSet
()).
contains
(
values
[
4
])),
()
->
assertEquals
(
1
,
response
.
assignees
.
size
()),
()
->
assertEquals
(
values
[
5
],
String
.
valueOf
(
response
.
assignees
.
stream
().
findFirst
().
get
().
id
)),
()
->
assertEquals
(
values
[
6
],
response
.
assignees
.
stream
().
findFirst
().
get
().
login
)
);
}
}
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubTicketSystemBuilderTest.java
0 → 100644
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
de.hftstuttgart.unifiedticketing.exceptions.AssertionException
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.Mockito
;
import
java.util.logging.Level
;
import
java.util.regex.Pattern
;
public
class
GithubTicketSystemBuilderTest
{
public
GithubTicketSystemBuilder
instance
;
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@BeforeEach
public
void
initBeforeEach
()
{
instance
=
Mockito
.
spy
(
new
GithubTicketSystemBuilder
());
}
@Test
public
void
testInit
()
{
assertAll
(
()
->
assertEquals
(
"application/vnd.github.v3+json"
,
instance
.
acceptHeader
),
()
->
assertNull
(
instance
.
apiKey
),
()
->
assertNull
(
instance
.
owner
),
()
->
assertNull
(
instance
.
repo
),
()
->
assertTrue
(
instance
.
https
),
()
->
assertNull
(
instance
.
username
)
);
}
@Test
public
void
testWithApiKey
()
{
String
username
=
"someUser"
;
String
apiKey
=
"asoashdboahasjhdfbeuazhvfef4q34v3h4v435v2"
;
instance
.
withAuthentication
(
username
,
apiKey
);
assertAll
(
()
->
assertEquals
(
username
,
instance
.
username
),
()
->
assertEquals
(
apiKey
,
instance
.
apiKey
)
);
}
@Test
public
void
testWithHttp
()
{
instance
.
https
=
true
;
instance
.
withHttp
();
assertFalse
(
instance
.
https
);
}
@Test
public
void
testWithHttps
()
{
instance
.
https
=
false
;
instance
.
withHttps
();
assertTrue
(
instance
.
https
);
}
@Test
public
void
testWithProjectIdByInt
()
{
String
expected
=
"255254123"
;
instance
.
withRepo
(
expected
);
assertEquals
(
expected
,
instance
.
repo
);
}
@Test
public
void
testBuild
()
{
instance
.
withBaseUrl
(
"blabla.tld"
);
assertThrows
(
AssertionException
.
class
,
()
->
instance
.
build
());
String
apiKey
=
"aaesfsef32qqfq3f"
;
String
baseUrl
=
"api.github.com"
;
String
owner
=
"someOwner"
;
String
repo
=
"42"
;
String
username
=
"someUsername"
;
instance
=
new
GithubTicketSystemBuilder
();
instance
.
withOwner
(
owner
);
instance
.
withRepo
(
repo
);
assertThrows
(
AssertionException
.
class
,
()
->
instance
.
build
());
instance
=
new
GithubTicketSystemBuilder
();
String
regex
=
"^%s://%s/repos/%s/%s/issues$"
;
Pattern
pattern
=
Pattern
.
compile
(
String
.
format
(
regex
,
"https"
,
baseUrl
,
owner
,
repo
));
instance
.
withBaseUrl
(
baseUrl
)
.
withOwner
(
owner
)
.
withRepo
(
repo
);
GithubTicketSystem
system
=
instance
.
build
();
assertTrue
(
pattern
.
matcher
(
system
.
baseUrl
).
matches
());
pattern
=
Pattern
.
compile
(
String
.
format
(
regex
,
"http"
,
baseUrl
,
owner
,
repo
));
instance
.
withHttp
()
.
withAuthentication
(
username
,
apiKey
);
system
=
instance
.
build
();
assertTrue
(
pattern
.
matcher
(
system
.
baseUrl
).
matches
());
assertEquals
(
username
,
system
.
username
);
assertEquals
(
apiKey
,
system
.
apiKey
);
}
}
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubTicketSystemTest.java
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
de.hftstuttgart.unifiedticketing.core.TicketSystem
;
import
de.hftstuttgart.unifiedticketing.exceptions.AssertionException
;
import
de.hftstuttgart.unifiedticketing.exceptions.DeserializationException
;
import
de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException
;
import
okhttp3.*
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentCaptor
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.logging.Level
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
public
class
GithubTicketSystemTest
{
public
GithubTicketSystem
instance
;
public
Call
call
;
public
ArgumentCaptor
<
Request
>
requestCaptor
;
public
Response
.
Builder
responseBuilder
;
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@BeforeEach
public
void
initBeforeEach
()
{
instance
=
spy
(
new
GithubTicketSystem
(
"someHeader"
,
"https://api.github.com"
));
OkHttpClient
client
=
mock
(
OkHttpClient
.
class
);
call
=
mock
(
Call
.
class
);
requestCaptor
=
ArgumentCaptor
.
forClass
(
Request
.
class
);
responseBuilder
=
new
Response
.
Builder
()
.
request
(
new
Request
.
Builder
().
url
(
"http://test.some.tld/"
).
build
())
.
protocol
(
Protocol
.
HTTP_1_1
)
.
message
(
"some message"
);
doReturn
(
client
).
when
(
instance
).
getHttpClient
();
doReturn
(
call
).
when
(
client
).
newCall
(
requestCaptor
.
capture
());
}
@Test
public
void
blocker
()
public
void
testFromUri
()
{
String
base
=
"api.github.com"
;
String
owner
=
"myOwner"
;
String
repo
=
"myRepo"
;
String
username
=
"someUser"
;
String
apikey
=
"afdaf3aqraf3afafmyxcbvmyxvbas3wrawra"
;
GithubTicketSystem
actual
=
(
GithubTicketSystem
)
TicketSystem
.
fromUri
(
buildTestUri
(
true
,
base
,
owner
,
repo
,
username
,
apikey
));
assertEquals
(
buildFinalBaseurl
(
true
,
base
,
owner
,
repo
),
actual
.
baseUrl
);
assertEquals
(
username
,
actual
.
username
);
assertEquals
(
apikey
,
actual
.
apiKey
);
assertEquals
(
GithubTicketSystem
.
class
,
actual
.
getClass
());
base
=
"api.github.somedomain.tld:8080"
;
owner
=
"otherOwner"
;
repo
=
"otherRepo"
;
username
=
"me"
;
apikey
=
"a34adfae3ra3rasd"
;
actual
=
(
GithubTicketSystem
)
TicketSystem
.
fromUri
(
buildTestUri
(
false
,
base
,
owner
,
repo
,
username
,
apikey
));
assertEquals
(
buildFinalBaseurl
(
false
,
base
,
owner
,
repo
),
actual
.
baseUrl
);
assertEquals
(
username
,
actual
.
username
);
assertEquals
(
apikey
,
actual
.
apiKey
);
assertEquals
(
GithubTicketSystem
.
class
,
actual
.
getClass
());
actual
=
(
GithubTicketSystem
)
TicketSystem
.
fromUri
(
buildTestUri
(
false
,
base
,
owner
,
repo
,
null
,
null
));
assertEquals
(
buildFinalBaseurl
(
false
,
base
,
owner
,
repo
),
actual
.
baseUrl
);
assertNull
(
actual
.
username
);
assertNull
(
actual
.
apiKey
);
assertEquals
(
GithubTicketSystem
.
class
,
actual
.
getClass
());
assertThrows
(
AssertionException
.
class
,
()
->
TicketSystem
.
fromUri
(
"unifiedticketing:github:blablabal"
));
}
@Test
public
void
testCreateTicket
()
{
assertEquals
(
GithubTicketBuilder
.
class
,
instance
.
createTicket
().
getClass
());
assertSame
(
instance
,
instance
.
createTicket
().
parent
);
}
@Test
public
void
testFind
()
{
assertEquals
(
GithubFilter
.
class
,
instance
.
find
().
getClass
());
assertSame
(
instance
,
instance
.
find
().
parent
);
}
@Test
public
void
testGetServerError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
500
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
getTicketById
(
"bla"
));
}
@Test
public
void
testGetClientError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
400
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
getTicketById
(
"bla"
));
}
@Test
public
void
testGetNullBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
).
build
()).
when
(
call
).
execute
();
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
getTicketById
(
"bla"
));
}
@Test
public
void
testGetNoJsonBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"somestrangething"
,
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
assertThrows
(
DeserializationException
.
class
,
()
->
instance
.
getTicketById
(
"bla"
));
}
@Test
public
void
testGetTicketById
()
throws
IOException
{
GithubTicketResponse
res
=
new
GithubTicketResponse
();
res
.
number
=
99
;
res
.
title
=
"some title"
;
res
.
body
=
"descriptive text"
;
res
.
assignees
=
new
HashSet
<>();
res
.
state
=
"open"
;
res
.
labels
=
Arrays
.
stream
(
new
String
[]{
"unifiedticketing"
,
"feature-request"
})
.
map
(
l
->
{
GithubTicketResponse
.
Label
label
=
new
GithubTicketResponse
.
Label
();
label
.
name
=
l
;
return
label
;
})
.
collect
(
Collectors
.
toSet
());
ObjectMapper
mapper
=
new
ObjectMapper
();
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
mapper
.
writeValueAsString
(
res
),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
GithubTicket
expected
=
GithubTicket
.
fromTicketResponse
(
instance
,
res
);
GithubTicket
actual
=
instance
.
getTicketById
(
res
.
number
);
assertEquals
(
expected
,
actual
);
assertTrue
(
expected
.
deepEquals
(
actual
));
}
@Test
public
void
testSupport
()
{
assertAll
(
()
->
assertTrue
(
instance
.
hasAssigneeSupport
()),
()
->
assertTrue
(
instance
.
hasDefaultPagination
()),
()
->
assertTrue
(
instance
.
hasLabelSupport
()),
()
->
assertTrue
(
instance
.
hasPaginationSupport
()),
()
->
assertTrue
(
instance
.
hasReturnNullOnErrorSupport
())
);
}
private
static
String
buildTestUri
(
boolean
https
,
String
base
,
String
owner
,
String
repo
,
String
username
,
String
apikey
)
{
return
String
.
format
(
"unifiedticketing:github:%s://%s::%s:%s%s"
,
(
https
)
?
"https"
:
"http"
,
base
,
owner
,
repo
,
(
username
==
null
||
apikey
==
null
)
?
""
:
":"
+
username
+
":"
+
apikey
);
}
private
static
String
buildFinalBaseurl
(
boolean
https
,
String
base
,
String
owner
,
String
repo
)
{
fail
(
);
return
String
.
format
(
"%s://%s/repos/%s/%s/issues"
,
(
https
)
?
"https"
:
"http"
,
base
,
owner
,
repo
);
}
}
src/test/java/de/hftstuttgart/unifiedticketing/systems/github/GithubTicketTest.java
0 → 100644
View file @
7443f5c5
package
de.hftstuttgart.unifiedticketing.systems.github
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
de.hftstuttgart.unifiedticketing.core.Logging
;
import
de.hftstuttgart.unifiedticketing.exceptions.AssertionException
;
import
de.hftstuttgart.unifiedticketing.exceptions.DeserializationException
;
import
de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException
;
import
okhttp3.*
;
import
okio.Buffer
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.Mockito
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.logging.Level
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
public
class
GithubTicketTest
{
public
GithubTicket
instance
;
public
Call
call
;
public
ArgumentCaptor
<
Request
>
requestCaptor
;
public
Response
.
Builder
responseBuilder
;
@BeforeAll
public
static
void
initBeforeAll
()
{
Logging
.
setLevel
(
Level
.
ALL
);
}
@BeforeEach
public
void
initBeforeEach
()
{
instance
=
spy
(
new
GithubTicket
(
spy
(
new
GithubTicketSystem
(
"someHeader"
,
"https://example.org"
))));
OkHttpClient
client
=
mock
(
OkHttpClient
.
class
);
call
=
mock
(
Call
.
class
);
requestCaptor
=
ArgumentCaptor
.
forClass
(
Request
.
class
);
responseBuilder
=
new
Response
.
Builder
()
.
request
(
new
Request
.
Builder
().
url
(
"http://test.some.tld/"
).
build
())
.
protocol
(
Protocol
.
HTTP_1_1
)
.
message
(
"some message"
);
doReturn
(
client
).
when
(
instance
).
getHttpClient
();
doReturn
(
call
).
when
(
client
).
newCall
(
requestCaptor
.
capture
());
}
@Test
public
void
testFromTicketResponse
()
{
GithubTicketResponse
res
=
new
GithubTicketResponse
();
GithubTicket
ticket
=
GithubTicket
.
fromTicketResponse
(
instance
.
getParent
(),
res
);
assertAll
(
()
->
assertSame
(
instance
.
getParent
(),
ticket
.
getParent
()),
()
->
assertNull
(
ticket
.
getTitle
()),
()
->
assertNull
(
ticket
.
getDescription
()),
()
->
assertEquals
(
"0"
,
ticket
.
getId
()),
()
->
assertNotNull
(
ticket
.
getLabels
()),
()
->
assertTrue
(
ticket
.
isOpen
()),
()
->
assertNull
(
ticket
.
getTitle
()),
()
->
assertNotNull
(
ticket
.
getAssignees
())
);
}
@Test
public
void
testAddAssigneeByString
()
{
String
username
=
"someUsername"
;
instance
.
addAssignee
(
username
);
assertEquals
(
1
,
instance
.
getAssignees
().
size
());
assertEquals
(
username
,
instance
.
getAssignees
().
stream
().
findFirst
().
get
().
username
);
}
@Test
public
void
testRemoveAssigneeByString
()
{
// first add assignee and check it was added properly
testAddAssigneeByString
();
String
username
=
instance
.
getAssignees
().
stream
().
findFirst
().
get
().
username
;
instance
.
removeAssignee
(
username
);
assertEquals
(
0
,
instance
.
getAssignees
().
size
());
}
@Test
public
void
testSaveNoHttpClientWithoutChanges
()
{
GithubTicket
actual
=
instance
.
save
();
verify
(
instance
,
never
()).
getHttpClient
();
assertSame
(
instance
,
actual
);
}
@Test
public
void
testSaveServerError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
500
).
build
()).
when
(
call
).
execute
();
instance
.
setTitle
(
"we have a new title"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
save
());
}
@Test
public
void
testSaveClientError
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
400
).
build
()).
when
(
call
).
execute
();
instance
.
setTitle
(
"we have another new title"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
save
());
}
@Test
public
void
testSaveNullBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
).
build
()).
when
(
call
).
execute
();
instance
.
setTitle
(
"we have no body this time"
);
assertThrows
(
HttpResponseException
.
class
,
()
->
instance
.
save
());
}
@Test
public
void
testSaveNoJsonBody
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"somestrangething"
,
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
instance
.
setTitle
(
"this body is no json"
);
assertThrows
(
DeserializationException
.
class
,
()
->
instance
.
save
());
}
@Test
public
void
testSaveSuccessfulUpdate
()
throws
IOException
{
GithubTicketResponse
ticketResponse
=
new
GithubTicketResponse
();
ticketResponse
.
title
=
"title of ticket"
;
ticketResponse
.
body
=
"description"
;
ticketResponse
.
number
=
5
;
ticketResponse
.
labels
=
Arrays
.
stream
(
new
String
[]{
"bug"
,
"unifiedticketing"
})
.
map
(
l
->
{
GithubTicketResponse
.
Label
label
=
new
GithubTicketResponse
.
Label
();
label
.
name
=
l
;
return
label
;
})
.
collect
(
Collectors
.
toSet
());
GithubTicketResponse
.
Assignee
assignee
=
new
GithubTicketResponse
.
Assignee
();
assignee
.
id
=
234
;
assignee
.
login
=
"username"
;
ticketResponse
.
assignees
=
new
HashSet
<>(
Collections
.
singleton
(
assignee
));
GithubTicket
expected
=
GithubTicket
.
fromTicketResponse
(
instance
.
getParent
(),
ticketResponse
);
String
responseJson
=
new
ObjectMapper
().
writeValueAsString
(
ticketResponse
);
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
responseJson
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
GithubTicket
actual
=
instance
.
close
().
save
();
assertEquals
(
expected
,
actual
);
assertTrue
(
expected
.
deepEquals
(
actual
));
}
@Test
public
void
testSaveRequestJson
()
throws
IOException
{
doReturn
(
responseBuilder
.
code
(
200
)
.
body
(
ResponseBody
.
create
(
"{}"
.
getBytes
(),
MediaType
.
get
(
"application/json"
)))
.
build
())
.
when
(
call
).
execute
();
instance
.
setTitle
(
"title of ticket"
)
.
setDescription
(
"description"
)
.
setLabels
(
new
HashSet
<>(
Arrays
.
asList
(
"bug"
,
"unifiedticketing"
)))
.
addAssignee
(
"someUsername"
)
.
save
();
Buffer
buffer
=
new
Buffer
();
ObjectMapper
mapper
=
new
ObjectMapper
();
requestCaptor
.
getValue
().
body
().
writeTo
(
buffer
);
String
expectedJson
=
"{\"assignees\":[\"someUsername\"],\"body\":\"description\","
+
"\"title\":\"title of ticket\",\"labels\":[\"bug\",\"unifiedticketing\"]}"
;
assertEquals
(
mapper
.
readTree
(
expectedJson
),
mapper
.
readTree
(
buffer
.
readUtf8
()));
}
}
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