Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit dcc4b73

Browse files
author
Peter Nied
authored
Merge pull request #17 from TBag/master
Bug fix and adding unit tests.
2 parents cde4195 + 9311679 commit dcc4b73

34 files changed

Lines changed: 1659 additions & 149 deletions

graphsdk/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ dependencies {
6262
compile ('com.google.code.gson:gson:2.3.1') {
6363
exclude module: 'com.google.code.gson'
6464
}
65+
androidTestCompile 'junit:junit:4.12'
6566
}
6667

6768
uploadArchives {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.microsoft.graph.authentication;
2+
3+
import com.microsoft.graph.http.IHttpRequest;
4+
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
/**
8+
* Mock authenticationProvider {@see IAuthenticationProvider}
9+
*/
10+
public class MockAuthenticationProvider implements IAuthenticationProvider {
11+
12+
/**
13+
* Interception counter
14+
*/
15+
private AtomicInteger mInterceptionCount = new AtomicInteger(0);
16+
17+
@Override
18+
public void authenticateRequest(IHttpRequest request) {
19+
mInterceptionCount.incrementAndGet();
20+
}
21+
22+
/**
23+
* Gets the number of intercepted requests
24+
* @return The number of intercepted requests
25+
*/
26+
public int getInterceptionCount() {
27+
return mInterceptionCount.get();
28+
}
29+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.microsoft.graph.concurrency;
2+
3+
import android.os.Looper;
4+
import android.test.AndroidTestCase;
5+
import com.microsoft.graph.core.ClientException;
6+
import com.microsoft.graph.core.GraphErrorCodes;
7+
import com.microsoft.graph.logger.MockLogger;
8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
import java.util.concurrent.atomic.AtomicReference;
11+
12+
/**
13+
* Test cases for {@see DefaultExecutors}
14+
*/
15+
public class DefaultExecutorsTests extends AndroidTestCase {
16+
17+
private MockLogger mLogger;
18+
private DefaultExecutors defaultExecutors;
19+
20+
@Override
21+
protected void setUp() throws Exception {
22+
super.setUp();
23+
mLogger = new MockLogger();
24+
defaultExecutors = new DefaultExecutors(mLogger);
25+
}
26+
27+
public void testNotNull() {
28+
assertNotNull(mLogger);
29+
assertNotNull(defaultExecutors);
30+
}
31+
32+
public void testPerformOnBackground() {
33+
final String expectedLogMessage = "Starting background task, current active count: 0";
34+
final String expectedResult = "test perform on background success";
35+
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
36+
37+
defaultExecutors.performOnBackground(new Runnable() {
38+
@Override
39+
public void run() {
40+
callback.success(expectedResult);
41+
}
42+
});
43+
44+
callback._completionWaiter.waitForSignal();
45+
assertEquals(CallingState.Background,callback._callingState.get());
46+
assertTrue(callback._successCalled.get());
47+
assertEquals(expectedResult, callback._successResult.get());
48+
assertEquals(1,mLogger.getLogMessages().size());
49+
assertTrue(mLogger.hasMessage(expectedLogMessage));
50+
}
51+
52+
public void testPerformOnForegroundWithResult() {
53+
final String expectedResult = "result value";
54+
final String expectedLogMessage = "Starting foreground task, current active count:0, with result result value";
55+
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
56+
57+
defaultExecutors.performOnForeground(expectedResult,callback);
58+
59+
callback._completionWaiter.waitForSignal();
60+
assertEquals(CallingState.Foreground,callback._callingState.get());
61+
assertTrue(callback._successCalled.get());
62+
assertFalse(callback._failureCalled.get());
63+
assertEquals(expectedResult, callback._successResult.get());
64+
assertEquals(1,mLogger.getLogMessages().size());
65+
assertTrue(mLogger.hasMessage(expectedLogMessage));
66+
}
67+
68+
public void testPerformOnForegroundWithProgress() throws Exception {
69+
final String expectedLogMessage = "Starting foreground task, current active count:0, with progress 1, max progress2";
70+
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
71+
final int expectedCurrentValue = 1;
72+
final int expectedMaxValue = 2;
73+
74+
defaultExecutors.performOnForeground(expectedCurrentValue, expectedMaxValue, callback);
75+
76+
callback._completionWaiter.waitForSignal();
77+
assertEquals(CallingState.Foreground, callback._callingState.get());
78+
assertFalse(callback._successCalled.get());
79+
assertFalse(callback._failureCalled.get());
80+
assertTrue(callback._progressCalled.get());
81+
assertEquals(expectedCurrentValue, callback._progressResultCurrent.get());
82+
assertEquals(expectedMaxValue, callback._progressResultMax.get());
83+
assertEquals(1,mLogger.getLogMessages().size());
84+
assertTrue(mLogger.hasMessage(expectedLogMessage));
85+
}
86+
87+
public void testPerformOnForegroundWithClientException() {
88+
final String expectedExceptionMessage = "client exception message";
89+
final String expectedLogMessage = "Starting foreground task, current active count:0, with exception com.microsoft.graph.core.ClientException: client exception message";
90+
final ExecutorTestCallback<String> callback = new ExecutorTestCallback<>();
91+
92+
defaultExecutors.performOnForeground(new ClientException(expectedExceptionMessage,null, GraphErrorCodes.InvalidAcceptType),
93+
callback);
94+
95+
callback._completionWaiter.waitForSignal();
96+
assertEquals(CallingState.Foreground, callback._callingState.get());
97+
assertFalse(callback._successCalled.get());
98+
assertTrue(callback._failureCalled.get());
99+
assertEquals(expectedExceptionMessage, callback._exceptionResult.get().getMessage());
100+
assertTrue(callback._exceptionResult.get().isError(GraphErrorCodes.InvalidAcceptType));
101+
assertEquals(1,mLogger.getLogMessages().size());
102+
assertTrue(mLogger.hasMessage(expectedLogMessage));
103+
}
104+
105+
private class ExecutorTestCallback<T> implements IProgressCallback<T> {
106+
SimpleWaiter _completionWaiter = new SimpleWaiter();
107+
AtomicReference<CallingState> _callingState = new AtomicReference<>(CallingState.Unknown);
108+
109+
AtomicBoolean _successCalled = new AtomicBoolean(false);
110+
AtomicReference<T> _successResult = new AtomicReference<>();
111+
112+
AtomicBoolean _failureCalled = new AtomicBoolean(false);
113+
AtomicReference<ClientException> _exceptionResult = new AtomicReference<>();
114+
115+
AtomicBoolean _progressCalled = new AtomicBoolean(false);
116+
AtomicLong _progressResultCurrent = new AtomicLong(-1);
117+
AtomicLong _progressResultMax = new AtomicLong(-1);
118+
119+
@Override
120+
public void success(final T result) {
121+
_successCalled.set(true);
122+
_successResult.set(result);
123+
_callingState.set(getCallingState());
124+
_completionWaiter.signal();
125+
}
126+
127+
@Override
128+
public void failure(final ClientException ex) {
129+
_failureCalled.set(true);
130+
_exceptionResult.set(ex);
131+
_callingState.set(getCallingState());
132+
_completionWaiter.signal();
133+
}
134+
135+
@Override
136+
public void progress(final long current, final long max) {
137+
_progressCalled.set(true);
138+
_progressResultCurrent.set(current);
139+
_progressResultMax.set(max);
140+
_callingState.set(getCallingState());
141+
_completionWaiter.signal();
142+
}
143+
144+
private CallingState getCallingState(){
145+
return Looper.myLooper() == Looper.getMainLooper() ? CallingState.Foreground : CallingState.Background;
146+
}
147+
}
148+
149+
private enum CallingState {
150+
Foreground, Background, Unknown;
151+
}
152+
}

graphsdk/src/androidTest/java/com/microsoft/graph/concurrency/MockExecutors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.microsoft.graph.core.ClientException;
2626

2727
/**
28-
* Mock {@see IExecutors}, everything runs on the current thread
28+
* Mock for {@see IExecutors}
2929
*/
3030
public class MockExecutors implements IExecutors {
3131

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.microsoft.graph.concurrency;
2+
3+
import android.test.AndroidTestCase;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
7+
/**
8+
* Test cases for {@see SynchronousExecutor}
9+
*/
10+
public class SynchronousExecutorTests extends AndroidTestCase {
11+
12+
public void testExecute() throws Exception {
13+
final AtomicBoolean success = new AtomicBoolean(false);
14+
final SimpleWaiter simpleWaiter = new SimpleWaiter();
15+
SynchronousExecutor synchronousExecutor = new SynchronousExecutor();
16+
synchronousExecutor.execute(new Runnable() {
17+
@Override
18+
public void run() {
19+
success.set(true);
20+
simpleWaiter.signal();
21+
}
22+
});
23+
simpleWaiter.waitForSignal();
24+
Thread.sleep(2000);
25+
assertTrue(success.get());
26+
assertEquals(0, synchronousExecutor.getActiveCount());
27+
}
28+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.microsoft.graph.core;
2+
3+
import android.test.AndroidTestCase;
4+
5+
import com.microsoft.graph.authentication.IAuthenticationProvider;
6+
import com.microsoft.graph.authentication.MockAuthenticationProvider;
7+
import com.microsoft.graph.concurrency.IExecutors;
8+
import com.microsoft.graph.concurrency.MockExecutors;
9+
import com.microsoft.graph.http.IHttpProvider;
10+
import com.microsoft.graph.http.MockHttpProvider;
11+
import com.microsoft.graph.logger.ILogger;
12+
import com.microsoft.graph.logger.MockLogger;
13+
import com.microsoft.graph.serializer.ISerializer;
14+
import com.microsoft.graph.serializer.MockSerializer;
15+
16+
/**
17+
* Test cases for {@see BaseClient}
18+
*/
19+
public class BaseClientTests extends AndroidTestCase {
20+
21+
public static final String DEFAULT_GRAPH_ENDPOINT = "https://graph.microsoft.com/v1.0";
22+
private String mEndpoint = DEFAULT_GRAPH_ENDPOINT;
23+
private BaseClient baseClient;
24+
private IAuthenticationProvider mAuthenticationProvider;
25+
private IExecutors mExecutors;
26+
private IHttpProvider mHttpProvider;
27+
private ILogger mLogger;
28+
private ISerializer mSerializer;
29+
30+
@Override
31+
protected void setUp() throws Exception {
32+
super.setUp();
33+
baseClient = new BaseClient() {
34+
@Override
35+
public String getServiceRoot() {
36+
return mEndpoint;
37+
}
38+
39+
@Override
40+
public void setServiceRoot(String value) {
41+
mEndpoint = value;
42+
}
43+
};
44+
mAuthenticationProvider = new MockAuthenticationProvider();
45+
mExecutors = new MockExecutors();
46+
mHttpProvider = new MockHttpProvider();
47+
mLogger = new MockLogger();
48+
mSerializer = new MockSerializer(null, "");
49+
}
50+
51+
public void testNotNull() {
52+
assertNotNull(baseClient);
53+
assertNotNull(mAuthenticationProvider);
54+
assertNotNull(mExecutors);
55+
assertNotNull(mHttpProvider);
56+
assertNotNull(mLogger);
57+
assertNotNull(mSerializer);
58+
}
59+
60+
public void testValidateThrowException() {
61+
Boolean success = false;
62+
try {
63+
baseClient.validate();
64+
}catch (NullPointerException nEx){
65+
success = true;
66+
}
67+
assertTrue(success);
68+
}
69+
70+
public void testValidateSuccess() throws Exception {
71+
baseClient.setAuthenticationProvider(mAuthenticationProvider);
72+
baseClient.setExecutors(mExecutors);
73+
baseClient.setHttpProvider(mHttpProvider);
74+
baseClient.setSerializer(mSerializer);
75+
baseClient.validate();
76+
}
77+
78+
public void testEndPoint() {
79+
assertEquals(DEFAULT_GRAPH_ENDPOINT, baseClient.getServiceRoot());
80+
String expectedServiceRoot = "https://foo.bar";
81+
baseClient.setServiceRoot(expectedServiceRoot);
82+
assertEquals(expectedServiceRoot, baseClient.getServiceRoot());
83+
}
84+
85+
public void testAuthenticationProvider() {
86+
assertNull(baseClient.getAuthenticationProvider());
87+
baseClient.setAuthenticationProvider(mAuthenticationProvider);
88+
assertEquals(mAuthenticationProvider, baseClient.getAuthenticationProvider());
89+
}
90+
91+
public void testExecutors() {
92+
assertNull(baseClient.getExecutors());
93+
baseClient.setExecutors(mExecutors);
94+
assertEquals(mExecutors, baseClient.getExecutors());
95+
}
96+
97+
public void testHttpProvider() {
98+
assertNull(baseClient.getHttpProvider());
99+
baseClient.setHttpProvider(mHttpProvider);
100+
assertEquals(mHttpProvider, baseClient.getHttpProvider());
101+
}
102+
103+
public void testLogger() {
104+
assertNull(baseClient.getLogger());
105+
baseClient.setLogger(mLogger);
106+
assertEquals(mLogger, baseClient.getLogger());
107+
}
108+
109+
public void testSerializer() {
110+
assertNull(baseClient.getSerializer());
111+
baseClient.setSerializer(mSerializer);
112+
assertEquals(mSerializer, baseClient.getSerializer());
113+
}
114+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.microsoft.graph.core;
2+
3+
import android.test.AndroidTestCase;
4+
5+
/**
6+
* Test cases for {@see ClientException}
7+
*/
8+
public class ClientExceptionTests extends AndroidTestCase {
9+
10+
private GraphErrorCodes graphErrorCodes;
11+
private ClientException clientException;
12+
private String expectMessage = "This is test exception message";
13+
14+
@Override
15+
protected void setUp() throws Exception {
16+
super.setUp();
17+
graphErrorCodes = GraphErrorCodes.AccessDenied;
18+
clientException = new ClientException(expectMessage, null, graphErrorCodes);
19+
}
20+
21+
public void testNotNull() {
22+
assertNotNull(clientException);
23+
}
24+
25+
public void testClientException() {
26+
assertTrue(clientException.isError(graphErrorCodes));
27+
assertEquals(expectMessage, clientException.getMessage());
28+
}
29+
}
30+

0 commit comments

Comments
 (0)