Skip to content

Commit

Permalink
Add Lambda support for Skygear Data Type #230
Browse files Browse the repository at this point in the history
  • Loading branch information
carmenlau committed May 16, 2018
2 parents efec69a + 7b2e77e commit b34166f
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,13 @@ public void testLambdaRequestCompatibleValueValidation() throws Exception {
(short) 3,
"3",
jsonObject,
jsonArray
jsonArray,
new Date()
});

request.validate();
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleValueValidation() throws Exception {
new LambdaRequest("test:op1", new Object[]{new Date()});
}

@Test
public void testLambdaRequestAllowNullArgumentWithMap() throws Exception {
LambdaRequest request = new LambdaRequest(
Expand Down Expand Up @@ -146,29 +142,17 @@ public void testLambdaRequestCompatibleValueWithMapValidation() throws Exception
put("key9", "3");
put("key10", jsonObject);
put("key11", jsonArray);
put("key12", new Date());
put("key13", new HashMap<String, Object>() {{
put("key13a", new Date());
}});
}});

request.validate();
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleValueWithMapValidation() throws Exception {
new LambdaRequest("test:op1", new HashMap<String, Object>() {{
put("key1", new Date());
}});
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleNestedValueWithMapValidation() throws Exception {
new LambdaRequest("test:op1", new HashMap<String, Object>() {{
put("key1", new HashMap<String, Object>() {{
put("key2", new Date());
}});
}});
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleNestedValueWithMapValidation2() throws Exception {
@Test
public void testLambdaRequestCompatibleNestedValueWithMapValidation1() throws Exception {
final JSONObject object = new JSONObject();
final JSONObject object2 = new JSONObject();
object2.put("key2", new Date());
Expand All @@ -179,8 +163,8 @@ public void testLambdaRequestIncompatibleNestedValueWithMapValidation2() throws
}


@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleNestedValueWithMapValidation3() throws Exception {
@Test
public void testLambdaRequestCompatibleNestedValueWithMapValidation2() throws Exception {
final JSONArray object = new JSONArray();
final JSONObject object2 = new JSONObject();
object2.put("key2", new Date());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2017 Oursky Ltd.
*
* 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 io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import java.util.Map;
import java.util.List;

import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;

@RunWith(AndroidJUnit4.class)
public class TypedLambdaResponseHandlerUnitTest {
@Test
public void testTypedLambdaResponseHandlerMap() throws Exception {
final boolean[] checkpoints = { false };
TypedLambdaResponseHandler handler = new TypedLambdaResponseHandler<Map<String, Object>>() {
@Override
public void onLambdaSuccess(Map<String, Object> result) {
assertEquals("world", result.get("hello"));
checkpoints[0] = true;
}

@Override
public void onLambdaFail(Error error) {
fail("Should not get fail callback");
}
};

handler.onSuccess(new JSONObject("{\"result\":{\"hello\":\"world\"}}"));
assertTrue(checkpoints[0]);
}

@Test
public void testTypedLambdaResponseHandlerList() throws Exception {
final boolean[] checkpoints = { false };
TypedLambdaResponseHandler handler = new TypedLambdaResponseHandler<List<String>>() {
@Override
public void onLambdaSuccess(List<String> result) {
assertEquals("hello", result.get(0));
assertEquals("world", result.get(1));
checkpoints[0] = true;
}

@Override
public void onLambdaFail(Error error) {
fail("Should not get fail callback");
}
};

handler.onSuccess(new JSONObject("{\"result\":[\"hello\",\"world\"]}"));
assertTrue(checkpoints[0]);
}

@Test
public void testTypedLambdaResponseHandlerNullResponse() throws Exception {
final boolean[] checkpoints = { false };
TypedLambdaResponseHandler handler = new TypedLambdaResponseHandler<Object>() {
@Override
public void onLambdaSuccess(Object result) {
assertNull(result);
checkpoints[0] = true;
}

@Override
public void onLambdaFail(Error error) {
fail("Should not get fail callback");
}
};

handler.onSuccess(new JSONObject("{\"result\":null}"));
assertTrue(checkpoints[0]);
}

@Test
public void testTypedLambdaResponseHandlerFailFlow() throws Exception {
final boolean[] checkpoints = { false };
TypedLambdaResponseHandler handler = new TypedLambdaResponseHandler<Object>() {
@Override
public void onLambdaSuccess(Object result) {
fail("Should not get success callback");
}

@Override
public void onLambdaFail(Error error) {
assertEquals("Test Error", error.getDetailMessage());
checkpoints[0] = true;
}
};

handler.onFail(new Error("Test Error"));
assertTrue(checkpoints[0]);
}
}
Loading

0 comments on commit b34166f

Please sign in to comment.