How to write expect statement for new keyword using EasyMock? - new-operator

I want to write expect statement for line filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE); using EasyMock only.
//SingleLimit.java
private Limitation filter;
protected final Limitation getBaseLimitation() {
Validate.notNull(type);
GroupClass Group = new GroupClass(GroupTypeClass.SELECTOR);
if (Activatable.class.isAssignableFrom(typeListed)) {
if (A.class.isAssignableFrom(type)) {
//expect statement for below line
filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
}
}
}
//Simple.java
public class Simple implements Serializable, Limitation
{
public Simple(String path, Operator operator, Object value) {
this(path, operator, new Object[]{value});
}
}
Any help would be appreciated

I'd create a new class that creates Simple objects.
public class SimpleFactory {
public Simple createSimple(....) {
return new Simple(....);
}
}
Than incject this factory into your class or pass it as parameter
public YourClazz(SimpleFactory simpleFactory) {
//constructor
this.simpleFactory = simpleFactory;
protected final Limitation getBaseLimitation() {
///...
//filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
//change this to
filter = simpleFactory.createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
}
//or pass as parameter
protected final Limitation getBaseLimitation(SimpleFactory simpleFacotry) {
///...
//filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
//change this to
filter = simpleFactory.createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
}
}
Now in your test code create a mock of simpleFactory.
//edited 2019/11/16 another solution
If you can not create other class, create new method in your class, then use it for creating new Simple object:
public YourClazz {
protected Simple createSimple(....) {
return new Simple(....);
}
protected final Limitation getBaseLimitation() {
//...
//filter = new Simple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
//change this to
filter = createSimple(A.ATTRIBUTE_ACTIVE, Operator.EQUALS, Boolean.TRUE);
//...
}
}
Now for testing you can create a partial mock of your class and mock createSimple method:
YourClazz mock = partialMockBuilder(YourClazz.class)
.addMockedMethod("createSimple").createMock();
Simple expectedSimple = new Simple(....);
expect(mock.createSimple(.....).andReturn(expectedSimple);
replay(mock);

Related

How to setup Elsa with custom schema and table creation without entity framework

Need to change Schema for Elsa tables
Need to avoid DDL operation for Elsa hosting service so I want to create all required tables before starting Elsa
I tried to override ElsaContext, but I do not have a clear idea of the implementation. I tried the following:
public class SqlElsaContextFactory : IDesignTimeDbContextFactory<ElsaContext>
{
public ElsaContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddCommandLine(args)
.Build();
var dbContextBuilder = new DbContextOptionsBuilder();
var connectionString = configuration.GetConnectionString("Elsa");
dbContextBuilder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(typeof(Program).Assembly.FullName));
return new ElsaContextExtension(dbContextBuilder.Options);
}
}
public class ElsaContextExtension : ElsaContext
{
public ElsaContextExtension(DbContextOptions options) : base(options)
{
}
protected string _schema = "test";
public override string Schema => _schema;
}
But getting the below error:
You can inherit from ElsaContext and override what you need. Something like this:
public class WorkflowDbContext : ElsaContext, IDbContext
{
public bool ProxyCreationEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool AutoDetectChangesEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public WorkflowDbContext(string connectionString) : base(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options)
{
}
}
Then you can generate the SQL scripts like this:
private string CreateDatabaseScript()
{
return this.Database.GetService<IRelationalDatabaseCreator>().GenerateCreateScript();
}
Then install the scripts like this:
public void Install()
{
var scriptsBatch = this.CreateDatabaseScript();
foreach (var script in scriptsBatch.Split("GO", StringSplitOptions.RemoveEmptyEntries))
{
this.Database.ExecuteSqlRaw(script);
}
}
So by calling this Install method you can generate the tables before starting Elsa.

dynamic values in tags object of swagger

