onacodechallenge.ONACodeChallengeTest.java Source code

Java tutorial

Introduction

Here is the source code for onacodechallenge.ONACodeChallengeTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package onacodechallenge;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author dennis
 */
public class ONACodeChallengeTest {
    ONACodeChallenge app;

    public ONACodeChallengeTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
        app = new ONACodeChallenge();
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of main method, of class ONACodeChallenge.
     */
    @Test
    public void testCalculate() {
        try {
            String fileURl = new File("data/testdata.json").toURI().toURL().toString();

            JSONObject calculate = (JSONObject) app.calculate(fileURl);

            JSONObject numWater = (JSONObject) calculate.get("number_water_points");
            JSONObject ranking = (JSONObject) calculate.get("community_ranking");

            assertEquals(12, calculate.get("number_functional"));
            assertEquals(8, numWater.get("Sangana"));
            assertEquals(50.0f, ranking.get("Machakos"));
        } catch (IOException ex) {
            Logger.getLogger(ONACodeChallengeTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(ONACodeChallengeTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Test of main method, of class ONACodeChallenge.
     */
    @Test
    public void testSortByRank() {
        Map testUnsortedMap = new HashMap();
        testUnsortedMap.put("kisumu", 33.3f);
        testUnsortedMap.put("mombasa", 10.0f);
        testUnsortedMap.put("maragwa", 0f);
        testUnsortedMap.put("kilifi", 80.0f);

        Map testSortedMap = new LinkedHashMap();
        testSortedMap.put("maragwa", 0f);
        testSortedMap.put("mombasa", 10.0f);
        testSortedMap.put("kisumu", 33.3f);
        testSortedMap.put("kilifi", 80.0f);

        Map result = app.sortByRank(testUnsortedMap);
        assertEquals(testSortedMap, result);
    }

}