I am using jackson ObjectReader and ObjectWriter object for serialization and deserialization in Akka Rest Server.
I am getting byteString in the request and deserialize it to object. Below is the scala code for it.
val a = objectReader.readValue[java.util.List[Base[Long]]](request.toArray)
Base class is an abstract class and I can have multiple implementation of it
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
property = "impl")
#JsonSubTypes({
#JsonSubTypes.Type(value = A.class, name = "A")
})
public abstract class Base<T> implements Serializable {
private String impl;
private ResponseStatus status;
public String getImpl() {
return impl;
}
public void setImpl(String impl) {
this.impl = impl;
}
public void setStatus(ResponseStatus status) {
this.status = status;
}
public ResponseStatus getStatus() {
return status;
}
public static class ResponseStatus implements Serializable {
private ReturnCode code;
private String msg;
public void setCode(ReturnCode code) {
this.code = code;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ReturnCode getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
}
How ever I have observed that first call for readValue and writeValueAsBytes takes long.
I tried initializing it. But still it is not improving in Akka Execution Context. Anyone know the solution of it? Please help.
Related
I'm using a custom itemReader to read data from an external rest API, and it's working great. However, the problem arises when processing the data with ItemProcessor into my model class. Unfortunately, the API response is an object with an array nested inside of it, which means I have to use a list referencing another class to store the data inside of it.
picture of API response
StockDTO data class:
public class StockDTO {
// We will change the field data types later when we process the data into our model.
private String from;
private String to;
private List<ProductDTO> products;
// Getters and Setters allow RestTemplate to set the data from the external rest API.
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public List<ProductDTO> getProducts() {
return products;
}
public void setProducts(List<ProductDTO> products) {
this.products = products;
}
}
ProductDTO data class (list data):
public class ProductDTO {
// We will change the field data types later when we process the data into our model.
private String sku;
private String name;
private String startDate;
// Getters and Setters allow RestTemplate to set the data from the external rest API.
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
}
model class:
#Entity
public class Stock {
#Id
private long id;
private int item_from;
private int item_to;
private long sku;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getItem_from() {
return item_from;
}
public void setItem_from(int item_from) {
this.item_from = item_from;
}
public int getItem_to() {
return item_to;
}
public void setItem_to(int item_to) {
this.item_to = item_to;
}
public long getSku() {
return sku;
}
public void setSku(long sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ItemProcessor<StockDTO, Stock>
public class StockDataProcessor implements ItemProcessor<StockDTO, Stock> {
#Override
public Stock process(StockDTO stockDTO) throws Exception {
Stock stock = new Stock();
stock.setItem_from(Integer.parseInt(stockDTO.getFrom()));
stock.setItem_to(Integer.parseInt(stockDTO.getTo()));
// I'm only able to get the first index sku, name from the list:
stock.setSku(Long.parseLong(stockDTO.getProducts().get(0).getSku()));
stock.setName(stockDTO.getProducts().get(0).getName());
return stock;
}
}
Should I create another model class called Product so that I can create ItemProcessor<ProductDTO, Product> or can I get all the list items from ItemProcessor<StockDTO, Stock> without creating another processor? Thank you.
You could create another model class called Product and modify the Stock class to include a List and modify your ItemProcessor to populate the list of Product in the Stock class.
Alternatively, you could make your ItemProcessor return a List instead of just a single Stock. Your ItemWriter would then need to process a List<List>.
In my work i have a lot of discrepancy of opinions about UMLing class diagram of this sample factory:
class Program
{
private static TrainComponentsData _trainComponentsData;
static void Main(string[] args)
{
InitTrainComponentsData();
string str = _trainComponentsData.ToString();
}
private static void InitTrainComponentsData()
{
ITrainComponentsReader reader = TrainComponentsReaderFactory.Create("XML");
_trainComponentsData = reader.Read();
}
}
public class TrainComponentsReaderFactory
{
internal static ITrainComponentsReader Create(string readerType)
{
//....
}
}
internal class XMLTrainComponentsReader : ITrainComponentsReader
{
//....
}
public class TrainComponentsData
{
//....
}
internal interface ITrainComponentsReader
{
TrainComponentsData Read();
}
i just a little confused about various of difference diagrams:
etc...
thanks!
I am trying to publish a webservice with spring-boot
Here is how I have set it up.
I have an interface that has some methods say
#WebService
public interface FirstInterface
{
#WebMethod
void method1(#WebParam(name = "id") String id);
void method2(#WebParam(name = "id2") String id);
}
I have another interface that has some more methods
and it extends FirstInterface
#WebService
public interface SecondInterface extends FirstInterface
{
#WebMethod
void method3(#WebParam(name = "id") String id);
void method4(#WebParam(name = "id2") String id);
}
Now I have an implementation class that implements SecondInterface
and has an endpointInterface referring to my SecondInterface something like this:
#Service
#WebService(endpointInterface = "com.somepackage.SecondInterface")
public class CallBackServicesImpl implements SecondInterface
{
#Override
//override all four methods here
}
Now I have a configuration class that is publishing these services
#Configuration
public class WebServiceConfig
{
#Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), serviceImpl());
endpoint.publish(SERVICE_NAME_PATH);
return endpoint;
}
}
Problem: The webservice gets published with this setup with endpointinterface pointing to FirstInterface but only two methods will be available for use.
Now I all want four methods to be available for the client to use, so I point endpointinterface to SecodInterface and it start throwing exception saying Error creating bean with name 'endpoint',org.apache.cxf.service.factory.ServiceConstructionException
Am I missing something basic here? How can I achieve this behavior?
Remove the #WebService annotation on impl class
Here is the working copy
FirstInterface.java
#WebService
public interface FirstInterface {
#WebMethod
void method1(#WebParam(name = "id") String id);
#WebMethod
void method2(#WebParam(name = "id2") String id);
}
SecondInterface.java
#WebService
public interface SecondInterface extends FirstInterface {
#WebMethod
void method3(#WebParam(name = "id") String id);
#WebMethod
void method4(#WebParam(name = "id2") String id);
}
Impl class
#Service
public class KPImpl implements SecondInterface {
public static final Logger LOG = LoggerFactory.getLogger(KPImpl.class);
#Override
public void method3(String id) {
LOG.info("Method3 {}", id);
}
#Override
public void method4(String id) {
LOG.info("Method4 {}", id);
}
#Override
public void method1(String id) {
LOG.info("Method1 {}", id);
}
#Override
public void method2(String id) {
LOG.info("Method2 {}", id);
}
}
Finally configuration file
#Configuration
public class WsConfiguration {
#Bean
public Server getJaxWsServer(SpringBus bus, KPImpl impl) {
final JaxWsServerFactoryBean serverFctry = new JaxWsServerFactoryBean();
serverFctry.setAddress("/kp");
serverFctry.setServiceBean(impl);
serverFctry.setServiceClass(KPImpl.class);
serverFctry.getFeatures().add(new LoggingFeature());
return serverFctry.create();
}
}
Here is part of code implementation in parent class:
handler.FooUpdateDelegate += FooUpdate(OnFooUpdate);
protected abstract void OnFooUpdate(ref IBoo boo, string s);
I have in test method mocked handler:
Mock<IHandler> mHandler = mockFactory.Create<IHandler>();
This...
mHandler.Raise(x => x.FooUpdateDelegate += null, boo, s);
...is not working. It says:
System.ArgumentException : Could not locate event for attach or detach method Void set_FooUpdateDelegate(FooUpdate).
I want to raise OnFooUpdate so it triggers the code to be tested in child class.
Question: How can I raise delegate (not common event handler) with Moq?
If I missed the point completely, please enligten me.
It looks like you are trying to raise a delegate rather than an event. Is this so?
Is your code along the lines of this?
public delegate void FooUpdateDelegate(ref int first, string second);
public class MyClass {
public FooUpdateDelegate FooUpdateDelegate { get; set; }
}
public class MyWrapperClass {
public MyWrapperClass(MyClass myclass) {
myclass.FooUpdateDelegate += HandleFooUpdate;
}
public string Output { get; private set; }
private void HandleFooUpdate(ref int i, string s) {
Output = s;
}
}
If so, then you can directly invoke the myClass FooUpdateDelegate like so
[TestMethod]
public void MockingNonStandardDelegate() {
var mockMyClass = new Mock<MyClass>();
var wrapper = new MyWrapperClass(mockMyClass.Object);
int z = 19;
mockMyClass.Object.FooUpdateDelegate(ref z, "ABC");
Assert.AreEqual("ABC", wrapper.Output);
}
EDIT: Adding version using interface
public interface IMyClass
{
FooUpdateDelegate FooUpdateDelegate { get; set; }
}
public class MyClass : IMyClass {
public FooUpdateDelegate FooUpdateDelegate { get; set; }
}
public class MyWrapperClass {
public MyWrapperClass(IMyClass myclass) {
myclass.FooUpdateDelegate += HandleFooUpdate;
}
public string Output { get; private set; }
private void HandleFooUpdate(ref int i, string s) {
Output = s;
}
}
[TestMethod]
public void MockingNonStandardDelegate()
{
var mockMyClass = new Mock<IMyClass>();
// Test fails with a Null Reference exception if we do not set up
// the delegate property.
// Can also use
// mockMyClass.SetupProperty(m => m.FooUpdateDelegate);
mockMyClass.SetupAllProperties();
var wrapper = new MyWrapperClass(mockMyClass.Object);
int z = 19;
mockMyClass.Object.FooUpdateDelegate(ref z, "ABC");
Assert.AreEqual("ABC", wrapper.Output);
}
[using Moq]
I am trying to mock a concrete class and mock a virtual method "Get()" of that class. When testing a method "GetItemsNotNull()" I always get returned null, instead of the return of the mocked function.
Here is the code
//SomeClasses.cs
namespace MoQExamples
{
public abstract class Entity
{
}
public class Abc : Entity
{
}
public interface IRepository<T> where T : Entity
{
IQueryable<T> Get();
}
public class Repository<T> : IRepository<T> where T : Entity
{
private readonly ISession _session;
public Repository()
{
_session = null;
}
public Repository(ISession session)
{
_session = session;
}
protected ISession CurrentSession
{
get { return _session; }
}
public virtual IQueryable<T> Get()
{
return CurrentSession.Query<T>();
}
}
public interface IAbcRepository
{
Abc GetItemsNotNull();
}
public class AbcRepository : Repository<Abc>, IAbcRepository
{
public Abc GetItemsNotNull()
{
return Get().FirstOrDefault(abc => abc !=null);
}
}
}
and here are the test class
namespace MoQExamples
{
[TestFixture]
public class SomeClassesTest
{
private readonly Mock<AbcRepository> _abcRepositoryMock = new Mock<AbcRepository>(MockBehavior.Strict) { CallBase = true };
[SetUp]
public void SetupTest()
{
_abcRepositoryMock.Setup(x => x.Get()).Returns(Get);
}
public IQueryable<Abc> Get()
{
return (new List<Abc>() { new Abc() }) as IQueryable<Abc>;
}
[Test]
public void TestGetItemsNotNull()
{
Assert.IsNotNull(_abcRepositoryMock.Object.GetItemsNotNull());
}
}
}
the assert alays fails..instead of returning the SomeClassesTest.Get()
thanks for advance guys!
I suspect this is the problem:
return (new List<Abc>() { new Abc() }) as IQueryable<Abc>;
List<T> doesn't implement IQueryable<T>, so this will always return null. Call AsQueryable to convert it instead:
return new List<Abc>().AsQueryable();
As an aside, this is a reason to prefer casts over as in most situations: if you'd just cast to IQueryable<Abc>, you'd have received an exception at the line which was really causing the problem. You should only use as when it's not a bug for the conversion to "fail". An as operator should almost always be followed by a nullity test.
(Note that this behaviour in itself has nothing to do with mocking or Moq. It's just the behaviour of the as operator...)