JsonApiTest.java
/***************************************************************************
Copyright 2014 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.web.concrete;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import java.util.LinkedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.metanotion.functor.Block;
import net.metanotion.json.JsonObject;
import net.metanotion.util.Json;
import net.metanotion.web.HttpAny;
import net.metanotion.web.HttpDefault;
import net.metanotion.web.HttpGet;
import net.metanotion.web.HttpPost;
import net.metanotion.web.HttpValues;
import net.metanotion.web.Param;
import net.metanotion.web.Response;
public final class JsonApiTest {
private static final Logger logger = LoggerFactory.getLogger(JsonApiTest.class);
public interface WebApiTest {
@HttpPost public Object login();
@HttpAny @HttpDefault public Object notFound();
@HttpGet @Response({@Param(value="json", name="mime")}) public Object whoAmI();
@HttpGet @Json public Object api();
@HttpGet @Json public Object v1();
@HttpGet @Json public Values v2();
}
public static final class Holder {
public String c;
}
public static final class Values {
public String a;
public int b;
public List<Holder> arr;
}
public static void main(String[] args) throws Exception {
JsonObject api = JsonUtil.makeWebApi(WebApiTest.class, "test/");
JsonObject result = JsonObject.read("{\"login\": { \"method\": \"POST\", \"uri\": \"test/login\" }, " +
"\"whoAmI\": { \"method\": \"GET\", \"uri\": \"test/whoAmI\", \"result\": \"json\" }, " +
"\"api\": { \"method\": \"GET\", \"uri\": \"test/api\", \"result\": \"json\" }, " +
"\"v1\": { \"method\": \"GET\", \"uri\": \"test/v1\", \"result\": \"json\" }, " +
"\"v2\": { \"method\": \"GET\", \"uri\": \"test/v2\", \"result\": \"json\" } } ");
logger.debug(api.toString());
logger.debug(result.toString());
boolean testResult = api.equals(result);
logger.debug("Result: {}", testResult);
if(testResult != true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final Map<String,Block> jsonXfmr = JsonUtil.generateJsonResultTransformers(WebApiTest.class);
for(Map.Entry<String,Block> e: jsonXfmr.entrySet()) {
logger.debug(e.getKey());
}
final HttpValues jsonResponse = (HttpValues) jsonXfmr.get("api").eval(api);
logger.debug("Response: {} - {}", jsonResponse.getClass(), jsonResponse);
logger.debug(jsonResponse.unwrap().toString());
testResult = jsonResponse.unwrap().equals(result);
logger.debug("Result: {}", testResult);
if(testResult != true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final Values v = new Values();
v.a = "foo";
v.b = 2;
Holder h = new Holder();
h.c = "bar";
Holder h2 = new Holder();
h2.c = "baz";
v.arr = Arrays.asList(h, h2);
final HttpValues r2 = (HttpValues) jsonXfmr.get("v1").eval(v);
logger.debug("Response: {} - u {} - {}", r2.getClass(), r2.unwrap().getClass(), r2);
logger.debug(r2.unwrap().toString());
result = JsonObject.read("{\"arr\": [ {\"c\": \"bar\"}, {\"c\":\"baz\"}], \"b\": 2, \"a\": \"foo\" }");
testResult = r2.unwrap().equals(result);
logger.debug("response: {}", r2.unwrap());
logger.debug("expected: {}", result);
logger.debug("Result: {}", testResult);
if(testResult == true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final HttpValues r3 = (HttpValues) jsonXfmr.get("v2").eval(v);
logger.debug("Response: {} - u {} - {}", r3.getClass(), r3.unwrap().getClass(), r3);
logger.debug(r3.unwrap().toString());
testResult = r3.unwrap().equals(result);
logger.debug("response: {}", r3.unwrap());
logger.debug("expected: {}", result);
logger.debug("Result: {}", testResult);
if(testResult == true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final SimpleHttpValues v2 = new SimpleHttpValues(new LinkedList<Map.Entry<String,Object>>(), new LinkedList<Map.Entry<String,Object>>(), v);
final HttpValues r4 = (HttpValues) jsonXfmr.get("v2").eval(v2);
logger.debug("Response: {} - u {} - {}", r4.getClass(), r4.unwrap().getClass(), r4);
logger.debug(r4.unwrap().toString());
testResult = r4.unwrap().equals(result);
logger.debug("response: {}", r4.unwrap());
logger.debug("expected: {}", result);
logger.debug("Result: {}", testResult);
if(testResult == true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final HttpValues r5 = (HttpValues) jsonXfmr.get("v1").eval("{\"a\": true, \"test\": 1}");
logger.debug("Response: {} - u {} - {}", r5.getClass(), r5.unwrap().getClass(), r5);
logger.debug(r5.unwrap().toString());
result =JsonObject.read("{\"test\": 1, \"a\": true}");
testResult = r5.unwrap().equals(result);
logger.debug("response: {}", r5.unwrap());
logger.debug("expected: {}", result);
logger.debug("Result: {}", testResult);
if(testResult != true) { throw new AssertionError("JSON Objects Expected to be equivalent"); }
final HttpValues r6 = (HttpValues) jsonXfmr.get("v1").eval("{\"a: true, \"test\": 1}");
if(r6.unwrap() != null) {
throw new AssertionError("Should have generated a null response due to the parsing error.");
}
}
}