Skip to content

Commit 8c96cb4

Browse files
author
alex
committed
add new methods
1 parent 076f7fb commit 8c96cb4

4 files changed

Lines changed: 187 additions & 73 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
target
2+
corezoid-sdk.iml
3+
.idea

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.corezoid</groupId>
66
<artifactId>corezoid-sdk</artifactId>
7-
<version>2.0</version>
7+
<version>2.1</version>
88
<packaging>jar</packaging>
99

1010
<name>corezoid-sdk-${project.version}</name>

src/main/java/com/corezoid/sdk/entity/RequestOperation.java

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,52 @@ public class RequestOperation {
2121
public static RequestOperation create(String convId, String ref,
2222
JSONObject data) {
2323

24-
return new RequestOperation(Query.create, convId, ref, data);
24+
return new RequestOperation(Query.create, convId, ref, null, data);
2525
}
2626
//----------------------------------------------------------------------------------------------------------------------
2727

2828
/**
29-
* Builder for 'Modify task' operation
29+
* @deprecated, please use modifyId or modifyRef methods
30+
* @param convId
31+
* @param ref
32+
* @param data
33+
* @return
34+
*/
35+
@Deprecated
36+
public static RequestOperation modify(String convId, String ref,
37+
JSONObject data) {
38+
39+
return new RequestOperation(Query.modify, convId, ref, null, data);
40+
}
41+
//------------------------------------------------------------------------------------------------------------------
42+
43+
/**
44+
* Builder for 'Modify task by id' operation
45+
*
46+
* @param convId - conveyor id
47+
* @param taskId - task id
48+
* @param data -task data
49+
* @return
50+
*/
51+
public static RequestOperation modifyId(String convId, String taskId,
52+
JSONObject data) {
53+
54+
return new RequestOperation(Query.modify, convId, null, taskId, data);
55+
}
56+
//------------------------------------------------------------------------------------------------------------------
57+
58+
/**
59+
* Builder for 'Modify task by ref' operation
3060
*
3161
* @param convId - conveyor id
3262
* @param ref - task reference
3363
* @param data -task data
3464
* @return
3565
*/
36-
public static RequestOperation modify(String convId, String ref,
37-
JSONObject data) {
38-
39-
return new RequestOperation(Query.modify, convId, ref, data);
66+
public static RequestOperation modifyRef(String convId, String ref,
67+
JSONObject data) {
68+
69+
return new RequestOperation(Query.modify, convId, ref, null, data);
4070
}
4171
//----------------------------------------------------------------------------------------------------------------------
4272

@@ -46,36 +76,49 @@ public static RequestOperation modify(String convId, String ref,
4676
* @param ref - task reference
4777
* @param data - task data
4878
*/
49-
private RequestOperation(Query type, String convId, String ref,
79+
private RequestOperation(Query type, String convId, String ref, String objId,
5080
JSONObject data) {
5181
if(convId == null || convId.equals("") ){
5282
throw new IllegalArgumentException("convId is null or empty");
5383
}
54-
if(ref == null || ref.equals("") ){
55-
throw new IllegalArgumentException("ref is null or empty");
84+
if(objId == null && ref == null ){
85+
throw new IllegalArgumentException("ref and taskId is null");
86+
}
87+
if(ref == null && objId.equals("") ){
88+
throw new IllegalArgumentException("taskId is empty");
89+
}
90+
if(objId == null && ref.equals("") ){
91+
throw new IllegalArgumentException("ref is empty");
5692
}
5793
if(data == null ){
5894
throw new IllegalArgumentException("data is null");
5995
}
6096
this.ref = ref;
97+
this.objId = objId;
6198
this.type = type;
6299
this.obj = "task";
63100
this.convId = convId;
64101
this.data = data;
65102
}
66103

67-
//----------------------------------------------------------------------------------------------------------------------
104+
//----------------------------------------------------------------------------------------------------------------------
68105
public JSONObject toJSONObject() {
69-
JSONObject res = new JSONObject()
70-
.element("ref", ref)
71-
.element("conv_id", convId)
72-
.element("type", type)
73-
.element("obj", obj)
74-
.element("data", data);
106+
JSONObject res = new JSONObject();
107+
if(ref!= null){
108+
res.element("ref", ref);
109+
}
110+
if(objId!= null){
111+
res.element("obj_id", objId);
112+
}
113+
res.element("conv_id", convId);
114+
res.element("type", type);
115+
res.element("obj", obj);
116+
res.element("data", data);
75117
return res;
76118
}
77-
//----------------------------------------------------------------------------------------------------------------------
119+
//----------------------------------------------------------------------------------------------------------------------
78120
private final String convId;
121+
private final String objId;
79122
private final String ref;
80123
private final Query type;
81124
private final String obj;

src/test/java/com/corezoid/sdk/test/UtiisTest.java

Lines changed: 124 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ public void testCheckSignTrue() {
3737
String key = "tgGsR8FTuLXzzVBsp3rngeSgOB2E2pMFJDwAXarbrs92GmLiiy";
3838
String time = "1389956320";
3939
String content = "{\n"
40-
+ " \"ops\":[\n"
41-
+ " {\n"
42-
+ " \"ref\":null,\n"
43-
+ " \"type\":\"data\",\n"
44-
+ " \"obj_id\":\"52d8d74610e87b10d98c8d90\",\n"
45-
+ " \"conv_id\":\"270\",\n"
46-
+ " \"node_id\":\"52d837d210e87b10d979e98c\",\n"
47-
+ " \"data\":{\n"
48-
+ " \"id\":1077413\n"
49-
+ " },\n"
50-
+ " \"extra\":{\n"
51-
+ " \"id\":\"1412737\",\n"
52-
+ " \"hide_products\":\"N\",\n"
53-
+ " \"properties\":\"Y\",\n"
54-
+ " \"show_all\":\"Y\",\n"
55-
+ " \"codeProduct\" : \"CRED\" \n"
56-
+ " }\n"
57-
+ " }\n"
58-
+ " ]\n"
59-
+ "}";
40+
+ " \"ops\":[\n"
41+
+ " {\n"
42+
+ " \"ref\":null,\n"
43+
+ " \"type\":\"data\",\n"
44+
+ " \"obj_id\":\"52d8d74610e87b10d98c8d90\",\n"
45+
+ " \"conv_id\":\"270\",\n"
46+
+ " \"node_id\":\"52d837d210e87b10d979e98c\",\n"
47+
+ " \"data\":{\n"
48+
+ " \"id\":1077413\n"
49+
+ " },\n"
50+
+ " \"extra\":{\n"
51+
+ " \"id\":\"1412737\",\n"
52+
+ " \"hide_products\":\"N\",\n"
53+
+ " \"properties\":\"Y\",\n"
54+
+ " \"show_all\":\"Y\",\n"
55+
+ " \"codeProduct\" : \"CRED\" \n"
56+
+ " }\n"
57+
+ " }\n"
58+
+ " ]\n"
59+
+ "}";
6060
boolean expResult = true;
6161
boolean result = CorezoidMessage.checkSign(sign, key, time, content);
6262
assertEquals(expResult, result);
@@ -72,26 +72,26 @@ public void testCheckSignFalse() {
7272
String key = "tgGsR8FTuLXzzVBsp3rngeSgOB2E2pMFJDwAXarbrs92GmLiiy";
7373
String time = "1389956320";
7474
String content = "{\n"
75-
+ " \"ops\":[\n"
76-
+ " {\n"
77-
+ " \"ref\":null,\n"
78-
+ " \"type\":\"data\",\n"
79-
+ " \"obj_id\":\"52d8d74610e87b10d98c8d90\",\n"
80-
+ " \"conv_id\":\"270\",\n"
81-
+ " \"node_id\":\"52d837d210e87b10d979e98c\",\n"
82-
+ " \"data\":{\n"
83-
+ " \"id\":1077413\n"
84-
+ " },\n"
85-
+ " \"extra\":{\n"
86-
+ " \"id\":\"1412737\",\n"
87-
+ " \"hide_products\":\"N\",\n"
88-
+ " \"properties\":\"Y\",\n"
89-
+ " \"show_all\":\"Y\",\n"
90-
+ " \"codeProduct\" : \"CRED\" \n"
91-
+ " }\n"
92-
+ " }\n"
93-
+ " ]\n"
94-
+ "}";
75+
+ " \"ops\":[\n"
76+
+ " {\n"
77+
+ " \"ref\":null,\n"
78+
+ " \"type\":\"data\",\n"
79+
+ " \"obj_id\":\"52d8d74610e87b10d98c8d90\",\n"
80+
+ " \"conv_id\":\"270\",\n"
81+
+ " \"node_id\":\"52d837d210e87b10d979e98c\",\n"
82+
+ " \"data\":{\n"
83+
+ " \"id\":1077413\n"
84+
+ " },\n"
85+
+ " \"extra\":{\n"
86+
+ " \"id\":\"1412737\",\n"
87+
+ " \"hide_products\":\"N\",\n"
88+
+ " \"properties\":\"Y\",\n"
89+
+ " \"show_all\":\"Y\",\n"
90+
+ " \"codeProduct\" : \"CRED\" \n"
91+
+ " }\n"
92+
+ " }\n"
93+
+ " ]\n"
94+
+ "}";
9595
boolean expResult = false;
9696
boolean result = CorezoidMessage.checkSign(sign, key, time, content);
9797
assertEquals(expResult, result);
@@ -106,11 +106,11 @@ public void testCheckSignFalse() {
106106
*/
107107
public void testCheckAnswerReturnsNotEmptyMap() throws Exception {
108108
String answerOk = "{\n"
109-
+ " \"request_proc\":\"ok\",\n"
110-
+ " \"ops\":[\n"
111-
+ " {\"id\":\"\",\"proc\":\"ok\",\"obj\":\"task\",\"ref\":\"PB11345969838\",\"obj_id\":\"52e266a05f845f6ec484d611\"}\n"
112-
+ " ]\n"
113-
+ "}";
109+
+ " \"request_proc\":\"ok\",\n"
110+
+ " \"ops\":[\n"
111+
+ " {\"id\":\"\",\"proc\":\"ok\",\"obj\":\"task\",\"ref\":\"PB11345969838\",\"obj_id\":\"52e266a05f845f6ec484d611\"}\n"
112+
+ " ]\n"
113+
+ "}";
114114
Map<String, String> map = CorezoidMessage.parseAnswer(answerOk);
115115
assertEquals(true, !map.isEmpty());
116116
System.out.println(">>testCheckAnswerReturnsNotEmptyMap :\t" + map.toString());
@@ -124,11 +124,11 @@ public void testCheckAnswerReturnsNotEmptyMap() throws Exception {
124124
*/
125125
public void testcheckAnswerShouldThrowException() throws Exception {
126126
String answerFail = "{\n"
127-
+ " \"request_proc\":\"fail\",\n"
128-
+ " \"ops\":[\n"
129-
+ " {\"id\":\"\",\"proc\":\"ok\",\"obj\":\"task\",\"ref\":\"PB11345969838\",\"obj_id\":\"52e266a05f845f6ec484d611\"}\n"
130-
+ " ]\n"
131-
+ "}";
127+
+ " \"request_proc\":\"fail\",\n"
128+
+ " \"ops\":[\n"
129+
+ " {\"id\":\"\",\"proc\":\"ok\",\"obj\":\"task\",\"ref\":\"PB11345969838\",\"obj_id\":\"52e266a05f845f6ec484d611\"}\n"
130+
+ " ]\n"
131+
+ "}";
132132
try {
133133
Map<String, String> map = CorezoidMessage.parseAnswer(answerFail);
134134
fail("Exception was expected");
@@ -233,7 +233,7 @@ public void testGetConAnswerMessage() {
233233
/**
234234
* Requst builder tests
235235
*/
236-
public void testGetConvQueryMessage() {
236+
public void testGetConvQueryCreateMessage() {
237237
JSONObject data = new JSONObject()
238238
.element("phone", "1")
239239
.element("card", "2");
@@ -242,16 +242,54 @@ public void testGetConvQueryMessage() {
242242
String key = "123";
243243
String apiLogin = "12345";
244244
CorezoidMessage mes = CorezoidMessage.request(key, apiLogin, Arrays.asList(RequestOperation.create(conv_id, ref, data)));
245-
System.out.println(">>testGetConvQueryMessage :");
245+
System.out.println(">>testGetConvQueryCreateMessage :");
246246
System.out.println("body \t\t" + mes.body);
247247
System.out.println("url \t\t " + mes.url);
248248
String expBody = "{\"ops\":[{\"ref\":\"11\",\"conv_id\":\"1234\",\"type\":\"create\",\"obj\":\"task\",\"data\":{\"phone\":\"1\",\"card\":\"2\"}}]}";
249249
assertEquals(expBody, mes.body);
250250

251251
}
252-
/**
253-
* MessageBuilder Exceptions test
254-
*/
252+
/**
253+
* Requst builder tests
254+
*/
255+
public void testGetConvQueryModifyRefMessage() {
256+
JSONObject data = new JSONObject()
257+
.element("phone", "1")
258+
.element("card", "2");
259+
String ref = "11";
260+
String conv_id = "1234";
261+
String key = "123";
262+
String apiLogin = "12345";
263+
CorezoidMessage mes = CorezoidMessage.request(key, apiLogin, Arrays.asList(RequestOperation.modifyRef(conv_id, ref, data)));
264+
System.out.println(">>testGetConvQueryModifyRefMessage :");
265+
System.out.println("body \t\t" + mes.body);
266+
System.out.println("url \t\t " + mes.url);
267+
String expBody = "{\"ops\":[{\"ref\":\"11\",\"conv_id\":\"1234\",\"type\":\"modify\",\"obj\":\"task\",\"data\":{\"phone\":\"1\",\"card\":\"2\"}}]}";
268+
assertEquals(expBody, mes.body);
269+
270+
}
271+
/**
272+
* Requst builder tests
273+
*/
274+
public void testGetConvQueryModifyIdMessage() {
275+
JSONObject data = new JSONObject()
276+
.element("phone", "1")
277+
.element("card", "2");
278+
String taskId = "11";
279+
String conv_id = "1234";
280+
String key = "123";
281+
String apiLogin = "12345";
282+
CorezoidMessage mes = CorezoidMessage.request(key, apiLogin, Arrays.asList(RequestOperation.modifyId(conv_id, taskId, data)));
283+
System.out.println(">>testGetConvQueryModifyRefMessage :");
284+
System.out.println("body \t\t" + mes.body);
285+
System.out.println("url \t\t " + mes.url);
286+
String expBody = "{\"ops\":[{\"obj_id\":\"11\",\"conv_id\":\"1234\",\"type\":\"modify\",\"obj\":\"task\",\"data\":{\"phone\":\"1\",\"card\":\"2\"}}]}";
287+
assertEquals(expBody, mes.body);
288+
289+
}
290+
/**
291+
* MessageBuilder Exceptions test
292+
*/
255293
public void testConveyorQueryShouldThrowException() {
256294
JSONObject data = new JSONObject()
257295
.element("phone", "1")
@@ -292,4 +330,35 @@ public void testConveyorQueryShouldThrowException() {
292330
System.out.println(">>testConveyorQueryBuilderShouldThrowException :\t" + ex.getMessage());
293331
}
294332
}
333+
public void testRequestOperationShouldThrowException(){
334+
JSONObject data = new JSONObject()
335+
.element("phone", "1")
336+
.element("card", "2");
337+
String ref = "11";
338+
String taskId = "12";
339+
String conv_id = "1234";
340+
String apiSecret = "1234";
341+
String apiLogin = "12345";
342+
RequestOperation op = null;
343+
try {
344+
op = RequestOperation.modifyId(conv_id, null, data);
345+
}catch (Exception ex){
346+
System.out.println(">>testRequestOperationShouldThrowException :\t" + ex.getMessage());
347+
}
348+
try {
349+
op = RequestOperation.modifyId(conv_id, "", data);
350+
}catch (Exception ex){
351+
System.out.println(">>testRequestOperationShouldThrowException :\t" + ex.getMessage());
352+
}
353+
try {
354+
op = RequestOperation.modifyRef(conv_id, null, data);
355+
}catch (Exception ex){
356+
System.out.println(">>testRequestOperationShouldThrowException :\t" + ex.getMessage());
357+
}
358+
try {
359+
op = RequestOperation.modifyRef(conv_id, "", data);
360+
}catch (Exception ex){
361+
System.out.println(">>testRequestOperationShouldThrowException :\t" + ex.getMessage());
362+
}
363+
}
295364
}

0 commit comments

Comments
 (0)