Commit 034811a3 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

rest query

parent 9ad35fe6
......@@ -9,9 +9,12 @@ repositories {
mavenCentral()
}
def jacksonVersion = '1.9.2'
dependencies {
// compile group: 'cglib', name: 'cglib', version: '3.2.4'
testCompile group: 'org.testng', name: 'testng', version: '6.8'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile(group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: jacksonVersion, transitive: false)
compile(group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: jacksonVersion, transitive: false)
}
package kz.arta.synergy.api;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
public class JsonUtils {
private static final ObjectMapper MAPPER = new ObjectMapper();
private JsonUtils() {
}
public static String toJson(Object object) throws IOException {
return MAPPER.writeValueAsString(object);
}
public static JsonNode readTree(String content) throws IOException {
return MAPPER.readTree(content);
}
public static ObjectMapper getMapper() {
return MAPPER;
}
public static <T> T read(String result, Class<T> synResponseClass) throws IOException {
return MAPPER.readValue(result, synResponseClass);
}
public static <T> T read(String result, TypeReference<T> typeReference) throws IOException {
return MAPPER.readValue(result, typeReference);
}
public static String getValueByKey(String jsonData, String key) throws IOException {
return MAPPER.readTree(jsonData).path(key).getTextValue();
}
}
......@@ -22,20 +22,30 @@ import java.util.Map;
public class RestHttpQuery {
public static final int TIMEOUT = 10*60*1000;
public String doGetQuery(QueryContext context, String query) throws IOException {
private QueryContext context;
public RestHttpQuery(QueryContext context) {
this.context = context;
}
public String doGetQuery(String query) throws IOException {
URL url = new URL(context.getAddress() + query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json; charset=utf-8");
authentication(context, conn);
authentication(conn);
return readStringReturn(conn);
}
public String doGetQuery(QueryContext context, String query, Map<String, ?> params) throws IOException {
return doGetQuery(context, makeQuery(query, params));
/*public String doPostQuery() {
}*/
public String doGetQuery(String query, Map<String, ?> params) throws IOException {
return doGetQuery(makeQuery(query, params));
}
private String makeQuery(String query, Map<String, ?> params) {
......@@ -75,7 +85,7 @@ public class RestHttpQuery {
return result.toString();
}
private void authentication(QueryContext context, HttpURLConnection conn) {
private void authentication(HttpURLConnection conn) {
if (context.getLogin() != null && context.getPassword() != null) {
conn.setRequestProperty("Authorization", "Basic " + context.getAuthHeader());
}
......
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
import kz.arta.synergy.api.asforms.pojo.*;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
......@@ -17,20 +21,24 @@ import java.util.Set;
*/
public class AsFormProvider {
private AsFormProvider() {
private RestHttpQuery restHttpQuery;
private AsFormProvider(QueryContext context) {
this.restHttpQuery = new RestHttpQuery(context);
}
public static AsFormProvider newInstance() {
return new AsFormProvider();
public static AsFormProvider newInstance(QueryContext context) {
return new AsFormProvider(context);
}
public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams advancedSearchParams) {
return new ArrayList<AdvancedSearchResult>();
}
/*public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) {
return create(formClass, asfData);
}*/
public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) throws IOException {
String data = restHttpQuery.doGetQuery("/rest/api/asforms/data/" + dataUUID);
return create(formClass, JsonUtils.read(data, AsFormWrapper.class));
}
public <T extends AsForm> T create(Class<T> asFormClass, AsFormWrapper asfData) {
T asForm = createAsFormObject(asFormClass, asfData, null);
......@@ -41,9 +49,11 @@ public class AsFormProvider {
return asForm;
}
/*public <T extends AsForm> String save(T asForm) {
}*/
public <T extends AsForm> String save(T asForm) throws IOException {
AsFormWrapper asFormWrapper = toAsfData(asForm);
JsonUtils.toJson(asFormWrapper);
return null;
}
public <T extends AsForm> AsFormWrapper toAsfData(T asForm) {
AsFormWrapper asFormWrapper = new AsFormWrapper();
......@@ -301,4 +311,12 @@ public class AsFormProvider {
}
return fetchKey;
}
public RestHttpQuery getRestHttpQuery() {
return restHttpQuery;
}
public void setRestHttpQuery(RestHttpQuery restHttpQuery) {
this.restHttpQuery = restHttpQuery;
}
}
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -8,6 +10,7 @@ import java.util.List;
* @author raimbek
* @since 02.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AdvancedSearchParams {
private String query;
private List<String> parameters = new ArrayList<String>();
......
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
/**
* @author raimbek
* @since 02.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AdvancedSearchResult {
private String dataUUID;
private String documentID;
......
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
/**
* @author raimbek
* @since 09.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AsForm {
private String nodeUUID;
......
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import java.util.List;
/**
* @author raimbek
* @since 09.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AsFormData extends AsFormDataContainer {
private String id;
private String type;
......
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.List;
......@@ -7,6 +9,7 @@ import java.util.List;
* @author raimbek
* @since 10.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AsFormDataContainer {
private List<AsFormData> data;
......
package kz.arta.synergy.api.asforms.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
/**
* @author raimbek
* @since 09.11.2016
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AsFormWrapper extends AsFormDataContainer {
private String nodeUUID;
private String uuid;
private String form;
private String modified;
private String version;
public AsFormWrapper() {
}
......@@ -44,4 +48,12 @@ public class AsFormWrapper extends AsFormDataContainer {
public void setModified(String modified) {
this.modified = modified;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.SynergyApiProvider;
import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.asforms.pojo.AsFormData;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
import kz.arta.synergy.api.asforms.pojo.ComponentTypes;
import kz.arta.synergy.pojo.TableCmp;
import kz.arta.synergy.pojo.TestForm;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
/**
......@@ -36,8 +36,8 @@ public class AsFormProviderTest {
AsFormProvider asFormProvider;
@BeforeClass
public void createAsfWrapper() {
AsFormWrapper asFormWrapper = new AsFormWrapper();
public void createAsfWrapper() throws IOException {
/*AsFormWrapper asFormWrapper = new AsFormWrapper();
asFormWrapper.addData(AsFormData.textbox("text_input", expectedTextInputValue));
asFormWrapper.addData(AsFormData.textbox("double_input", expectedDoubleValue));
asFormWrapper.addData(AsFormData.textbox("integer_input", expectedIntegerValue));
......@@ -54,11 +54,18 @@ public class AsFormProviderTest {
tableData.addData(AsFormData.numericinput("table_input_2-b2", expectedTableInputValueB2, expectedTableInputValueB2));
asFormWrapper.getData().add(tableData);
SynergyApiProvider synergyApiProviderMock = Mockito.mock(SynergyApiProvider.class);
Mockito.when(synergyApiProviderMock.getAsfData(Mockito.anyString())).thenReturn(asFormWrapper);
RestHttpQuery restHttpQueryMock = Mockito.mock(RestHttpQuery.class);
//Mockito.when(restHttpQueryMock.doGetQuery(Mockito.anyString())).thenReturn(asFormWrapper)
asFormProvider = AsFormProvider.newInstance(new QueryContext("localhost"));
//asFormProvider.setRestHttpQuery();
testForm = asFormProvider.fetch(TestForm.class, "123456");*/
}
asFormProvider = AsFormProvider.newInstance(synergyApiProviderMock);
testForm = asFormProvider.fetch(TestForm.class, "123456");
@Test
public void testQuery() throws IOException {
asFormProvider = AsFormProvider.newInstance(new QueryContext("http://192.168.0.143:8080/Synergy", "NppAdmin", "123456"));
TestForm fetch = asFormProvider.fetch(TestForm.class, "2ae95419-af0e-4017-bad9-ff527591ef02");
System.out.println();
}
@Test
......
package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.SynergyApiProvider;
import org.testng.annotations.Test;
/**
......@@ -9,8 +8,6 @@ import org.testng.annotations.Test;
*/
public class AsFormTest {
private SynergyApiProvider synergyApiProvider;
@Test
public void createAsForm() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment