mirror of https://github.com/iSoron/uhabits.git
parent
5d2ff40dc9
commit
ac1441aba5
@ -1,81 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.core.commands;
|
|
||||||
|
|
||||||
import androidx.annotation.*;
|
|
||||||
|
|
||||||
import com.google.gson.*;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
|
|
||||||
public class CommandParser
|
|
||||||
{
|
|
||||||
private HabitList habitList;
|
|
||||||
|
|
||||||
private ModelFactory modelFactory;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public CommandParser(@NonNull HabitList habitList,
|
|
||||||
@NonNull ModelFactory modelFactory)
|
|
||||||
{
|
|
||||||
this.habitList = habitList;
|
|
||||||
this.modelFactory = modelFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public Command parse(@NonNull String json)
|
|
||||||
{
|
|
||||||
JsonObject parsed = new JsonParser().parse(json).getAsJsonObject();
|
|
||||||
String event = parsed.get("event").getAsString();
|
|
||||||
Gson gson = new GsonBuilder().create();
|
|
||||||
|
|
||||||
if (event.equals("Archive")) return gson
|
|
||||||
.fromJson(json, ArchiveHabitsCommand.Record.class)
|
|
||||||
.toCommand(habitList);
|
|
||||||
|
|
||||||
if (event.equals("ChangeColor")) return gson
|
|
||||||
.fromJson(json, ChangeHabitColorCommand.Record.class)
|
|
||||||
.toCommand(habitList);
|
|
||||||
|
|
||||||
if (event.equals("CreateHabit")) return gson
|
|
||||||
.fromJson(json, CreateHabitCommand.Record.class)
|
|
||||||
.toCommand(modelFactory, habitList);
|
|
||||||
|
|
||||||
if (event.equals("CreateRep")) return gson
|
|
||||||
.fromJson(json, CreateRepetitionCommand.Record.class)
|
|
||||||
.toCommand(habitList);
|
|
||||||
|
|
||||||
if (event.equals("DeleteHabit")) return gson
|
|
||||||
.fromJson(json, DeleteHabitsCommand.Record.class)
|
|
||||||
.toCommand(habitList);
|
|
||||||
|
|
||||||
if (event.equals("EditHabit")) return gson
|
|
||||||
.fromJson(json, EditHabitCommand.Record.class)
|
|
||||||
.toCommand(modelFactory, habitList);
|
|
||||||
|
|
||||||
if (event.equals("Unarchive")) return gson
|
|
||||||
.fromJson(json, UnarchiveHabitsCommand.Record.class)
|
|
||||||
.toCommand(habitList);
|
|
||||||
|
|
||||||
throw new IllegalStateException("Unknown command");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides commands to modify the models, such as {@link
|
|
||||||
* org.isoron.uhabits.core.commands.CreateHabitCommand}.
|
|
||||||
*/
|
|
||||||
package org.isoron.uhabits.core.commands;
|
|
@ -1,149 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.core.commands;
|
|
||||||
|
|
||||||
import androidx.annotation.*;
|
|
||||||
|
|
||||||
import org.hamcrest.*;
|
|
||||||
import org.isoron.uhabits.core.*;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
|
||||||
import org.json.*;
|
|
||||||
import org.junit.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
|
||||||
|
|
||||||
public class CommandParserTest extends BaseUnitTest
|
|
||||||
{
|
|
||||||
@NonNull
|
|
||||||
private CommandParser parser;
|
|
||||||
|
|
||||||
private Habit habit;
|
|
||||||
|
|
||||||
private List<Habit> selected;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception
|
|
||||||
{
|
|
||||||
super.setUp();
|
|
||||||
parser = new CommandParser(habitList, modelFactory);
|
|
||||||
habit = fixtures.createShortHabit();
|
|
||||||
selected = Collections.singletonList(habit);
|
|
||||||
habitList.add(habit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeArchiveCommand() throws JSONException
|
|
||||||
{
|
|
||||||
ArchiveHabitsCommand original, decoded;
|
|
||||||
original = new ArchiveHabitsCommand(habitList, selected);
|
|
||||||
decoded = (ArchiveHabitsCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeChangeColorCommand() throws JSONException
|
|
||||||
{
|
|
||||||
ChangeHabitColorCommand original, decoded;
|
|
||||||
original = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(20));
|
|
||||||
decoded = (ChangeHabitColorCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.newColor, equalTo(original.newColor));
|
|
||||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeCreateHabitCommand() throws JSONException
|
|
||||||
{
|
|
||||||
Habit model = modelFactory.buildHabit();
|
|
||||||
model.setName("JSON");
|
|
||||||
|
|
||||||
CreateHabitCommand original, decoded;
|
|
||||||
original = new CreateHabitCommand(modelFactory, habitList, model);
|
|
||||||
original.execute();
|
|
||||||
|
|
||||||
decoded = (CreateHabitCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.savedId, equalTo(original.savedId));
|
|
||||||
MatcherAssert.assertThat(decoded.model.getData(), equalTo(model
|
|
||||||
.getData()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeCreateRepCommand() throws JSONException
|
|
||||||
{
|
|
||||||
CreateRepetitionCommand original, decoded;
|
|
||||||
original = new CreateRepetitionCommand(habitList, habit, Timestamp.ZERO.plus(100), 5);
|
|
||||||
decoded = (CreateRepetitionCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.timestamp, equalTo(original
|
|
||||||
.timestamp));
|
|
||||||
MatcherAssert.assertThat(decoded.value, equalTo(original.value));
|
|
||||||
MatcherAssert.assertThat(decoded.habit, equalTo(original.habit));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeDeleteCommand() throws JSONException
|
|
||||||
{
|
|
||||||
DeleteHabitsCommand original, decoded;
|
|
||||||
original = new DeleteHabitsCommand(habitList, selected);
|
|
||||||
decoded = (DeleteHabitsCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeEditHabitCommand() throws JSONException
|
|
||||||
{
|
|
||||||
Habit modified = modelFactory.buildHabit();
|
|
||||||
modified.setName("Edited JSON");
|
|
||||||
modified.setColor(new PaletteColor(2));
|
|
||||||
|
|
||||||
EditHabitCommand original, decoded;
|
|
||||||
original = new EditHabitCommand(modelFactory, habitList, habit, modified);
|
|
||||||
original.execute();
|
|
||||||
|
|
||||||
decoded = (EditHabitCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.savedId, equalTo(original.savedId));
|
|
||||||
MatcherAssert.assertThat(decoded.modified.getData(), equalTo(modified
|
|
||||||
.getData()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDecodeUnarchiveCommand() throws JSONException
|
|
||||||
{
|
|
||||||
UnarchiveHabitsCommand original, decoded;
|
|
||||||
original = new UnarchiveHabitsCommand(habitList, selected);
|
|
||||||
decoded = (UnarchiveHabitsCommand) parser.parse(original.toJson());
|
|
||||||
|
|
||||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
|
||||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue