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
J
Java Client API
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Genesys PGR
Java Client API
Commits
df07a9ad
Commit
df07a9ad
authored
Mar 16, 2015
by
Matija Obreza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
API updates
parent
c9483a89
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
65 additions
and
105 deletions
+65
-105
src/main/java/org/genesys2/client/oauth/GenesysClient.java
src/main/java/org/genesys2/client/oauth/GenesysClient.java
+48
-76
src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java
...rg/genesys2/client/oauth/api/accession/AccessionJson.java
+3
-3
src/test/java/org/geneys2/client/oauth/AccessionApiTest.java
src/test/java/org/geneys2/client/oauth/AccessionApiTest.java
+5
-8
src/test/java/org/geneys2/client/oauth/ApiTest.java
src/test/java/org/geneys2/client/oauth/ApiTest.java
+3
-12
src/test/java/org/geneys2/client/oauth/MockGenesysServer.java
...test/java/org/geneys2/client/oauth/MockGenesysServer.java
+3
-5
src/test/java/org/geneys2/client/oauth/ModelTests.java
src/test/java/org/geneys2/client/oauth/ModelTests.java
+3
-1
No files found.
src/main/java/org/genesys2/client/oauth/GenesysClient.java
View file @
df07a9ad
...
...
@@ -30,6 +30,7 @@ import org.apache.commons.lang.StringUtils;
import
org.apache.log4j.LogManager
;
import
org.apache.log4j.Logger
;
import
org.genesys2.client.oauth.api.GenesysApi
;
import
org.genesys2.client.oauth.api.accession.AccessionJson
;
import
org.scribe.builder.ServiceBuilder
;
import
org.scribe.exceptions.OAuthConnectionException
;
import
org.scribe.exceptions.OAuthException
;
...
...
@@ -40,7 +41,10 @@ import org.scribe.model.Verb;
import
org.scribe.model.Verifier
;
import
org.scribe.oauth.OAuthService
;
import
com.fasterxml.jackson.annotation.JsonInclude.Include
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
...
...
@@ -59,7 +63,7 @@ public class GenesysClient {
private
static
final
String
SCOPE
=
"read,write"
;
/** The mapper. */
private
static
ObjectMapper
m
apper
=
new
ObjectMapper
();
private
static
ObjectMapper
objectM
apper
=
new
ObjectMapper
();
/** The service. */
private
OAuthService
service
;
...
...
@@ -79,6 +83,13 @@ public class GenesysClient {
/** The api secret. */
private
String
apiSecret
;
static
{
objectMapper
=
new
ObjectMapper
();
objectMapper
.
configure
(
SerializationFeature
.
WRITE_ENUMS_USING_TO_STRING
,
true
);
objectMapper
.
configure
(
SerializationFeature
.
WRITE_EMPTY_JSON_ARRAYS
,
false
);
objectMapper
.
setSerializationInclusion
(
Include
.
NON_NULL
);
}
/**
* Instantiates a new genesys client.
*/
...
...
@@ -364,7 +375,7 @@ public class GenesysClient {
* @return the object node
*/
public
static
ObjectNode
makeAid3
(
String
instCode
,
String
genus
,
String
acceNumb
)
{
ObjectNode
json
=
m
apper
.
createObjectNode
();
ObjectNode
json
=
objectM
apper
.
createObjectNode
();
json
.
put
(
"instCode"
,
instCode
);
json
.
put
(
"acceNumb"
,
acceNumb
);
json
.
put
(
"genus"
,
genus
);
...
...
@@ -372,7 +383,7 @@ public class GenesysClient {
}
/**
* Update
mls.
* Update
MLS status of accessions
*
* @param instCode
* the inst code
...
...
@@ -381,103 +392,64 @@ public class GenesysClient {
* @return the string
* @throws GenesysApiException
* the genesys api exception
* @throws JsonProcessingException
* @throws PleaseRetryException
*
* @deprecated Please use {@link #updateAccessions(String, Collection)} with
* only the instCode, acceNumb, (genus, ) and mlsStat provided.
*/
public
String
updateMLS
(
String
instCode
,
Collection
<
ObjectNode
>
accns
)
throws
GenesysApiException
{
ArrayNode
arr
=
toArrayNode
(
accns
);
_log
.
debug
(
"Sending: "
+
arr
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/update"
,
null
,
arr
.
toString
());
}
/**
* Accession exists.
*
* @param instCode
* the inst code
* @param accns
* the accns
* @return the string
* @throws GenesysApiException
* the genesys api exception
*/
public
String
accessionExists
(
String
instCode
,
Collection
<
ObjectNode
>
accns
)
throws
GenesysApiException
{
ArrayNode
arr
=
toArrayNode
(
accns
);
_log
.
debug
(
"Sending: "
+
arr
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/check"
,
null
,
arr
.
toString
());
}
/**
* Update accessions.
*
* @param instCode
* the inst code
* @param accns
* the accns
* @return the string
* @throws GenesysApiException
* the genesys api exception
* @throws InterruptedException
* the interrupted exception
*/
public
String
updateAccessions
(
String
instCode
,
Collection
<
ObjectNode
>
accns
)
throws
GenesysApiException
,
InterruptedException
{
@Deprecated
public
String
updateMLS
(
String
instCode
,
Collection
<
ObjectNode
>
accns
)
throws
GenesysApiException
,
PleaseRetryException
,
JsonProcessingException
{
if
(
accns
==
null
||
accns
.
size
()
==
0
)
{
return
null
;
}
return
updateAccessions
(
instCode
,
toArrayNode
(
accns
));
}
/**
* To array node.
*
* @param accns
* the accns
* @return the array node
*/
public
static
ArrayNode
toArrayNode
(
Collection
<
ObjectNode
>
accns
)
{
ArrayNode
arr
=
mapper
.
createArrayNode
();
for
(
ObjectNode
accn
:
accns
)
{
arr
.
add
(
accn
);
}
return
arr
;
_log
.
debug
(
"Sending: "
+
accns
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/update"
,
null
,
objectMapper
.
writeValueAsString
(
accns
));
}
/**
* Update accessions.
*
* Update accession information with new values provided in the JSON string.
* In case of {@link PleaseRetryException}, this method will attempt to
* re-send the data 5 times before giving up.
*
* @param instCode
* the inst code
* @param arr
* the arr
* @return the string
* @throws OAuthAuthenticationException
* the o auth authentication exception
* the WIEWS institute code
* @param jsonAccessionList
* the JSON array of accessions
* @return "OK"
* @throws GenesysApiException
*
the genesys api exception
*
when data is not valid
* @throws InterruptedException
* the interrupted exception
* when thread was interrupted during sleep between retries
*
*/
public
String
updateAccessions
(
String
instCode
,
ArrayNode
arr
)
throws
OAuthAuthenticationException
,
GenesysApiException
,
InterruptedException
{
_log
.
debug
(
"Sending: "
+
arr
);
public
String
updateAccessions
(
String
instCode
,
String
jsonAccessionList
)
throws
GenesysApiException
,
InterruptedException
{
for
(
int
retry
=
0
;
retry
<
5
;
retry
++)
{
try
{
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/upsert"
,
null
,
arr
.
toString
()
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/upsert"
,
null
,
jsonAccessionList
);
}
catch
(
PleaseRetryException
e
)
{
long
sleepTime
=
(
long
)
(
Math
.
pow
(
2
,
retry
)
*
100
+
Math
.
pow
(
2
,
retry
)
*
2500
*
Math
.
random
());
_log
.
warn
(
"Retrying PUT after "
+
sleepTime
+
" ms."
);
Thread
.
sleep
(
sleepTime
);
}
}
throw
new
RuntimeException
(
"All retries failed"
);
}
public
String
updateAccessions
(
String
instCode
,
Collection
<
AccessionJson
>
accns
)
throws
GenesysApiException
,
InterruptedException
,
JsonProcessingException
{
if
(
accns
==
null
||
accns
.
size
()
==
0
)
{
return
null
;
}
return
updateAccessions
(
instCode
,
objectMapper
.
writeValueAsString
(
accns
));
}
/**
* Update organization members.
*
...
...
src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java
View file @
df07a9ad
...
...
@@ -21,7 +21,7 @@ import org.genesys2.client.oauth.api.accession.Api1Constants.Accession;
import
com.fasterxml.jackson.annotation.JsonProperty
;
public
class
AccessionJson
{
private
long
version
=
0
;
private
Long
version
;
private
Long
genesysId
;
@JsonProperty
(
value
=
Accession
.
INSTCODE
)
...
...
@@ -79,11 +79,11 @@ public class AccessionJson {
@JsonProperty
(
value
=
Accession
.
HISTORIC
)
private
Boolean
historic
;
public
l
ong
getVersion
()
{
public
L
ong
getVersion
()
{
return
version
;
}
public
void
setVersion
(
l
ong
l
)
{
public
void
setVersion
(
L
ong
l
)
{
this
.
version
=
l
;
}
...
...
src/test/java/org/geneys2/client/oauth/AccessionApiTest.java
View file @
df07a9ad
...
...
@@ -43,7 +43,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
AccessionApiTest
{
...
...
@@ -66,11 +65,11 @@ public class AccessionApiTest {
@Before
public
void
initMocks
()
throws
OAuthAuthenticationException
,
PleaseRetryException
,
GenesysApiException
,
InterruptedException
{
InterruptedException
,
JsonProcessingException
{
System
.
err
.
println
(
"Initializing mocks"
);
when
(
genesysClient
.
listAccessions
(
anyString
(),
anyInt
(),
Matchers
.
isNull
(
String
.
class
))).
then
(
mockServer
.
listAccessions
());
when
(
genesysClient
.
updateAccessions
(
anyString
(),
anyCollectionOf
(
ObjectNode
.
class
))).
then
(
when
(
genesysClient
.
updateAccessions
(
anyString
(),
anyCollectionOf
(
AccessionJson
.
class
))).
then
(
mockServer
.
updateAccessions
());
}
...
...
@@ -115,19 +114,17 @@ public class AccessionApiTest {
assertThat
(
"Expected empty list"
,
results
.
size
(),
is
(
0
));
List
<
ObjectNode
>
nodeList
=
new
ArrayList
<
ObjectNode
>();
List
<
AccessionJson
>
ajList
=
new
ArrayList
<
AccessionJson
>();
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
AccessionJson
aj
=
new
AccessionJson
();
aj
.
setInstCode
(
instCode
);
aj
.
setAcceNumb
(
"ACC-"
+
(
i
+
1
));
aj
.
setGenus
(
"Genus"
);
ObjectNode
node
=
(
ObjectNode
)
objectMapper
.
readTree
(
objectMapper
.
writeValueAsString
(
aj
));
nodeList
.
add
(
node
);
ajList
.
add
(
aj
);
}
result
=
genesysClient
.
updateAccessions
(
instCode
,
node
List
);
result
=
genesysClient
.
updateAccessions
(
instCode
,
aj
List
);
System
.
err
.
println
(
result
);
results
=
objectMapper
.
readValue
(
genesysClient
.
listAccessions
(
instCode
,
1
,
null
),
...
...
src/test/java/org/geneys2/client/oauth/ApiTest.java
View file @
df07a9ad
...
...
@@ -127,32 +127,23 @@ public class ApiTest {
};
}
@SuppressWarnings
(
"deprecation"
)
@Test
(
expected
=
PleaseRetryException
.
class
)
public
void
testPleaseRetryException
()
throws
OAuthAuthenticationException
,
PleaseRetryException
,
GenesysApiException
{
GenesysApiException
,
JsonProcessingException
{
when
(
genesysClient
.
updateMLS
(
"INS000"
,
null
)).
thenThrow
(
PleaseRetryException
.
class
);
genesysClient
.
updateMLS
(
"INS000"
,
null
);
}
@Test
(
expected
=
NullPointerException
.
class
)
public
void
testNullUpdate
()
throws
OAuthAuthenticationException
,
PleaseRetryException
,
GenesysApiException
{
when
(
genesysClient
.
updateMLS
(
"INS000"
,
null
)).
thenCallRealMethod
();
genesysClient
.
updateMLS
(
"INS000"
,
null
);
}
@Test
public
void
test2
()
throws
OAuthAuthenticationException
,
PleaseRetryException
,
GenesysApiException
{
when
(
genesysClient
.
query
(
Mockito
.
isA
(
Verb
.
class
),
anyString
(),
anyMap
(),
anyString
())).
thenCallRealMethod
();
when
(
genesysClient
.
updateMLS
(
anyString
(),
anyCollection
())).
thenCallRealMethod
();
when
(
genesysClient
.
query
(
Mockito
.
isA
(
Verb
.
class
),
anyString
(),
anyMapOf
(
String
.
class
,
String
.
class
),
anyString
())).
thenCallRealMethod
();
Collection
<
ObjectNode
>
list
=
new
ArrayList
<
ObjectNode
>();
list
.
add
(
objectMapper
.
createObjectNode
().
put
(
Accession
.
INSTCODE
,
"INS000"
).
put
(
Accession
.
ACCENUMB
,
"ACC-1"
)
.
put
(
Accession
.
GENUS
,
"Genus"
));
String
x
=
genesysClient
.
updateMLS
(
"INS000"
,
list
);
System
.
out
.
println
(
x
);
}
}
src/test/java/org/geneys2/client/oauth/MockGenesysServer.java
View file @
df07a9ad
...
...
@@ -31,7 +31,6 @@ import org.mockito.stubbing.Answer;
import
com.fasterxml.jackson.annotation.JsonInclude.Include
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
...
...
@@ -76,7 +75,7 @@ public class MockGenesysServer {
public
String
answer
(
InvocationOnMock
invocation
)
throws
Throwable
{
Object
[]
args
=
invocation
.
getArguments
();
String
instCode
=
(
String
)
args
[
0
];
List
arrayNode
=
(
List
)
args
[
1
];
List
<?>
arrayNode
=
(
List
<?>
)
args
[
1
];
String
res
=
objectMapper
.
writeValueAsString
(
upsertAccessions
(
instCode
,
arrayNode
));
System
.
err
.
println
(
"Result: "
+
res
);
...
...
@@ -85,15 +84,14 @@ public class MockGenesysServer {
};
}
protected
String
upsertAccessions
(
final
String
instCode
,
List
nodeList
)
throws
JsonParseException
,
protected
String
upsertAccessions
(
final
String
instCode
,
List
<?>
nodeList
)
throws
JsonParseException
,
JsonMappingException
,
IOException
,
GenesysApiException
{
List
<
AccessionJson
>
accList
=
instAcc
.
get
(
instCode
);
if
(
accList
==
null
)
{
instAcc
.
put
(
instCode
,
accList
=
new
ArrayList
<
AccessionJson
>());
}
for
(
int
i
=
0
;
i
<
nodeList
.
size
();
i
++)
{
JsonNode
node
=
(
JsonNode
)
nodeList
.
get
(
i
);
AccessionJson
aj
=
objectMapper
.
readValue
(
node
.
toString
(),
AccessionJson
.
class
);
AccessionJson
aj
=
(
AccessionJson
)
nodeList
.
get
(
i
);
AccessionJson
existing
=
findMatch
(
accList
,
aj
);
merge
(
accList
,
existing
,
aj
);
}
...
...
src/test/java/org/geneys2/client/oauth/ModelTests.java
View file @
df07a9ad
...
...
@@ -22,9 +22,9 @@ import static org.hamcrest.MatcherAssert.*;
import
java.io.IOException
;
import
org.genesys2.client.oauth.api.accession.AccessionJson
;
import
org.genesys2.client.oauth.api.accession.GeoJson
;
import
org.genesys2.client.oauth.api.accession.Api1Constants.Accession
;
import
org.genesys2.client.oauth.api.accession.Api1Constants.Geo
;
import
org.genesys2.client.oauth.api.accession.GeoJson
;
import
org.junit.Test
;
import
com.fasterxml.jackson.annotation.JsonInclude.Include
;
...
...
@@ -52,6 +52,7 @@ public class ModelTests {
aj
.
setGenus
(
"Genus"
);
JsonNode
tree
=
getJsonTree
(
aj
);
System
.
err
.
println
(
tree
.
toString
());
assertThat
(
"JSON elements count mismatch"
,
tree
,
JsonNodeMatchers
.
withSize
(
3
));
assertThat
(
"No INSTCODE"
,
tree
.
get
(
Accession
.
INSTCODE
),
notNullValue
());
assertThat
(
"INSTCODE must be a text node"
,
tree
.
get
(
Accession
.
INSTCODE
),
JsonNodeMatchers
.
isTextNode
());
...
...
@@ -85,6 +86,7 @@ public class ModelTests {
aj
.
setStorage
(
new
Integer
[]
{
1
,
2
,
3
,
4
});
JsonNode
tree
=
getJsonTree
(
aj
);
System
.
err
.
println
(
tree
.
toString
());
assertThat
(
"JSON elements count mismatch"
,
tree
,
JsonNodeMatchers
.
withSize
(
1
));
assertThat
(
"STORAGE must be an array"
,
tree
.
get
(
Accession
.
STORAGE
),
JsonNodeMatchers
.
isArrayNode
());
}
...
...
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