summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/SendLocationTests.java
blob: ad1ad8a2c38c43bb9106d68404c118ae7027a930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.smartdevicelink.test.rpc.requests;

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.rpc.Image;
import com.smartdevicelink.proxy.rpc.SendLocation;
import com.smartdevicelink.test.BaseRpcTests;
import com.smartdevicelink.test.JsonUtils;
import com.smartdevicelink.test.TestValues;
import com.smartdevicelink.test.Validator;
import com.smartdevicelink.test.json.rpc.JsonFileReader;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import java.util.Hashtable;
import java.util.List;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

/**
 * This is a unit test class for the SmartDeviceLink library project class : 
 * {@link com.smartdevicelink.rpc.SendLocation}
 */
public class SendLocationTests extends BaseRpcTests {
	
    @Override
    protected RPCMessage createMessage(){
    	SendLocation msg = new SendLocation();
    	
    	msg.setLatitudeDegrees(TestValues.GENERAL_DOUBLE);
    	msg.setLongitudeDegrees(TestValues.GENERAL_DOUBLE);
    	msg.setLocationName(TestValues.GENERAL_STRING);
    	msg.setLocationDescription(TestValues.GENERAL_STRING);
    	msg.setPhoneNumber(TestValues.GENERAL_STRING);
    	msg.setAddressLines(TestValues.GENERAL_STRING_LIST);
    	msg.setLocationImage(TestValues.GENERAL_IMAGE);
    	
    	return msg;
    }
    
    @Override
    protected String getMessageType(){
        return RPCMessage.KEY_REQUEST;
    }
    
    @Override
    protected String getCommandType(){
        return FunctionID.SEND_LOCATION.toString();
    }
    
    @Override
    protected JSONObject getExpectedParameters (int sdlVersion){
    	JSONObject result = new JSONObject();
    	
    	try {
    		result.put(SendLocation.KEY_LAT_DEGREES, TestValues.GENERAL_DOUBLE);
    		result.put(SendLocation.KEY_LON_DEGREES, TestValues.GENERAL_DOUBLE);
    		result.put(SendLocation.KEY_LOCATION_NAME, TestValues.GENERAL_STRING);
    		result.put(SendLocation.KEY_LOCATION_DESCRIPTION, TestValues.GENERAL_STRING);
    		result.put(SendLocation.KEY_PHONE_NUMBER, TestValues.GENERAL_STRING);
    		result.put(SendLocation.KEY_LOCATION_IMAGE, TestValues.GENERAL_IMAGE.serializeJSON());
    		result.put(SendLocation.KEY_ADDRESS_LINES, JsonUtils.createJsonArray(TestValues.GENERAL_STRING_LIST));
    	} catch(JSONException e){
    		fail(TestValues.JSON_FAIL);
        }
    	
    	return result;
    }
    
    /**
	 * Tests the expected values of the RPC message.
	 */
    @Test
    public void testRpcValues () {    	
    	// Test Values
    	Double latitude = ((SendLocation) msg).getLatitudeDegrees();
        Double longitude = ((SendLocation) msg).getLongitudeDegrees();
        String locationName = ((SendLocation) msg).getLocationName();
    	String locationDesc = ((SendLocation) msg).getLocationDescription();
    	String phoneNumber = ((SendLocation) msg).getPhoneNumber();
    	List<String> addressLines = ((SendLocation) msg).getAddressLines();
    	Image locationImage = ((SendLocation) msg).getLocationImage();
    	
    	// Valid Tests
        assertEquals(TestValues.MATCH, TestValues.GENERAL_DOUBLE, longitude);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_DOUBLE, latitude);
    	assertEquals(TestValues.MATCH, TestValues.GENERAL_STRING, locationDesc);
    	assertEquals(TestValues.MATCH, TestValues.GENERAL_STRING, locationName);
    	assertEquals(TestValues.MATCH, TestValues.GENERAL_STRING, phoneNumber);
    	assertTrue(TestValues.TRUE, Validator.validateStringList(TestValues.GENERAL_STRING_LIST, addressLines));
    	assertTrue(TestValues.TRUE, Validator.validateImage(TestValues.GENERAL_IMAGE, locationImage));
    
    	// Invalid/Null Tests
    	SendLocation msg = new SendLocation();
    	assertNotNull(TestValues.NOT_NULL, msg);
        testNullBase(msg);

        assertNull(TestValues.NULL, msg.getLatitudeDegrees());
        assertNull(TestValues.NULL, msg.getLongitudeDegrees());
        assertNull(TestValues.NULL, msg.getLocationName());
        assertNull(TestValues.NULL, msg.getLocationDescription());
        assertNull(TestValues.NULL, msg.getPhoneNumber());
        assertNull(TestValues.NULL, msg.getAddressLines());
        assertNull(TestValues.NULL, msg.getLocationImage());
    }

    /**
     * Tests a valid JSON construction of this RPC message.
     */
    @Test
    public void testJsonConstructor () {
    	JSONObject commandJson = JsonFileReader.readId(getInstrumentation().getTargetContext(), getCommandType(), getMessageType());
    	assertNotNull("Command object is null", commandJson);
    	
    	try {
    		Hashtable<String, Object> hash = JsonRPCMarshaller.deserializeJSONObject(commandJson);
    		SendLocation cmd = new SendLocation(hash);
    		assertNotNull(TestValues.NOT_NULL, cmd);
    		
    		JSONObject body = JsonUtils.readJsonObjectFromJsonObject(commandJson, getMessageType());
    		assertNotNull(TestValues.NOT_NULL, body);
    		
    		// Test everything in the json body.
    		assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(body, RPCMessage.KEY_FUNCTION_NAME), cmd.getFunctionName());
			assertEquals(TestValues.MATCH, JsonUtils.readIntegerFromJsonObject(body, RPCMessage.KEY_CORRELATION_ID), cmd.getCorrelationID());
    		
			JSONObject parameters = JsonUtils.readJsonObjectFromJsonObject(body, RPCMessage.KEY_PARAMETERS);
			assertEquals(TestValues.MATCH, JsonUtils.readDoubleFromJsonObject(parameters, SendLocation.KEY_LAT_DEGREES), cmd.getLatitudeDegrees());
			assertEquals(TestValues.MATCH, JsonUtils.readDoubleFromJsonObject(parameters, SendLocation.KEY_LON_DEGREES), cmd.getLongitudeDegrees());
			assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(parameters, SendLocation.KEY_LOCATION_NAME), cmd.getLocationName());
			assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(parameters, SendLocation.KEY_LOCATION_DESCRIPTION), cmd.getLocationDescription());
			assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(parameters, SendLocation.KEY_PHONE_NUMBER), cmd.getPhoneNumber());
			
			List<String> addressList = JsonUtils.readStringListFromJsonObject(parameters, SendLocation.KEY_ADDRESS_LINES);
			List<String> testList = cmd.getAddressLines();
			assertEquals(TestValues.MATCH, addressList.size(), testList.size());
			assertTrue(TestValues.TRUE, Validator.validateStringList(addressList, testList));
			
			JSONObject cmdIcon = JsonUtils.readJsonObjectFromJsonObject(parameters, SendLocation.KEY_LOCATION_IMAGE);
			Image reference = new Image(JsonRPCMarshaller.deserializeJSONObject(cmdIcon));
			assertTrue(TestValues.TRUE, Validator.validateImage(reference, cmd.getLocationImage()));
			
    	} catch (JSONException e) {
    		fail(TestValues.JSON_FAIL);
		}
    }
}