<commit_before>import com.avaje.ebean.Ebean;
import com.fasterxml.jackson.databind.JsonNode;
import drones.models.BepopDriver;
import models.Drone;
import controllers.routes;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import play.libs.Json;
import play.mvc.Http;
import play.mvc.Result;
import utilities.JsonHelper;

import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;

/**
 * Created by Benjamin on 4/03/2015.
 */
public class DroneControllerTest extends TestSuperclass {

    private static List<Drone> testDrones = new ArrayList<>();

    @BeforeClass
    public static void setup() {
        startFakeApplication();
        initialiseDatabase();
    }

    private static void initialiseDatabase() {
        // Add drones to the database
        testDrones.add(new Drone("testdrone1", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone2", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone3", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone4", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        Ebean.save(testDrones);
    }

    @AfterClass
    public static void teardown() {
        stopFakeApplication();
    }

    @Test
    public void getAll_AuthorizedRequest_SuccessfullyGetAllDrones() {
        Result result = callAction(routes.ref.DroneController.getAll(), authorizeRequest(fakeRequest(), getAdmin()));

        try {
            JsonNode node = JsonHelper.removeRootElement(contentAsString(result), Drone.class, true);
            if (node.isArray()) {
                for (int i = 0; i < testDrones.size(); ++i) {
                    Drone testDrone = testDrones.get(i);
                    Drone receivedDrone = Json.fromJson(node.get(i), Drone.class);
                    assertThat(testDrone.getId()).isEqualTo(receivedDrone.getId());
                }
            } else
                Assert.fail("Returned JSON is not an array");
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }

    @Test
    public void get_AuthorizedRequestWithValidId_SuccessfullyGetDrone() {
        Drone droneToGet = testDrones.get(testDrones.size()-1);
        Result result = callAction(routes.ref.DroneController.get(droneToGet.getId()),
                authorizeRequest(fakeRequest(), getAdmin()));

        try {
            JsonNode node = JsonHelper.removeRootElement(contentAsString(result), Drone.class, false);
            Drone d = Json.fromJson(node, Drone.class);
            assertThat(d).isEqualTo(droneToGet);
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }

    @Test
    public void get_AuthorizedRequestWithInvalidId_NotFoundReturned() {
        Result result = callAction(routes.ref.DroneController.get(testDrones.size()+1000),
                authorizeRequest(fakeRequest(), getAdmin()));
        assertThat(status(result)).isEqualTo(Http.Status.NOT_FOUND);
    }

    @Test
    public void create_AuthorizedRequest_DroneCreated() {
        Drone droneToBeAdded = new Drone("newDrone", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE, "ipAddress");
        JsonNode node = JsonHelper.createJsonNode(droneToBeAdded, Drone.class);

        Result result = callAction(routes.ref.DroneController.create(),
                authorizeRequest(fakeRequest().withJsonBody(node), getAdmin()));

        try {
            JsonNode receivedNode = JsonHelper.removeRootElement(contentAsString(result), Drone.class, false);
            Drone d = Json.fromJson(receivedNode, Drone.class);
            droneToBeAdded.setId(d.getId());
            assertThat(d).isEqualTo(droneToBeAdded);

            Drone fetchedDrone = Drone.FIND.byId(d.getId());
            assertThat(fetchedDrone).isEqualTo(droneToBeAdded);
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }<commit_msg>drone test fix<commit_after>import com.avaje.ebean.Ebean;
import com.fasterxml.jackson.databind.JsonNode;
import drones.models.BepopDriver;
import drones.simulation.SimulatorDriver;
import models.Drone;
import controllers.routes;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import play.libs.Json;
import play.mvc.Http;
import play.mvc.Result;
import utilities.JsonHelper;

import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;

/**
 * Created by Benjamin on 4/03/2015.
 */
public class DroneControllerTest extends TestSuperclass {

    private static List<Drone> testDrones = new ArrayList<>();

    @BeforeClass
    public static void setup() {
        startFakeApplication();
        initialiseDatabase();
    }

    private static void initialiseDatabase() {
        // Add drones to the database
        testDrones.add(new Drone("testdrone1", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone2", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone3", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        testDrones.add(new Drone("testdrone4", Drone.Status.AVAILABLE, BepopDriver.BEPOP_TYPE,  "x.x.x.x"));
        Ebean.save(testDrones);
    }

    @AfterClass
    public static void teardown() {
        stopFakeApplication();
    }

    @Test
    public void getAll_AuthorizedRequest_SuccessfullyGetAllDrones() {
        Result result = callAction(routes.ref.DroneController.getAll(), authorizeRequest(fakeRequest(), getAdmin()));

        try {
            JsonNode node = JsonHelper.removeRootElement(contentAsString(result), Drone.class, true);
            if (node.isArray()) {
                for (int i = 0; i < testDrones.size(); ++i) {
                    Drone testDrone = testDrones.get(i);
                    Drone receivedDrone = Json.fromJson(node.get(i), Drone.class);
                    assertThat(testDrone.getId()).isEqualTo(receivedDrone.getId());
                }
            } else
                Assert.fail("Returned JSON is not an array");
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }

    @Test
    public void get_AuthorizedRequestWithValidId_SuccessfullyGetDrone() {
        Drone droneToGet = testDrones.get(testDrones.size()-1);
        Result result = callAction(routes.ref.DroneController.get(droneToGet.getId()),
                authorizeRequest(fakeRequest(), getAdmin()));

        try {
            JsonNode node = JsonHelper.removeRootElement(contentAsString(result), Drone.class, false);
            Drone d = Json.fromJson(node, Drone.class);
            assertThat(d).isEqualTo(droneToGet);
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }

    @Test
    public void get_AuthorizedRequestWithInvalidId_NotFoundReturned() {
        Result result = callAction(routes.ref.DroneController.get(testDrones.size()+1000),
                authorizeRequest(fakeRequest(), getAdmin()));
        assertThat(status(result)).isEqualTo(Http.Status.NOT_FOUND);
    }

    @Test
    public void create_AuthorizedRequest_DroneCreated() {
        Drone droneToBeAdded = new Drone("newDrone", Drone.Status.AVAILABLE, SimulatorDriver.SIMULATOR_TYPE, "ipAddress");
        JsonNode node = JsonHelper.createJsonNode(droneToBeAdded, Drone.class);

        Result result = callAction(routes.ref.DroneController.create(),
                authorizeRequest(fakeRequest().withJsonBody(node), getAdmin()));

        try {
            JsonNode receivedNode = JsonHelper.removeRootElement(contentAsString(result), Drone.class, false);
            Drone d = Json.fromJson(receivedNode, Drone.class);
            droneToBeAdded.setId(d.getId());
            assertThat(d).isEqualTo(droneToBeAdded);

            Drone fetchedDrone = Drone.FIND.byId(d.getId());
            assertThat(fetchedDrone).isEqualTo(droneToBeAdded);
        } catch(JsonHelper.InvalidJSONException ex) {
            Assert.fail("Invalid json exception: " + ex.getMessage());
        }
    }
{'fraction_non_alphanumeric': 0.11700034718203911, 'fraction_numerical': 0.003934729776646222, 'mean_word_length': 3.0439868975198876, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '218679', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2568, 'n_words': 446}