I want to provide values from properties file in tags section of the swagger for ex: tags = "${metric.tags}" but not able to pickup from properties file. for values it is working fine value = "${metric.value}".
I have made plugin configuration in swagger configuration file and it started working as per my requirement.
#Bean
public TranslationOperationBuilderPlugin translationPlugin() {
return new TranslationOperationBuilderPlugin();
}
#Order(Ordered.LOWEST_PRECEDENCE)
public static class TranslationOperationBuilderPlugin implements OperationBuilderPlugin {
#Autowired
Environment environment;
#Override
public boolean supports(DocumentationType delimiter) {
return true;
}
#Override
public void apply(OperationContext context) {
String summary = context.operationBuilder().build().getSummary();
String notes = context.operationBuilder().build().getNotes();
Set<String>tags = context.operationBuilder().build().getTags();
Set<String>translatedTags= new HashSet<>();
for(String tag:tags) {
if(environment.getProperty(tag)!=null) {
translatedTags.add(environment.getProperty(tag));
}else {
translatedTags.add(tag);
}
}
ModelReference modelReference= context.operationBuilder().build().getResponseModel();
AllowableListValues allowableValues=(AllowableListValues) modelReference.getAllowableValues();
if(allowableValues!=null && allowableValues.getValues()!=null) {
List<String> translatedAllowables=new ArrayList<>();
for(String value:allowableValues.getValues()) {
if(environment.getProperty(value)!=null) {
translatedAllowables.add(environment.getProperty(value));
}else {
translatedAllowables.add(value);
}
}
allowableValues.getValues().removeAll(allowableValues.getValues());
allowableValues.getValues().addAll(translatedAllowables);
}
//String summaryTranslated = apiDescriptionPropertiesReader.getProperty(summary);
//String notesTranslated = apiDescriptionPropertiesReader.getProperty(notes);
//context.operationBuilder().summary(summaryTranslated);
//context.operationBuilder().notes(notesTranslated);
context.operationBuilder().tags(translatedTags);
}

Swagger does not recognize WebAPI controller parameter with custom attribute [FromContent]

I want to have a custom attribute to parse data as stream and be testable with Swagger.
So I created controller which reads from POST body:
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public async Task<string> Post([FromContent]Stream contentStream)
{
using (StreamReader reader = new StreamReader(contentStream, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
return "OK";
}
How to define stream so it is visible in Swagger UI?
Here is my implementation of FromContent attribute and ContentParameterBinding binding:
public class ContentParameterBinding : HttpParameterBinding
{
private struct AsyncVoid{}
public ContentParameterBinding(HttpParameterDescriptor descriptor) : base(descriptor)
{
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,
HttpActionContext actionContext,
CancellationToken cancellationToken)
{
var binding = actionContext.ActionDescriptor.ActionBinding;
if (binding.ParameterBindings.Length > 1 ||
actionContext.Request.Method == HttpMethod.Get)
{
var taskSource = new TaskCompletionSource<AsyncVoid>();
taskSource.SetResult(default(AsyncVoid));
return taskSource.Task as Task;
}
var type = binding.ParameterBindings[0].Descriptor.ParameterType;
if (type == typeof(HttpContent))
{
SetValue(actionContext, actionContext.Request.Content);
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(actionContext.Request.Content);
return tcs.Task;
}
if (type == typeof(Stream))
{
return actionContext.Request.Content
.ReadAsStreamAsync()
.ContinueWith((task) =>
{
SetValue(actionContext, task.Result);
});
}
throw new InvalidOperationException("Only HttpContent and Stream are supported for [FromContent] parameters");
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public sealed class FromContentAttribute : ParameterBindingAttribute
{
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
{
if (parameter == null)
throw new ArgumentException("Invalid parameter");
return new ContentParameterBinding(parameter);
}
}
Update
When I create Stream using [FromBody] is shows correctly in Swagger, however Stream is not initiated and ==null
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public async Task<string> Post([FromBody]Stream contentStream)
{
using (StreamReader reader = new StreamReader(contentStream, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
return "OK";
}
So I want to have the same UI but with my custom attribute which let's me have Stream from content.
With my custom attribute it shows without TextArea for the parameter but could be tested using Postman and work correctly and Stream is available
Inherit your binding from FormatterParameterBinding class:
public class ContentParameterBinding : FormatterParameterBinding
{
public ContentParameterBinding(HttpParameterDescriptor descriptor)
: base(descriptor,
descriptor.Configuration.Formatters,
descriptor.Configuration.Services.GetBodyModelValidator())
{
}
//your code
}
Try implementing the interface IValueProviderParameterBinding:
public class ContentParameterBinding
: HttpParameterBinding, IValueProviderParameterBinding
{
public IEnumerable<ValueProviderFactory> ValueProviderFactories
{
get
{
return this.Descriptor.Configuration.Services.GetValueProviderFactories();
}
}
}
In my case it helped.
Also it's generally cleaner as it doesn't inherit FormatterParameterBinding logic, which may not be required.

Simple Injector: Implementation that depends on http request

I'm a beginner with Simple Injector and have a scenario where I need help to implement. I will try to simplify what I need to do.
I have a WebAPI where I need to authenticate users and based on the type of user choose an implementation.
Consider this structure
public interface ICarRepository {
void SaveCar(Car car);
}
//Some implementation for ICarRepository
public interface ICarLogic {
void CreateCar(Car car);
}
public class CarLogicStandard: ICarLogic {
private ICarRepository _carRepository;
public CarLogicStandard(ICarRepository carRepository) {
_carRepository = carRepository;
}
public void CreateCar(Car car) {
car.Color = "Blue";
_carRepository.SaveCar();
//Other stuff...
}
}
public class CarLogicPremium: ICarLogic {
private ICarRepository _carRepository;
public CarLogicPremium(ICarRepository carRepository) {
_carRepository = carRepository;
}
public void CreateCar(Car car) {
car.Color = "Red";
_carRepository.SaveCar();
//Other stuff 2...
}
}
And now I have a controller
public class CarController: ApiController {
private ICarLogic _carLogic;
public CarController(ICarLogic carLogic) {
_carLogic = carLogic;
}
public void Post(somePostData) {
//Identify the user based on post data
//....
Car car = somePostData.SomeCar();
_carLogic.CreateCar(car);
}
}
The code above will not work because in my request I need to identify the user. If it is a premium user the controller should use the CarLogicPremium and if it is a standard user the controller should use the CarLogicStandard.
I can configure the repository and others interfaces that don't need this logic on Global.asax however, since this case I need the request to decide which implementation should be used, I supose that I need to solve this in some other way.
There is a "Simple Injector" way to handle this? Or should I try another approach?
The simplest solution would be to configure the decision in the composition root, along with the rest of the container's configuration:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = new Container();
container.Register<CarLogicStandard>();
container.Register<CarLogicPremium>();
container.RegisterPerWebRequest<ICarRepository, CarRepository>();
container.Register<ICarLogic>(
() =>
HttpContext.Current != null &&
HttpContext.Current.User != null &&
HttpContext.Current.User.IsInRole("Premium")
? (ICarLogic)container.GetInstance<CarLogicPremium>()
: (ICarLogic)container.GetInstance<CarLogicStandard>()
);
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
You could also create an abstraction over the current user and decorate standard features with premium features
public class CarLogicPremium : ICarLogic
{
private readonly ICarLogic decorated;
private readonly ICurrentUser currentUser;
private readonly ICarRepository carRepository;
public CarLogicPremium(
ICarLogic decorated,
ICurrentUser currentUser,
ICarRepository carRepository)
{
this.decorated = decorated;
this.currentUser = currentUser;
this.carRepository = carRepository;
}
public void CreateCar(Car car)
{
if (currentUser.IsPremiumMember)
{
car.Color = "Red";
this.carRepository.SaveCar(car);
//Other stuff 2...
}
else
{
this.decorated.CreateCar(car);
}
}
}
which would be configured a bit like this
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = new Container();
container.Register<ICurrentUser, HttpCurrentUserProxy>();
container.RegisterPerWebRequest<ICarRepository, CarRepository>();
container.Register<ICarLogic, CarLogicStandard>();
container.RegisterDecorator(typeof(ICarLogic), typeof(CarLogicPremium));
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
But it really depends how many variations of services you will be creating over time. If you will be constantly adding new premium features you should look to implement a variation of the Try-X pattern. Let me know if one of the above works for you or if you need more info ...

How to make queryparams mandatory in Java Jersey REST services?

I have a REST API that accepts 3 query params. When the query is called without any one of the query parameters, the API executes and returns the result. How do we make the queryparams mandatory? How can I add validation to check if all the parameters are present? Also, please let me know the best approach.
On a very simple level you could just inject the HttpServletRequest and check yourself:
#GET
public Response example(#Context HttpServletRequest request,
#QueryParam("name") String name) {
if (null == request.getParameter("name")) {
ResponseBuilder builder = Response.status(404);
return builder.build();
}
// Do something with name
}
Or you can implement something more elaborate using AOP. Here's a blog post about further options.
jersey doesn't give a mandatory parameter checking functionality out of the box. however you can do something like implementing your own annotation to achieve it.
Below is the annotation code:
#Target(value = ElementType.METHOD)
#Retention(value = RetentionPolicy.RUNTIME)
public #interface Required {
String[] value();
}
You also need a filter, below is the code:
public class RequiredParamResourceFilterFactory implements ResourceFilterFactory {
#Context
private transient HttpServletRequest servletRequest;
private class RequiredParamFilter implements ResourceFilter, ContainerRequestFilter {
private final String[] requiredParams;
protected List<String> parametersValueMissing;
private RequiredParamFilter(String[] requiredParams) {
this.requiredParams = requiredParams;
}
#Override
public ContainerRequest filter(ContainerRequest containerRequest) {
boolean missingMandatoryParameter = false;
List<String> missingParameters = new ArrayList<String>();
List<String> requiredParametersValueMissing = new ArrayList<String>();
List<String> URLParameters = getURLParameters(containerRequest.getQueryParameters());
List<String> methodRequiredParameters = Arrays.asList(requiredParams);
if (methodRequiredParameters != null) {
for (String methodRequiredParam : methodRequiredParameters) {
if (URLParameters == null) {
missingMandatoryParameter = true; //we will check this flag before returning result set to caller
missingParameters.add(methodRequiredParam);
} else if (!URLParameters.contains(methodRequiredParam)) {
missingMandatoryParameter = true; //we will check this flag before returning result set to caller
missingParameters.add(methodRequiredParam);
//Add to required parameters value missing List, only if the parameter is mandatory and value is not provided
// in the URL
} else if (parametersValueMissing.contains(methodRequiredParam)) {
requiredParametersValueMissing.add(methodRequiredParam);
}
}
if (missingMandatoryParameter && requiredParametersValueMissing.size() > 0) {
throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters) +
"\nParameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));
} else if (missingMandatoryParameter) {
throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters), MisbarErrorCode.VALIDATION_WRONG_INPUT_ERROR, "Customers");
} else if (requiredParametersValueMissing != null &&
requiredParametersValueMissing.size() > 0) {
throw new YourCustomException("Parameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));
}
}
return containerRequest;
}
#Override
public ContainerRequestFilter getRequestFilter() {
return this;
}
#Override
public ContainerResponseFilter getResponseFilter() {
return null;
}
/**
* To fetch the parameters sent to webservice call, these will be used to find if required parameter
* are present or not
*
* #param queryParams the queryparams sent
* #return all the parameters sent in URL
*/
private List<String> getURLParameters(MultivaluedMap<String,String> queryParams) {
parametersValueMissing = new ArrayList<String>();
List<String> arr = new ArrayList<String>();
for(String key:queryParams.keySet())
{
arr.add(key);
if(queryParams.get(key)==null)
parametersValueMissing.add(key);
}
if(!arr.isEmpty())
return arr;
return null;
}
}
#Override
public List<ResourceFilter> create(AbstractMethod am) {
Required required = am.getAnnotation(Required.class);
if(required!=null)
{
return Collections.<ResourceFilter>singletonList(new RequiredParamFilter(required.value()));
}
return null;
}
}
Below sample shows how to use this annotation, so in below webservice; file_id and count are mandatory parameters:
#GET
#Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
#Cacheable(isCacheable = true)
#Path("posts/clusters")
#Required({"file_id","count"})
#Timed
public Response getClusters(
#QueryParam("file_id") Integer fileId,
#QueryParam("count") Integer count,
#DefaultValue("-1")#QueryParam("start_time") Long startTime){
;
}
If mandatory parameters are not provided in webservice call, you receive an error like below, mentioning the parameter names that are missing:
{
message: "Missing Parameters = file_id, count",
errorCode: "600"
}
Hope this solves your problem.

Resources