com.aqnote.app.wifianalyzer.wifi.AccessPointsAdapterDataTest.java Source code

Java tutorial

Introduction

Here is the source code for com.aqnote.app.wifianalyzer.wifi.AccessPointsAdapterDataTest.java

Source

/*
 * WiFi Analyzer
 * Copyright (C) 2016  VREM Software Development <VREMSoftwareDevelopment@gmail.com>
 *
 * This program 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.
 *
 * This program 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 com.aqnote.app.wifianalyzer.wifi;

import com.aqnote.app.wifianalyzer.MainContextHelper;
import com.aqnote.app.wifianalyzer.settings.Settings;
import com.aqnote.app.wifianalyzer.wifi.band.WiFiBand;
import com.aqnote.app.wifianalyzer.wifi.model.GroupBy;
import com.aqnote.app.wifianalyzer.wifi.model.SortBy;
import com.aqnote.app.wifianalyzer.wifi.model.WiFiData;
import com.aqnote.app.wifianalyzer.wifi.model.WiFiDetail;
import com.aqnote.app.wifianalyzer.wifi.model.WiFiSignal;

import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AccessPointsAdapterDataTest {
    @Mock
    private WiFiData wiFiData;

    private Settings settings;
    private AccessPointsAdapterData fixture;

    @Before
    public void setUp() {
        settings = MainContextHelper.INSTANCE.getSettings();
        fixture = new AccessPointsAdapterData();
    }

    @After
    public void tearDown() {
        MainContextHelper.INSTANCE.restore();
    }

    @Test
    public void testBeforeUpdate() throws Exception {
        assertEquals(0, fixture.parentsCount());
        assertEquals(0, fixture.childrenCount(0));
        assertEquals(WiFiDetail.EMPTY, fixture.parent(0));
        assertEquals(WiFiDetail.EMPTY, fixture.parent(-1));
        assertEquals(WiFiDetail.EMPTY, fixture.child(0, 0));
        assertEquals(WiFiDetail.EMPTY, fixture.child(0, -1));
    }

    @Test
    public void testAfterUpdate() throws Exception {
        // setup
        withSettings();
        List<WiFiDetail> wiFiDetails = withWiFiDetails();
        when(wiFiData.getWiFiDetails(WiFiBand.GHZ5, SortBy.SSID, GroupBy.CHANNEL)).thenReturn(wiFiDetails);
        // execute
        fixture.update(wiFiData);
        // validate
        verify(wiFiData).getWiFiDetails(WiFiBand.GHZ5, SortBy.SSID, GroupBy.CHANNEL);
        verifySettings();

        assertEquals(wiFiDetails.size(), fixture.parentsCount());
        assertEquals(wiFiDetails.get(0), fixture.parent(0));
        assertEquals(wiFiDetails.get(0).getChildren().size(), fixture.childrenCount(0));
        assertEquals(wiFiDetails.get(0).getChildren().get(0), fixture.child(0, 0));

        assertEquals(WiFiDetail.EMPTY, fixture.parent(-1));
        assertEquals(WiFiDetail.EMPTY, fixture.parent(wiFiDetails.size()));
        assertEquals(WiFiDetail.EMPTY, fixture.child(0, -1));
        assertEquals(WiFiDetail.EMPTY, fixture.child(0, wiFiDetails.get(0).getChildren().size()));
    }

    private List<WiFiDetail> withWiFiDetails() {
        WiFiDetail wiFiDetail1 = new WiFiDetail("SSID1", "BSSID1", StringUtils.EMPTY, WiFiSignal.EMPTY);
        wiFiDetail1.addChild(new WiFiDetail("SSID1-1", "BSSID1-1", StringUtils.EMPTY, WiFiSignal.EMPTY));
        wiFiDetail1.addChild(new WiFiDetail("SSID1-2", "BSSID1-2", StringUtils.EMPTY, WiFiSignal.EMPTY));
        wiFiDetail1.addChild(new WiFiDetail("SSID1-3", "BSSID1-3", StringUtils.EMPTY, WiFiSignal.EMPTY));
        return Arrays.asList(wiFiDetail1, new WiFiDetail("SSID2", "BSSID2", StringUtils.EMPTY, WiFiSignal.EMPTY),
                new WiFiDetail("SSID3", "BSSID3", StringUtils.EMPTY, WiFiSignal.EMPTY));
    }

    private void verifySettings() {
        verify(settings).getWiFiBand();
        verify(settings).getSortBy();
        verify(settings).getGroupBy();
    }

    private void withSettings() {
        when(settings.getWiFiBand()).thenReturn(WiFiBand.GHZ5);
        when(settings.getSortBy()).thenReturn(SortBy.SSID);
        when(settings.getGroupBy()).thenReturn(GroupBy.CHANNEL);
    }
}