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

Commit

Permalink
Improved test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dov Katz committed Apr 3, 2018
1 parent d903f01 commit 7b7727c
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public SignalsClientImpl(SymAuth symAuth, SymphonyClientConfig config, Client ht
*/
@Override
public SymSignal createSignal(SymSignal signal) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
BaseSignal base = SymSignal.toBaseSignal(signal);
try {
Signal result = api.v1SignalsCreatePost(symAuth.getSessionToken().getToken(), base,
Expand All @@ -112,6 +112,14 @@ public SymSignal createSignal(SymSignal signal) throws SignalsException {
}
}

/**
* @return SignalsApi instance
*/
public SignalsApi createSignalsApi() {
SignalsApi api = new SignalsApi(apiClient);
return api;
}

/**
* Updates an existing signal
*
Expand All @@ -126,7 +134,7 @@ public SymSignal createSignal(SymSignal signal) throws SignalsException {
*/
@Override
public SymSignal updateSignal(String id, SymSignal signal) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
BaseSignal base = SymSignal.toBaseSignal(signal);
try {
Signal result = api.v1SignalsIdUpdatePost(symAuth.getSessionToken().getToken(), id, base,
Expand All @@ -146,7 +154,7 @@ public SymSignal updateSignal(String id, SymSignal signal) throws SignalsExcepti
*/
@Override
public void deleteSignal(String id) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
api.v1SignalsIdDeletePost(symAuth.getSessionToken().getToken(), id, symAuth.getKeyToken().getToken());
} catch (ApiException ex) {
Expand All @@ -166,7 +174,7 @@ public void deleteSignal(String id) throws SignalsException {
*/
@Override
public SymSignal getSignal(String id) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
Signal result = api.v1SignalsIdGetGet(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken());
Expand All @@ -191,7 +199,7 @@ public SymSignal getSignal(String id) throws SignalsException {
*/
@Override
public List<SymSignal> listSignals(int skip, int limit) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
SignalList result = api.v1SignalsListGet(symAuth.getSessionToken().getToken(),
symAuth.getKeyToken().getToken(), skip, limit);
Expand Down Expand Up @@ -220,7 +228,7 @@ public List<SymSignal> listSignals(int skip, int limit) throws SignalsException
@Override
public SymChannelSubscriberResponse listSubscribers(String id, BigDecimal skip, BigDecimal limit)
throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
ChannelSubscriberResponse result = api.v1SignalsIdSubscribersGet(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken(), skip, limit);
Expand All @@ -241,7 +249,7 @@ public SymChannelSubscriberResponse listSubscribers(String id, BigDecimal skip,
*/
@Override
public ChannelSubscriptionResponse subscribe(String id) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
return api.v1SignalsIdSubscribePost(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken(), null, null);
Expand Down Expand Up @@ -269,7 +277,7 @@ public ChannelSubscriptionResponse subscribe(String id) throws SignalsException
@Override
public ChannelSubscriptionResponse bulkSubscribe(String id, boolean pushed, List<BigDecimal> userIds)
throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
return api.v1SignalsIdSubscribePost(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken(), pushed, userIds);
Expand All @@ -289,7 +297,7 @@ public ChannelSubscriptionResponse bulkSubscribe(String id, boolean pushed, List
*/
@Override
public ChannelSubscriptionResponse unsubscribe(String id) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
return api.v1SignalsIdUnsubscribePost(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken(), null);
Expand All @@ -313,7 +321,7 @@ public ChannelSubscriptionResponse unsubscribe(String id) throws SignalsExceptio
*/
@Override
public ChannelSubscriptionResponse bulkUnsubscribe(String id, List<BigDecimal> userIds) throws SignalsException {
SignalsApi api = new SignalsApi(apiClient);
SignalsApi api = createSignalsApi();
try {
return api.v1SignalsIdUnsubscribePost(symAuth.getSessionToken().getToken(), id,
symAuth.getKeyToken().getToken(), userIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.symphonyoss.symphony.clients;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Spy;
import org.powermock.api.mockito.internal.mockcreation.RuntimeExceptionProxy;
import org.symphonyoss.client.SymphonyClient;
import org.symphonyoss.client.exceptions.SignalsException;
import org.symphonyoss.symphony.agent.api.SignalsApi;
import org.symphonyoss.symphony.agent.invoker.ApiException;
import org.symphonyoss.symphony.agent.model.BaseSignal;
import org.symphonyoss.symphony.agent.model.Signal;
import org.symphonyoss.symphony.agent.model.SuccessResponse;
import org.symphonyoss.symphony.clients.impl.SignalsClientImpl;
import org.symphonyoss.symphony.clients.model.SymSignal;
import org.symphonyoss.symphony.clients.model.SymSignalTest;
import org.symphonyoss.util.TestFactory;

public class SignalsClientTest {

SignalsClient signalsClient;
SymphonyClient testClient;

@Spy
SignalsApi api;
@Spy
SignalsClientImpl spySignalsClient;

@Before
public void setUp() {
testClient = TestFactory.getSymClient();
signalsClient = testClient.getSignalsClient();
spySignalsClient = spy(new SignalsClientImpl(testClient.getSymAuth(), testClient.getConfig()));
api = mock(SignalsApi.class);
when(spySignalsClient.createSignalsApi()).thenReturn(api);
}

@Test
public void getClientTest() throws Exception {
assertTrue("get DEFAULT client", SignalsFactory.getClient(testClient) instanceof SignalsClientImpl);
}

@Test
public void createSignalTest() throws Exception {
SymSignal signal = new SymSignal();
signal.setName(SymSignalTest.DUMMY_NAME);
signal.setQuery(SymSignalTest.DUMMY_QUERY);

Signal apiSignal = SymSignal.toSignal(signal);
when(api.v1SignalsCreatePost(anyString(), any(BaseSignal.class), anyString())).thenReturn(apiSignal);
SymSignal result = spySignalsClient.createSignal(signal);
assertEquals(SymSignalTest.DUMMY_NAME, result.getName());
assertEquals(SymSignalTest.DUMMY_QUERY, result.getQuery());
}

@Test
public void createSignalWithError() throws Exception {
when(api.v1SignalsCreatePost(anyString(), any(BaseSignal.class), anyString())).thenThrow(new ApiException());
try {
spySignalsClient.createSignal(new SymSignal());
} catch (Throwable sig) {
// NOTE we will get a runtimeExceptionProxy due to how this is mocked
assertTrue(sig instanceof RuntimeExceptionProxy);
// This is the part we would expect in real life.
assertTrue(sig.getCause() instanceof SignalsException);
assertTrue(sig.getCause().getCause() instanceof ApiException);
}
}

@Test
public void updateSignalTest() throws Exception {
SymSignal signal = new SymSignal();
signal.setName(SymSignalTest.DUMMY_NAME);
signal.setQuery(SymSignalTest.DUMMY_QUERY);
signal.setId(SymSignalTest.DUMMY_ID);
when(api.v1SignalsIdUpdatePost(anyString(), anyString(), any(BaseSignal.class), anyString())).thenReturn(SymSignal.toSignal(signal));
SymSignal result = spySignalsClient.updateSignal(signal.getId(), signal);
assertEquals(SymSignalTest.DUMMY_ID, result.getId());
}

@Test
public void deleteSignalTest() throws Exception {
SymSignal signal = new SymSignal();
signal.setName(SymSignalTest.DUMMY_NAME);
signal.setQuery(SymSignalTest.DUMMY_QUERY);
signal.setId(SymSignalTest.DUMMY_ID);
SuccessResponse resp=new SuccessResponse();
when(api.v1SignalsIdDeletePost(anyString(), anyString(), anyString())).thenReturn(resp);
spySignalsClient.deleteSignal(signal.getId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.symphonyoss.symphony.clients.model;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.symphonyoss.symphony.agent.model.BaseSignal;
import org.symphonyoss.symphony.agent.model.Signal;
import org.symphonyoss.symphony.agent.model.SignalList;

public class SymSignalTest {
public static final String DUMMY_ID = "dummy_id";
public static final String DUMMY_QUERY = "HASHTAG:dummy AND CASHTAG:query";
public static final String DUMMY_NAME = "Dummy Name for signal";
public static final Boolean COMPANY_WIDE = false;
public static final Boolean VISIBLE_ON_PROFILE = true;

public void compareSignalToSymSignal(Signal sig, SymSignal symSig) {
assertEquals(sig.getId(), symSig.getId());
assertEquals(sig.getName(), symSig.getName());
assertEquals(sig.getQuery(), symSig.getQuery());
assertEquals(sig.getCompanyWide(), symSig.getCompanyWide());
assertEquals(sig.getVisibleOnProfile(), symSig.getVisibleOnProfile());
}

public void compareBaseSignalToSymSignal(BaseSignal sig, SymSignal symSig) {
assertEquals(sig.getName(), symSig.getName());
assertEquals(sig.getQuery(), symSig.getQuery());
assertEquals(sig.getCompanyWide(), symSig.getCompanyWide());
assertEquals(sig.getVisibleOnProfile(), symSig.getVisibleOnProfile());
}

@Test
public void testToSymSignal() {
Signal sig = new Signal();
sig.id(DUMMY_ID);
sig.name(DUMMY_NAME);
sig.query(DUMMY_QUERY);
sig.companyWide(COMPANY_WIDE);
sig.visibleOnProfile(VISIBLE_ON_PROFILE);
SymSignal symSig = SymSignal.toSymSignal(sig);
compareSignalToSymSignal(sig, symSig);
}

@Test
public void testFromSymSignal() {
SymSignal symSig = new SymSignal();
symSig.setId(DUMMY_ID);
symSig.setName(DUMMY_NAME);
symSig.setQuery(DUMMY_QUERY);
symSig.setCompanyWide(COMPANY_WIDE);
symSig.setVisibleOnProfile(VISIBLE_ON_PROFILE);
Signal sig = SymSignal.toSignal(symSig);
compareSignalToSymSignal(sig, symSig);
}

@Test
public void toBaseSignalTest() {
SymSignal symSig = new SymSignal();
symSig.setName(DUMMY_NAME);
symSig.setQuery(DUMMY_QUERY);
symSig.setCompanyWide(COMPANY_WIDE);
symSig.setVisibleOnProfile(VISIBLE_ON_PROFILE);
BaseSignal sig = SymSignal.toBaseSignal(symSig);
compareBaseSignalToSymSignal(sig, symSig);
}

@Test
public void toSignalListTest() {
SignalList sigList=new SignalList();
Signal sig = new Signal();
sig.id(DUMMY_ID);
sig.name(DUMMY_NAME);
sig.query(DUMMY_QUERY);
sig.companyWide(COMPANY_WIDE);
sig.visibleOnProfile(VISIBLE_ON_PROFILE);
sigList.add(sig);
List<SymSignal> sigs=SymSignal.fromSignalList(sigList);
SymSignal symSig=sigs.get(0);
compareSignalToSymSignal(sig, symSig);
}
}

0 comments on commit 7b7727c

Please sign in to comment.