1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
3 
4 package org.mozilla.android.sync.test;
5 
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.mozilla.gecko.background.testhelpers.TestRunner;
9 import org.mozilla.gecko.sync.CommandProcessor;
10 import org.mozilla.gecko.sync.CommandRunner;
11 import org.mozilla.gecko.sync.ExtendedJSONObject;
12 import org.mozilla.gecko.sync.GlobalSession;
13 import org.mozilla.gecko.sync.NonObjectJSONException;
14 
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 
25 @RunWith(TestRunner.class)
26 public class TestCommandProcessor extends CommandProcessor {
27 
28   public static final String commandType = "displayURI";
29   public static final String commandWithNoArgs = "{\"command\":\"displayURI\"}";
30   public static final String commandWithNoType = "{\"args\":[\"https://bugzilla.mozilla.org/show_bug.cgi?id=731341\",\"PKsljsuqYbGg\"]}";
31   public static final String wellFormedCommand = "{\"args\":[\"https://bugzilla.mozilla.org/show_bug.cgi?id=731341\",\"PKsljsuqYbGg\"],\"command\":\"displayURI\"}";
32   public static final String wellFormedCommandWithNullArgs = "{\"args\":[\"https://bugzilla.mozilla.org/show_bug.cgi?id=731341\",null,\"PKsljsuqYbGg\",null],\"command\":\"displayURI\"}";
33 
34   private boolean commandExecuted;
35 
36   // Session is not used in these tests.
37   protected final GlobalSession session = null;
38 
39   public class MockCommandRunner extends CommandRunner {
MockCommandRunner(int argCount)40     public MockCommandRunner(int argCount) {
41       super(argCount);
42     }
43 
44     @Override
executeCommand(final GlobalSession session, List<String> args)45     public void executeCommand(final GlobalSession session, List<String> args) {
46       commandExecuted = true;
47     }
48   }
49 
50   @Test
testRegisterCommand()51   public void testRegisterCommand() throws NonObjectJSONException, IOException {
52     assertNull(commands.get(commandType));
53     this.registerCommand(commandType, new MockCommandRunner(1));
54     assertNotNull(commands.get(commandType));
55   }
56 
57   @Test
testProcessRegisteredCommand()58   public void testProcessRegisteredCommand() throws NonObjectJSONException, IOException {
59     commandExecuted = false;
60     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(wellFormedCommand);
61     this.registerCommand(commandType, new MockCommandRunner(1));
62     this.processCommand(session, unparsedCommand);
63     assertTrue(commandExecuted);
64   }
65 
66   @Test
testProcessUnregisteredCommand()67   public void testProcessUnregisteredCommand() throws NonObjectJSONException, IOException {
68     commandExecuted = false;
69     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(wellFormedCommand);
70     this.processCommand(session, unparsedCommand);
71     assertFalse(commandExecuted);
72   }
73 
74   @Test
testProcessInvalidCommand()75   public void testProcessInvalidCommand() throws NonObjectJSONException, IOException {
76     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(commandWithNoType);
77     this.registerCommand(commandType, new MockCommandRunner(1));
78     this.processCommand(session, unparsedCommand);
79     assertFalse(commandExecuted);
80   }
81 
82   @Test
testParseCommandNoType()83   public void testParseCommandNoType() throws NonObjectJSONException, IOException {
84     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(commandWithNoType);
85     assertNull(CommandProcessor.parseCommand(unparsedCommand));
86   }
87 
88   @Test
testParseCommandNoArgs()89   public void testParseCommandNoArgs() throws NonObjectJSONException, IOException {
90     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(commandWithNoArgs);
91     assertNull(CommandProcessor.parseCommand(unparsedCommand));
92   }
93 
94   @Test
testParseWellFormedCommand()95   public void testParseWellFormedCommand() throws NonObjectJSONException, IOException {
96     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(wellFormedCommand);
97     Command parsedCommand = CommandProcessor.parseCommand(unparsedCommand);
98     assertNotNull(parsedCommand);
99     assertEquals(2, parsedCommand.args.size());
100     assertEquals(commandType, parsedCommand.commandType);
101   }
102 
103   @Test
testParseCommandNullArg()104   public void testParseCommandNullArg() throws NonObjectJSONException, IOException {
105     ExtendedJSONObject unparsedCommand = new ExtendedJSONObject(wellFormedCommandWithNullArgs);
106     Command parsedCommand = CommandProcessor.parseCommand(unparsedCommand);
107     assertNotNull(parsedCommand);
108     assertEquals(4, parsedCommand.args.size());
109     assertEquals(commandType, parsedCommand.commandType);
110     final List<String> expectedArgs = new ArrayList<String>();
111     expectedArgs.add("https://bugzilla.mozilla.org/show_bug.cgi?id=731341");
112     expectedArgs.add(null);
113     expectedArgs.add("PKsljsuqYbGg");
114     expectedArgs.add(null);
115     assertEquals(expectedArgs, parsedCommand.getArgsList());
116   }
117 }
118