Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #19 from schul-cloud/37-increase-test-coverage
Browse files Browse the repository at this point in the history
37 increase test coverage of models
  • Loading branch information
Niklas Kiefer authored May 23, 2017
2 parents f5be2aa + 6aa8862 commit 5dac568
Show file tree
Hide file tree
Showing 16 changed files with 874 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.schulcloud.mobile.test.common;

import org.schulcloud.mobile.data.model.Device;
import org.schulcloud.mobile.data.model.Event;
import org.schulcloud.mobile.data.model.User;

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

import org.schulcloud.mobile.data.model.User;

/**
* Factory class that makes instances of data models with random field values.
* The aim of this class is to help setting up test fixtures.
Expand All @@ -32,4 +34,34 @@ public static List<User> makeListUsers(int number) {
}
return users;
}

public static Event makeEvent(String uniqueSuffix) {
Event e = new Event();

return e;
}

public static List<Event> makeListEvents(int number) {
List <Event> events = new ArrayList<>();
for (int i = 0; i < number; i++) {
events.add(makeEvent(String.valueOf(i)));
}
return events;
}


public static Device makeDevice(String uniqueSuffix) {
Device d = new Device();

return d;
}

public static List<Device> makeListDevices(int number) {
List <Device> devices = new ArrayList<>();
for (int i = 0; i < number; i++) {
devices.add(makeDevice(String.valueOf(i)));
}
return devices;
}

}
104 changes: 104 additions & 0 deletions app/src/test/java/org/schulcloud/mobile/SettingsPresenterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.schulcloud.mobile;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.schulcloud.mobile.data.DataManager;
import org.schulcloud.mobile.data.model.Device;
import org.schulcloud.mobile.data.model.Event;
import org.schulcloud.mobile.test.common.TestDataFactory;
import org.schulcloud.mobile.ui.settings.SettingsMvpView;
import org.schulcloud.mobile.ui.settings.SettingsPresenter;
import org.schulcloud.mobile.util.RxSchedulersOverrideRule;

import java.util.Collections;
import java.util.List;

import rx.Observable;

import static org.mockito.Matchers.anyListOf;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.doReturn;

@RunWith(MockitoJUnitRunner.class)
public class SettingsPresenterTest {

@Rule
public final RxSchedulersOverrideRule mOverrideSchedulersRule = new RxSchedulersOverrideRule();
@Mock
SettingsMvpView mMockSettingsMvpView;
@Mock
DataManager mMockDataManager;
private SettingsPresenter mSettingsPresenter;

@Before
public void setUp() {
mSettingsPresenter = new SettingsPresenter(mMockDataManager);
mSettingsPresenter.attachView(mMockSettingsMvpView);
}

@After
public void tearDown() {
mSettingsPresenter.detachView();
}

@Test
public void loadEventsReturnsEvents() {
List<Event> events = TestDataFactory.makeListEvents(10);
doReturn(Observable.just(events))
.when(mMockDataManager)
.getEvents();

mSettingsPresenter.loadEvents();
verify(mMockSettingsMvpView).connectToCalendar(events);
verify(mMockSettingsMvpView, never()).showEventsEmpty();
}

@Test
public void loadEventsReturnsEmptyList() {
doReturn(Observable.just(Collections.emptyList()))
.when(mMockDataManager)
.getEvents();

mSettingsPresenter.loadEvents();
verify(mMockSettingsMvpView).showEventsEmpty();
verify(mMockSettingsMvpView, never()).connectToCalendar(anyListOf(Event.class));
}

@Test
public void loadEventsFails() {
doReturn(Observable.error(new RuntimeException()))
.when(mMockDataManager)
.getEvents();

mSettingsPresenter.loadEvents();
verify(mMockSettingsMvpView, never()).showEventsEmpty();
verify(mMockSettingsMvpView, never()).connectToCalendar(anyListOf(Event.class));
}

@Test
public void loadDevices() {
List<Device> devices = TestDataFactory.makeListDevices(10);
doReturn(Observable.just(devices))
.when(mMockDataManager)
.getDevices();

mSettingsPresenter.loadDevices();
verify(mMockSettingsMvpView).showDevices(devices);
}

@Test
public void loadDevicesFails() {
doReturn(Observable.error(new RuntimeException()))
.when(mMockDataManager)
.getDevices();

mSettingsPresenter.loadDevices();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.schulcloud.mobile.data.model;

import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

public class AccessTokenTest {
private static final String ACCESTOKEN = "OHNOACCESS";

private AccessToken accessToken;

@Before
public void setUp() throws Exception {
accessToken = createAccesToken();
}

public static AccessToken createAccesToken() {
AccessToken accessToken = new AccessToken();
accessToken.accessToken = ACCESTOKEN;

return accessToken;
}

@Test
public void getAccessToken() throws Exception {
assertEquals(accessToken.getAccessToken(), ACCESTOKEN);
}

@Test
public void setAccessToken() throws Exception {
accessToken.setAccessToken(ACCESTOKEN);
assertNotNull(accessToken.getAccessToken());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.schulcloud.mobile.data.model;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CurrentUserTest {
private static final String _ID = "key";
private static final String FIRSTNAME = "Ernst";
private static final String LASTNAME = "Haft";
private static final String EMAIL = "[email protected]";
private static final String SCHOOLID = "123214123";
private static final String DISPLAYNAME = "Ernst Haft";

private CurrentUser user;

@Before
public void setUp() {
user = createNewUser();
}

public static CurrentUser createNewUser() {
CurrentUser user = new CurrentUser();
user.set_id(_ID);
user.setDisplayName(DISPLAYNAME);
user.setSchoolId(SCHOOLID);
user.setLastName(LASTNAME);
user.setFirstName(FIRSTNAME);
user.setEmail(EMAIL);

return user;
}

@Test
public void get_id() throws Exception {
assertEquals(user.get_id(), _ID);
}

@Test
public void set_id() throws Exception {
user.set_id(_ID);
assertNotNull(user.get_id());
}

@Test
public void getFirstName() throws Exception {
assertEquals(user.getFirstName(), FIRSTNAME);
}

@Test
public void setFirstName() throws Exception {
user.setFirstName(FIRSTNAME);
assertNotNull(user.getFirstName());
}

@Test
public void getLastName() throws Exception {
assertEquals(user.getLastName(), LASTNAME);
}

@Test
public void setLastName() throws Exception {
user.setLastName(LASTNAME);
assertNotNull(user.getLastName());
}

@Test
public void getEmail() throws Exception {
assertEquals(user.getEmail(), EMAIL);
}

@Test
public void setEmail() throws Exception {
user.setEmail(EMAIL);
assertNotNull(user.getEmail());
}

@Test
public void getSchoolId() throws Exception {
assertEquals(user.getSchoolId(), SCHOOLID);
}

@Test
public void setSchoolId() throws Exception {
user.setSchoolId(SCHOOLID);
assertNotNull(user.getSchoolId());
}

@Test
public void getDisplayName() throws Exception {
assertEquals(user.getDisplayName(), DISPLAYNAME);
}

@Test
public void setDisplayName() throws Exception {
user.setDisplayName(DISPLAYNAME);
assertNotNull(user.getDisplayName());
}

}
57 changes: 57 additions & 0 deletions app/src/test/java/org/schulcloud/mobile/data/model/DeviceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.schulcloud.mobile.data.model;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DeviceTest {
private static final String _ID = "_ID";
private static final String TOKEN = "TOKEN";
private static final String TYPE = "MOBILE";
private static final String SERVICE = "SERVICE";
private static final String NAME = "NAME";
private static final String OS = "MAGIC";
private static final String STATE = "UNKNOWN";
private static final String UPDATEDAT = "YESTERDAY";
private static final String CREATEDAT = "NOW";
private static final String ACTIVE = "TRUE";

private Device device;

@Before
public void setUp() {
device = createNewDevice();
}

@Test
public void testGetProperties() {
assertEquals(device.name, NAME);
assertEquals(device._id, _ID);
assertEquals(device.token, TOKEN);
assertEquals(device.type, TYPE);
assertEquals(device.service, SERVICE);
assertEquals(device.OS, OS);
assertEquals(device.state, STATE);
assertEquals(device.updatedAt, UPDATEDAT);
assertEquals(device.createdAt, CREATEDAT);
assertEquals(device.active, ACTIVE);
}

public static Device createNewDevice() {
Device device = new Device();
device.name = NAME;
device._id = _ID;
device.token = TOKEN;
device.type = TYPE;
device.service = SERVICE;
device.OS = OS;
device.state = STATE;
device.createdAt = CREATEDAT;
device.updatedAt = UPDATEDAT;
device.active = ACTIVE;

return device;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.schulcloud.mobile.data.model;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DirectoryTest {
private static final String NAME = "NAME";

private Directory directory;

@Before
public void setUp() {
directory = createNewDirectory();
}

@Test
public void testGetProperties() {
assertEquals(directory.name, NAME);

}

public static Directory createNewDirectory() {
Directory directory = new Directory();
directory.name = NAME;

return directory;
}
}
Loading

0 comments on commit 5dac568

Please sign in to comment.