I am trying to write tests for spring boot application which has repositories.
My test class looks like this:
package se.volvo.spp.controller.spp;
import com.datastax.driver.core.utils.UUIDs;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.cassandra.repository.MapId;
import org.springframework.data.cassandra.repository.support.BasicMapId;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import se.volvo.spp.BaseTest;
import se.volvo.spp.config.JacksonConfig;
import se.volvo.spp.dao.cassandra.*;
import se.volvo.spp.dto.ServiceAgentDTO;
import se.volvo.spp.exception.NotFoundException;
import se.volvo.spp.repository.AssignmentRepository;
import se.volvo.spp.repository.ServiceAgentRepository;
import se.volvo.spp.service.impl.ServiceAgentService;
import javax.inject.Inject;
import java.util.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class ServiceAgentControllerTest extends BaseTest {
#Autowired
private MockMvc mockMvc;
#Mock
ServiceProviderDAO serviceProviderDAO;
#Mock
ServiceTypeDAO serviceTypeDAO;
#Mock
ServiceDefinitionDAO serviceDefinitionDAO;
#Mock
ServiceAgentDAO serviceAgentDAO;
#Mock
AssignmentDAO assignmentDAO;
#Mock
ServiceAgentRepository serviceAgentRepository;
#Mock
AssignmentRepository assignmentRepository;
#InjectMocks
ServiceAgentService serviceAgentService;
#Before
public void setUp() {
serviceAgentDAO.setId(UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b"));
serviceAgentDAO.setContactEmail("joe.smith#arwe.com");
serviceAgentDAO.setContactTelephone("0735333333");
serviceAgentDAO.setEnabled(true);
serviceAgentDAO.setFirstNames("Joe");
serviceAgentDAO.setLastName("Smith");
serviceAgentDAO.setServiceDefinitionIds(Arrays.asList(UUID.fromString("9a9cb34a-4221-11e8-842f-0ed5f89f718b")));
assignmentDAO.setId(UUID.fromString("69f7cd9c-423f-11e8-842f-0ed5f89f718b"));
assignmentDAO.setCustomerId(UUID.fromString("7b479d52-423f-11e8-842f-0ed5f89f718b"));
assignmentDAO.setCustomerName("John Gill");
assignmentDAO.setServiceAgentId(UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b"));
assignmentDAO.setServiceDefinitionId(UUID.fromString("9a9cb34a-4221-11e8-842f-0ed5f89f718b"));
assignmentDAO.setServiceSlotId(UUID.fromString("a7ff6543-4236-11e8-8706-313df4405fc7"));
assignmentDAO.setVehicle("WIN898987686");
/*when(serviceAgentRepository.findOne(BasicMapId.id("id", Mockito.any(UUID.class)))).thenAnswer(new Answer() {
#Override
public ServiceAgentDAO answer(InvocationOnMock invocationOnMock) throws Throwable {
System.out.println("ARGUMENT IS OUTSIDE :: "+(String)invocationOnMock.getArguments()[0]);
if(((String)invocationOnMock.getArguments()[0]).equals(UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b"))) {
System.out.println("ARGUMENT IS :: "+invocationOnMock.getArguments()[0]);
return serviceAgentDAO;
}
else
throw new NotFoundException("The Service Agent ID does not exist!");
}
}
);*/
//when(serviceAgentRepository.findOne(BasicMapId.id("id", AdditionalMatchers.not(Matchers.eq(UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b")))))).thenThrow (new NotFoundException("The ServiceAgent with that ID does not exist!"));
when(serviceAgentRepository.findOne(BasicMapId.id("id", UUID.class))).thenReturn (serviceAgentDAO);
List<AssignmentDAO> assignmentDAOList = Arrays.asList(assignmentDAO);
when(assignmentRepository.findByServiceAgentId(UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b"))).thenReturn(assignmentDAOList);
assignmentDAO.setServiceAgentId(null);
when(assignmentRepository.save(Mockito.any(AssignmentDAO.class))).thenReturn(assignmentDAO);
serviceAgentDAO.setEnabled(false);
when(serviceAgentRepository.save(Mockito.any(ServiceAgentDAO.class))).thenReturn(serviceAgentDAO);
}
#Test
public void testServiceAgentNotFound() throws Exception {
ObjectMapper objectMapper = JacksonConfig.createMapper();
ServiceAgentDTO serviceAgentDTO = new ServiceAgentDTO();
serviceAgentDTO.setEnabled(false);
this.mockMvc.perform(patch("/serviceagent/" + UUIDs.timeBased())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(serviceAgentDTO))).andDo(print())
.andExpect(status().isNotFound());
}
#Test
public void testServiceAgentIDNotAllowedInRequest() throws Exception {
ObjectMapper objectMapper = JacksonConfig.createMapper();
ServiceAgentDTO serviceAgentDTO = new ServiceAgentDTO();
serviceAgentDTO.setId(UUIDs.timeBased());
serviceAgentDTO.setEnabled(false);
this.mockMvc.perform(patch("/serviceagent/" + UUIDs.timeBased())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(serviceAgentDTO))).andDo(print())
.andExpect(status().isBadRequest());
}
/*
test to verify if the agent is disabled, then the agent is removed on his assignments as well
*/
#Test
public void testServiceAgentDisabledFromEnabled() throws Exception {
ObjectMapper objectMapper = JacksonConfig.createMapper();
// add a service provider
serviceProviderDAO.setId(UUID.fromString("8890e560-4170-11e8-a465-3f3e0c8fed10"));
serviceProviderDAO.setContactAddress("Torsgatan 190");
serviceProviderDAO.setContactTelephone("073q12345678");
serviceProviderDAO.setEnabled(true);
serviceProviderDAO.setName("Cleany Car");
// add a service type
serviceTypeDAO.setId(UUID.fromString("e3e0c006-4220-11e8-842f-0ed5f89f718b"));
serviceTypeDAO.setEnabled(true);
serviceTypeDAO.setName("Refuel Benzene 95");
serviceTypeDAO.setParentServiceTypeId(UUID.fromString("c1ecdf7a-4220-11e8-842f-0ed5f89f718b"));
serviceTypeDAO.setScopes(Arrays.asList("UNLOCK", "LOCK"));
// add service definition
serviceDefinitionDAO.setId(UUID.fromString("9a9cb34a-4221-11e8-842f-0ed5f89f718b"));
serviceDefinitionDAO.setEnabled(true);
serviceDefinitionDAO.setGeographicAvailability(null);
serviceDefinitionDAO.setLeadTimesInHours(24);
serviceDefinitionDAO.setMaxAdvanceTimeInDays(7);
Map<Integer, String> operatingWeekMap = new HashMap<>();
operatingWeekMap.put(1, "10:00&17:00");
operatingWeekMap.put(2, "09:00&17:00");
operatingWeekMap.put(3, "09:00&17:00");
operatingWeekMap.put(4, "09:00&17:00");
operatingWeekMap.put(5, "09:00&17:00");
operatingWeekMap.put(6, "11:00&17:00");
operatingWeekMap.put(7, "09:00&17:00");
serviceDefinitionDAO.setOperatingWeek(operatingWeekMap);
serviceDefinitionDAO.setServiceProviderId(UUID.fromString("8890e560-4170-11e8-a465-3f3e0c8fed10"));
serviceDefinitionDAO.setServiceSlotDurationInMinutes(60);
serviceDefinitionDAO.setServiceTypeId(UUID.fromString("e3e0c006-4220-11e8-842f-0ed5f89f718b"));
serviceDefinitionDAO.setTimeZoneId("Europe/London");
// we do not populate service slot as of now, since we just need to disable agent!
ServiceAgentDTO serviceAgentDTO = new ServiceAgentDTO();
serviceAgentDTO.setEnabled(false);
this.mockMvc.perform(patch("/serviceagent/" + UUID.fromString("e5cec1dc-422b-11e8-842f-0ed5f89f718b"))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(serviceAgentDTO))).andDo(print())
.andExpect(status().isOk());
}
}
The ServiceAgentService class has the constructor injection already for the repositories.
For some reason my mocked repositories in the setup() method do not take effect.
Also, the BaseTest class looks like below
package se.volvo.spp;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = Application.class)
#AutoConfigureMockMvc
#ActiveProfiles("dev")
#Ignore
public class BaseTest {
}
Any help is highly appreciated!
Related
I have the following two classes -
Service class
#Service
public class ProfileInfoCacheService {
public ProfileInfoResponse getProfileInfo(String uuid) {
ProfileInfoResponse response = getUserInfoFromIdentity(uuid);
return response;
}
public ProfileInfoResponse getUserInfoFromIdentity(String identity) {
LinkedHashMap<String, List<UserInfo>> profileInfo = userService.getUserInfoFromIdentity(identityList);
return profcache.getProfileInfo();
}
And component
#Component
public class UserInfoServiceImpl implements UserInfoService {
#SuppressWarnings("unchecked")
#Override
public LinkedHashMap<String, List<UserInfo>> getUserInfoFromIdentity(List<String> identityList) {
//Calls an API and returns data in LinkedHashMap.
}
Now, here is my test case file. Please bear with me as I am still new at it.
package com.citruspay.prepaid;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
//class file imports
import org.mockito.runners.MockitoJUnitRunner;
#RunWith(MockitoJUnitRunner.class)
public class ProfileInfoCacheServiceTest {
private PrepaidAccount account;
private ProfileInfoCacheService pics;
#Before
public void setUp() throws Exception {
account = new PrepaidAccount("8588844251");
//pics = new ProfileInfoCacheService();
ProfileInfoCacheService pics = mock(ProfileInfoCacheService.class);
}
#Test
public void testConstructor() {
assertEquals("8588888888", account.owner());
}
#Test
public void testNumber() throws Exception {
ProfileInfoResponse response = pics.getProfileInfo("7266704751953077361"); //pics is null here
assertEquals("", response.getEmail() );
assertEquals("8588888888", response.getMobile() );
}
}
The response of getProfileInfo() call is expected to be in this format -
{
"email": "some.email#something.in",
"mobile": "8888888888",
}
Every mobile/email pair has a number (like 7266704751953077361), using which, when the API is called, it returns the same.
However, I am getting a Null Pointer exception, due to pics being null where I call pics.getProfileInfo("7266704751953077361").
Please help me correct my mistake here.
Update
Upon changing mock() to new class call, I get the ProfileInfoCacheService object but then on moving the debug to the last line of the setup() method, getting the following -
java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at com.citruspay.prepaid.common.user.beans.ProfileInfoCacheService.<init>(ProfileInfoCacheService.java:42)
at com.citruspay.prepaid.ProfileInfoCacheServiceTest.setUp(ProfileInfoCacheServiceTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
... 33 more
Update 2
Adding more details of ProfileInfoCacheService class -
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.exceptions.JedisException;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Key;
import com.citruspay.prepaid.common.cache.dao.ProfileInfoCacheDao;
import com.citruspay.prepaid.common.cache.entity.ProfileInfoCache;
import com.citruspay.prepaid.common.cache.factory.DaoCacheFactory;
import com.citruspay.prepaid.common.user.process.UserInfoServiceImpl;
import com.spikeify.ResultSet;
#Service
public class ProfileInfoCacheService {
#Autowired
UserInfoServiceImpl userService;
#Autowired
DaoCacheFactory<ProfileInfoCacheDao> daoCacheFactory;
#PostConstruct
public void init() {
ProfileInfoCacheDao profileInfoCacheDao = daoCacheFactory.getDaoForRead(ProfileInfoCacheDao.class);
profileInfoCacheDao.init();
}
Logger logger = LoggerFactory.getLogger(ProfileInfoCacheService.class);
I am using easymock framework to write UT's. I have one class which uses java.util.Properties in public function. Below is sample code:
public URI generateSASURI() throws CloudStorageException, DataProcessingException {
final String token = "token";
try {
return new URI(URIUtils.getAssetURI(azureStorageProperties.getProperty(StorageConstant.STORAGE_ACCOUNT_NAME)
, cloudBlob).toString() + "?" + token);
} catch (URISyntaxException e) {
throw new DataProcessingException("Error while creating SAS URL " + e.getMessage(), e);
}
}
where azureStorageProperties is instance of java.util.Properties, which is created as bean and injected in class.
Now while writing unit test case , i am trying to mock Properties azureStorageProperties, i am getting below error:
Unable to evaluate the expression Method threw 'java.lang.IllegalStateException' exception.
Below is ss:
Below is class to be tested:
package com.company.ops.azure.storage;
import com.company.ops.azure.storage.constants.StorageConstant;
import com.company.ops.azure.storage.dto.CloudBlob;
import com.company.ops.cloudopsfacade.dto.SignedUrlDTO;
import com.company.ops.cloudopsfacade.exception.storage.CloudStorageException;
import com.company.ops.cloudopsfacade.exception.storage.DataProcessingException;
import com.company.ops.azure.storage.util.URIUtils;
import com.company.ops.cloudopsfacade.storage.ICloudStorageClient;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.SharedAccessBlobHeaders;
import com.microsoft.azure.storage.blob.SharedAccessBlobPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
public class StorageClient implements ICloudStorageClient {
private static final Logger LOG = LoggerFactory.getLogger(StorageClient.class);
#Inject
private CloudBlobClient cloudBlobClient;
#Inject
Properties azureStorageProperties;
#Inject
private SASTokenGenerator sasTokenGenerator;
private long sasUrlDuration;
private Optional<String> sasUrlDurationKey;
public StorageClient() {
sasUrlDurationKey = Optional.ofNullable(System.getenv(StorageConstant.SAS_URL_DURATION_KEY));
if (sasUrlDurationKey.isPresent()) {
try {
sasUrlDuration = Integer.parseInt(sasUrlDurationKey.get());
} catch(NumberFormatException ex) {
LOG.debug("sasURLDurationKey invalid" + ex.getMessage());
sasUrlDuration = StorageConstant.DEFAULT_SAS_URL_DURATION;
}
} else {
sasUrlDuration = StorageConstant.DEFAULT_SAS_URL_DURATION;
}
}
//NOTE: This constructor is just created for test case. As #Mock of final class is NOT supported in easymock
public StorageClient(long sasUrlDuration, CloudBlobClient cloudBlobClient) {
this.sasUrlDuration = sasUrlDuration;
this.cloudBlobClient = cloudBlobClient;
}
/**
*
* #param containerName
* #param blob
* #param expiryTime
* #return {#link URI}
* #throws CloudStorageException
* #throws DataProcessingException
*/
private URI generateSASURI(String containerName,
String blob, Long expiryTime) throws CloudStorageException, DataProcessingException {
CloudBlob cloudBlob = new CloudBlob(Optional.ofNullable(containerName)
.orElseThrow(() -> new CloudStorageException("container name is null")),
Optional.ofNullable(blob).orElseThrow(() -> new CloudStorageException("blob name is null")));
//#TODO: Need to check permissions: Currently create and write assigned
final Set<SharedAccessBlobPermissions> permissions = EnumSet.of(SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.CREATE);
final SharedAccessBlobHeaders contentHeaders = new SharedAccessBlobHeaders();
//in case if duration need to set manually via api
if (Optional.ofNullable(expiryTime).isPresent()) {
sasUrlDuration = expiryTime;
}
sasTokenGenerator.sasTokenGeneratorInitializer(
cloudBlobClient,
cloudBlob,
permissions,
sasUrlDuration,
contentHeaders);
final String token = sasTokenGenerator.getToken();
try {
return new URI(URIUtils.getAssetURI(azureStorageProperties.getProperty(StorageConstant.STORAGE_ACCOUNT_NAME)
, cloudBlob).toString() + "?" + token);
} catch (URISyntaxException e) {
throw new DataProcessingException("Error while creating SAS URL " + e.getMessage(), e);
}
}
/**
*
* #param containerName
* #param blob
* #param expiryTime
* #return {#link URI}
* #throws CloudStorageException
* #throws DataProcessingException
*/
#Override
public SignedUrlDTO generateSignedUrl(String containerName, String blob, Long expiryTime)
throws CloudStorageException, DataProcessingException, MalformedURLException {
try {
URI uri = generateSASURI(containerName, blob, expiryTime);
SignedUrlDTO signedUrlDTO = new SignedUrlDTO();
signedUrlDTO.setSignedURL(uri.toURL());
return signedUrlDTO;
} catch (DataProcessingException ex) {
LOG.error(ex.getMessage());
throw ex;
} catch (CloudStorageException ex) {
LOG.error(ex.getMessage());
throw ex;
} catch (MalformedURLException e) {
LOG.error("Unable to get URL");
throw e;
}
}
}
Below is complete code for UT
package com.company.ops.azure.storage;
import com.company.ops.azure.constants.AzureConstants;
import com.company.ops.azure.storage.constants.StorageConstant;
import com.company.ops.azure.storage.util.URIUtils;
import com.company.ops.cloudopsfacade.dto.SignedUrlDTO;
import com.company.ops.cloudopsfacade.exception.storage.CloudStorageException;
import com.company.ops.cloudopsfacade.exception.storage.DataProcessingException;
import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import org.easymock.*;
import static org.easymock.EasyMock.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Properties;
#RunWith(EasyMockRunner.class)
public class StorageClientTest extends EasyMockSupport {
private CloudBlobClient cloudBlobClient;
#Mock
private SASTokenGenerator sasTokenGenerator;
#Mock
private Properties azureStorageProperties;
#Mock
private StorageClient storageClient; /*= /*new StorageClient(StorageConstant.DEFAULT_SAS_URL_DURATION,
cloudBlobClient, azureStorageProperties);*/
#Before
public void setup() throws URISyntaxException {
resetAll();
}
#After
public void tearDown() {
verifyAll();
}
#Test
public void testGenerateSASURI() throws MalformedURLException, CloudStorageException, DataProcessingException {
String containerName = "myprojectlocal";
String blob = "testfile";
Long expiryTime = 1000L;
/*Properties azureStorageProperties = EasyMock.createMockBuilder(Properties.class)
.addMockedMethod("getProperty", String.class).createMock();*/
//azureStorageProperties = new Properties();
// azureStorageProperties.setProperty(StorageConstant.STORAGE_ACCOUNT_NAME, "storage-account-name");
expect(azureStorageProperties.getProperty(StorageConstant.STORAGE_ACCOUNT_NAME)).andReturn("storage-account-name");
expect(sasTokenGenerator.getToken()).andReturn("token");
sasTokenGenerator.sasTokenGeneratorInitializer(anyObject(CloudBlobClient.class),
anyObject(),
anyObject(),
anyLong(),
anyObject());
expectLastCall();
//azureStorageProperties.getProperty(anyString()); //.andReturn("storage-account-name");
//expectLastCall();
//expect(azureStorageProperties.getProperty(StorageConstant.STORAGE_ACCOUNT_NAME)).andReturn("storage-account-name");
replayAll();
SignedUrlDTO signedUrlDTO = storageClient.generateSignedUrl(containerName, blob, expiryTime);
Assert.assertNotNull(signedUrlDTO);
}
private CloudBlobClient getCloudBlobClient() {
final StorageCredentials credentials = new StorageCredentialsAccountAndKey(TestConstants.STORAGE_ACCOUNT_NAME,
TestConstants.STORAGE_ACCOUNT_KEY);
try {
return new CloudBlobClient(URIUtils.getStorageAccountURI(TestConstants.STORAGE_ACCOUNT_NAME), credentials);
} catch (URISyntaxException e) {}
return null;
}
}
I am using Junit 5 and getting the following error while importing collection packages
The import org.hamcrest.collection cannot be resolved
I need to validate assertThat method & hasSize() from collection for which the below import should be done.
import org.hamcrest.collection.IsEmptyCollection;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.MatcherAssert.assertThat;
import java.awt.List;
import java.util.ArrayList;
public class PortJUnit {
PortBO portBO;
ArrayList<Port> list = new ArrayList<Port>();
#Before
public void createObjectForPort()
{
portBO = new PortBO();
}
#Test
public void testPortDetails()
{
list.add(new Port(101,"abc","cbe"));
list.add(new Port(102,"abd","chennai"));
list.add(new Port(103,"abe","bangalore"));
list.add(new Port(104,"abf","mumbai"));
list.add(new Port(105,"abg","delhi"));
String detail = "107,abh,Toronto";
portBO.addElementAtSpecfiedPosition(list, 6, detail);
assertThat(list, hasSize(6));
}
}
I suggest that you use hamcrest-core dependency.
Please try to use instead - hamcrest-all-1.3.jar and your problem will be solved.
I have two soap end points(soap services) deployed in one server. When i override the following interceptor, it is applying to both services. How to enable/disable the interceptor specific to one service. Kindly help
The interceptor code as follows.
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(true);
validatingInterceptor.setValidateResponse(true);
validatingInterceptor.setXsdSchemaCollection(LogAnalyzerFile());
interceptors.add(validatingInterceptor);
}
Note: Its an spring boot project, using annotations.
I
package com.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurationSupport;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.SmartEndpointInterceptor;
import org.springframework.ws.server.endpoint.MethodEndpoint;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
import org.springframework.ws.wsdl.wsdl11.Wsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
#EnableWs
#Configuration
public class TestConfig extends WsConfigurationSupport implements SmartEndpointInterceptor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
#Bean(name="testlog")
public ServletRegistrationBean testlog(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "/File/*");
servletRegistrationBean.setName("Log");
return servletRegistrationBean;
}
#Bean(name = "testFile")
public Wsdl11Definition testFile()
{
SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
wsdl11Definition.setWsdl(new ClassPathResource("test.wsdl"));
logger.info("test.wsdl:");
return wsdl11Definition;
}
#Bean(name = "UploadLogFile")
public XsdSchema UploadLogFile() {
return new SimpleXsdSchema(new ClassPathResource("1.xsd"));
}
#Bean(name = "ErrorInfo")
public XsdSchema ErrorInfo() {
return new SimpleXsdSchema(new ClassPathResource("2.xsd"));
}
public Resource[] getSchemas() {
List<Resource> schemaResources = new ArrayList<>();
schemaResources.add(new ClassPathResource("1.xsd"));
schemaResources.add(new ClassPathResource("2.xsd"));
return schemaResources.toArray(new Resource[schemaResources.size()]);
}
#Override
public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
if (endpoint instanceof MethodEndpoint) {
MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
return methodEndpoint.getMethod().getDeclaringClass() == YourEndpoint.class;
}
return false;
}
private Boolean validateSchema(Source source_, MessageContext messageContext) throws Exception {
boolean errorFlag = true;
SchemaFactory _schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema _schema = _schemaFactory.newSchema(getSchemas()[0].getFile());
Validator _validator = _schema.newValidator();
DOMResult _result = new DOMResult();
try {
_validator.validate(source_, _result);
} catch (SAXException _exception) {
errorFlag = false;
SoapMessage response = (SoapMessage) messageContext.getResponse();
String faultString = StringUtils.hasLength(_exception.getMessage()) ? _exception.getMessage() : _exception.toString();
SoapBody body = response.getSoapBody();
body.addServerOrReceiverFault(faultString, Locale.US);
_exception.printStackTrace();
}
return errorFlag;
}
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
WebServiceMessage webServiceMessageRequest = messageContext.getRequest();
SoapMessage soapMessage = (SoapMessage) webServiceMessageRequest;
SoapHeader soapHeader = soapMessage.getSoapHeader();
Source bodySource = soapMessage.getSoapBody().getPayloadSource();
return validateSchema(bodySource,messageContext);
}
#Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
#Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
return false;
}
#Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
}
}
i did this tutorial http://netbeans.org/kb/docs/websvc/rest.html to create a restful webservice with my mysql db.
the basic things works fine, but now i want to extend the service functionality. how can i add additional parameters to the GET service ?
i tried this
but as a result I have all cities.
when I add the parameter countryCode links the service becomes
http://localhost:8080/Data/resources/converter.city/?
countryCode=TUR×tamp=1323114935089
This is my code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package service;
import converter.City;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
/**
*
* #author mehdi
*/
#Stateless
#Path("converter.city")
public class CityFacadeREST extends AbstractFacade<City> {
#PersistenceContext(unitName = "DataPU")
private EntityManager em;
public CityFacadeREST() {
super(City.class);
}
#POST
#Override
#Consumes({"application/xml", "application/json"})
public void create(City entity) {
super.create(entity);
}
#PUT
#Override
#Consumes({"application/xml", "application/json"})
public void edit(City entity) {
super.edit(entity);
}
#DELETE
#Path("{id}")
public void remove(#PathParam("id") Integer id) {
super.remove(super.find(id));
}
#GET
#Path("{id}")
#Produces({"application/xml", "application/json"})
public City find(#PathParam("id") Integer id) {
return super.find(id);
}
#GET
#Override
#Produces({"application/xml", "application/json"})
public List<City> findAll() {
return super.findAll();
}
#GET
#Path("{from}/{to}")
#Produces({"application/xml", "application/json"})
public List<City> findRange(#PathParam("from") Integer from, #PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
#GET
#Path("count")
#Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
#java.lang.Override
protected EntityManager getEntityManager() {
return em;
}
}
You will have to update the annotations in your Java resource class to accept the additional parameter(s). Have you looked at the annotation support for query parameters in JAX-RS?