AEM MockitoJUnitRunner using powermockito for testing components using WCMUsePojo for aemcontext - junit4

i am referring the link https://www.exadel.com/news/aem-tip-junit-tests-for-wcmusepojo-objects/
I am working on adding the abstract
Below the code is not resolving in IDE
component = PowerMockito.mock(componentClass);
any inputs as what could be the reason and how to resolve it
import org.apache.poi.ss.formula.functions.T;
import org.powermock.api.mockito.PowerMockito;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
protected T component;
protected final Class componentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
component = PowerMockito.mock(componentClass);
========================================================
>>Please provide inputs

Below is the actual class i added and it showing errors in ide
import com.adobe.cq.sightly.SightlyWCMMode;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.poi.ss.formula.functions.T;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo;
import org.junit.Before;
import org.powermock.api.mockito.PowerMockito;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import static org.powermock.api.mockito.PowerMockito.when;
public abstract class AbstractComponenPowerJuTest {
protected T component;
protected ValueMap valueMap;
protected SlingScriptHelper slingScriptHelper;
protected SlingHttpServletRequest slingHttpServletRequest;
protected ResourceResolver resourceResolver;
protected SightlyWCMMode sightlyWCMMode;
protected Resource resource;
protected MockRequestPathInfo requestPathInfo;
private boolean firstRun = true;
protected final Class componentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
protected final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
protected void runOnceBeforeTests() {}
#Before
public void setUp() throws Exception {
runOnce();
component = PowerMockito.mock(componentClass);
resource = PowerMockito.mock(Resource.class);
resourceResolver = context.resourceResolver();
valueMap = new ValueMapDecorator(new HashMap<>());
slingScriptHelper = PowerMockito.mock(SlingScriptHelper.class);
slingHttpServletRequest = PowerMockito.mock(SlingHttpServletRequest.class);
sightlyWCMMode = PowerMockito.mock(SightlyWCMMode.class);
when(component.getResource()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(component.getResourceResolver()).thenReturn(resourceResolver);
when(component.getProperties()).thenReturn(valueMap);
when(component.getSlingScriptHelper()).thenReturn(slingScriptHelper);
when(component.getRequest()).thenReturn(slingHttpServletRequest);
when(component.getWcmMode()).thenReturn(sightlyWCMMode);
requestPathInfo = new MockRequestPathInfo();
when(slingHttpServletRequest.getRequestPathInfo()).thenReturn(requestPathInfo);
when(slingHttpServletRequest.getResourceResolver()).thenReturn(resourceResolver);
}
private void runOnce() {
if (firstRun) {
firstRun = false;
runOnceBeforeTests();
}
}
}
}

Related

how avoid to access to other data users. Spring boot + MongoDB

I'm developing a web application with Spring Boot and MongoDB. I'm following the MVC model.
I have a view which shows a list of stored data, but the app ignores the logged user and shows every objects.
https://i.stack.imgur.com/zl7TC.png
Here, the first row was added by another user, but it's showed anyway.
How could I get only the object allowed to the authenticated user?.
The only way I can see is checking the user Id after each query and get only the object of the given user. I think that there should be a better way to do this.
The code is the following:
Entity
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import org.springframework.data.mongodb.core.index.IndexDirection;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "trackings")
public class Tracking extends Entity implements Serializable {
private static final long serialVersionUID = -1249902722123443448L;
#Indexed(unique = true, direction = IndexDirection.DESCENDING)
private String trackingName;
private String SoftwareName;
#DBRef
private Set<Alarm> alarms;
public String getTrackingName() {
return trackingName;
}
public void setTrackingName(String trackingName) {
this.trackingName = trackingName;
}
public String getSoftwareName() {
return SoftwareName;
}
public void setSoftwareName(String softwareName) {
SoftwareName = softwareName;
}
public Collection<Alarm> getAlarms() {
return alarms;
}
public void setAlarms(Set<Alarm> alarms) {
this.alarms = alarms;
}
}
Repository
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import us.etsii.fvt.domains.Tracking;
#Repository
public interface TrackingRepository extends MongoRepository<Tracking, String>{
Tracking findByTrackingName(String name);
}
Controller
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import us.etsii.fvt.domains.Alarm;
import us.etsii.fvt.domains.Tracking;
import us.etsii.fvt.domains.User;
import us.etsii.fvt.services.TrackingService;
import us.etsii.fvt.services.UserService;
#Controller
public class TrackingController {
#Autowired
private UserService userService;
#Autowired
private TrackingService trackingService;
#RequestMapping(value = { "/tracking" }, method = RequestMethod.GET)
public ModelAndView tracking() {
ModelAndView modelAndView = new ModelAndView();
// AƱadimos el usuario al modelo
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
modelAndView.addObject("currentUser", user);
modelAndView.addObject("fullName", user.getFullname());
// AƱadimos la lista de trackings al modelo
List<Tracking> trackings = trackingService.findAll();
modelAndView.addObject("trackings", trackings);
// Devolvemos el modelo
modelAndView.setViewName("tracking");
return modelAndView;
}
...
}
Service
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.etsii.fvt.domains.Tracking;
import us.etsii.fvt.repositories.TrackingRepository;
#Service
public class TrackingService {
#Autowired
private TrackingRepository trackingRepository;
public Tracking findTrackingByName(String name) {
return trackingRepository.findByTrackingName(name);
}
public void saveTracking(Tracking tracking) {
trackingRepository.save(tracking);
}
public List<Tracking> findAll() {
return trackingRepository.findAll();
}
public Tracking findById(String id) {
Optional<Tracking> t = trackingRepository.findById(id);
if(!t.isPresent()) {
return null;
}
return t.get();
}
public void remove(String id) {
trackingRepository.deleteById(id);
}
}
There are ambiguous points in your question but assuming you have an User Entity:
You should get the logged in user from Spring Security
You should include a relation of User Entity to Tracking Entity (include User ID information in Tracking records)
Then you should query the mongo db Tracking Entity with the given user id.
You can use standard query by field functionality of Spring Repository.

HikariCP for MongoDB in springboot

I am looking to create a connection pool - for mongoDB in Springboot. I am currently making use of Springdata Mongo repositories to connect to DB and collections but unsure of how to create the datapool connection
Here is the current implementation
PersonRepository.java
package com.test.TestAPI.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.test.TestAPI.dto.Person;
#Repository
public interface PersonRepository extends MongoRepository<Person, String> {
}
PersonService
package com.test.TestAPI.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.TestAPI.repository.PersonRepository;
import com.test.TestAPI.dto.Person;
#Service
public class PersonService {
#Autowired
private PersonRepository personRepo;
public List<Person> findAllPersons() {
return personRepo.findAll();
}
public Person createPerson(Person person) {
return personRepo.save(person);
}
}
PersonController
package com.test.TestAPI.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.TestAPI.service.impl.PersonService;
import com.test.TestAPI.dto.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
#RestController
#RequestMapping(path="/v1/personController")
#Api(value="Controller for Person document")
public class PersonController {
#Autowired
PersonService service;
#GetMapping("/getAllPersons")
#ApiOperation(produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = List.class,
value = "getAllPersons from the database", notes = "Sample note")
public ResponseEntity<List<Person>> getAllPersons(){
List<Person> personList = service.findAllPersons();
return new ResponseEntity<List<Person>>(personList, HttpStatus.OK);
}
}
SimpleCommandLineConfig
package com.test.TestAPI.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Component;
import com.test.TestAPI.repository.PersonRepository;
import com.test.TestAPI.dto.Person;
#Component
#Order(3)
public class SimpleCommandLineConfig implements CommandLineRunner {
#Autowired
PersonRepository repo;
#Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
System.out.println("third command line runner");
System.out.println(repo.save(new Person("rr","rr",4)));
}
}
App.java
package com.test.TestAPI.main;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
* Hello world!
*
*/
#SpringBootApplication
#ComponentScan(basePackages= {"com.test.TestAPI"})
#EnableMongoRepositories(basePackages= {"com.test.TestAPI.repository"})
public class App
{
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
Also, I would like to know if spring data repos like a custom repo for MongoDB takes care of connection pool mechanism? How does connection pooling happen in that case? Could you please help me on this
I think I found an answer for this. Just like we use JDBC templates for RDBMS to store the databases, spring provides something called MongoTemplates which forms a connection pool based on the db configuration given
Here is a sample implementation
MongoClientFactory.java
public #Bean MongoClientFactoryBean mongo() throws Exception {
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
mongo.setHost("localhost");
MongoClientOptions clientOptions = MongoClientOptions.builder().applicationName("FeddBackAPI_DB")
.connectionsPerHost(2000)
.connectTimeout(4000)
//.maxConnectionIdleTime(1000000000)
.maxWaitTime(3000)
.retryWrites(true)
.socketTimeout(4000)
.sslInvalidHostNameAllowed(true)//this is very risky
.build();
mongo.setMongoClientOptions(clientOptions);
return mongo;
}
DataSourceConfig.java (Another config class. Same config class can also be used to define all beans)
package com.fmr.FeedBackAPI.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
#Configuration
#Import(value=MongoClientFactory.class)
public class DataSourceConfig {
#Autowired
Mongo mongo;
#Autowired
Environment env;
#Bean
public String test() {
System.out.println("mongo"+mongo);
return "rer";
}
private MongoTemplate mongoTemplate() {
MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test");
MongoTemplate template = new MongoTemplate(factory);
return template;
}
#Bean
#Qualifier(value="customMongoOps")
public MongoOperations mongoOps() {
MongoOperations ops = mongoTemplate();
return ops;
}
#Bean
public MongoDbFactory factory() {
MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test");
return factory;
}
// #Bean
// public GridFsTemplate gridFsTemplate() {
// return new GridFsTemplate(mongo, converter)
// }
#Bean
public javax.sql.DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(100);
// ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
// ds.setJdbcUrl(env.getProperty("spring.datasource.url"));;
//ds.setUsername(env.getProperty("spring.datasource.username"));
//ds.setPassword(env.getProperty("spring.datasource.password"));
ds.addDataSourceProperty("cachePrepStmts", true);
ds.addDataSourceProperty("prepStmtCacheSize", 250);
ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
ds.addDataSourceProperty("useServerPrepStmts", true);
return ds;
}
}
Now autowire the template/mongoOperations in you dao implementation or service and use it
package com.fmr.FeedBackAPI.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import com.fmr.FeedBackAPI.dto.ConfigDTO;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
#Service
public class PlanConfigService {
#Autowired
#Qualifier(value="customMongoOps")
MongoOperations mongoOps;
public List<ConfigDTO> createConfigDTOList(List<ConfigDTO> configDTOList) {
List<ConfigDTO> configList = new ArrayList<>();
if(!mongoOps.collectionExists(ConfigDTO.class)) {
mongoOps.createCollection("ConfigDTO_table");
}
//create the configDTOList
mongoOps.insert(configDTOList, ConfigDTO.class);
configList = mongoOps.findAll(ConfigDTO.class, "ConfigDTO_table");
return configList;
}
public List<ConfigDTO> createConfigDTO(ConfigDTO configDTO) {
List<ConfigDTO> configList = new ArrayList<>();
if(!mongoOps.collectionExists(ConfigDTO.class)) {
mongoOps.createCollection("ConfigDTO_table");
}
//create the configDTOList
mongoOps.save(configDTO);
configList = mongoOps.find(query(where("planId").is(configDTO.getPlanId())), ConfigDTO.class,"ConfigDTO_table");
return configList;
}
}
Here is the application.properties (this is the default instance running in local)
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mongo_test
spring.data.mongodb.repositories=true
#
spring.datasource.url=jdbc:mongodb://localhost:27017/mongo_test

Supporting Multiple data sources of Mongo using Spring data

I am trying to configure two different mongo data sources using spring data.According to my requirement I need to change my scanning of base package dynamically at run time. The user can select the data source for a particular entity via property file by providing datasource and entity mapping.
Eg: entity1:dataSource1, entity2:dataSource2. Depending on this mapping I need to package names and need to replace in the #EnableMongoRepositories(basePackages="xxx.xxx.xxxx") at run time. I tired a lot using both Xml configuration and java configuration.But I could not find a possible way.Could some one please provide a solution for the problem if it is there.I am pasting the Entity classes ,Repositories and Configuration both XML and Java Config
package com.account.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "SampleAccount")
public class Account {
#Id
private String acntName;
#Indexed
private Date startDate;
#Indexed
private String accntType;
private Long acntId;
private Byte byteField;
public String getAcntName() {
return this.acntName;
}
public void setAcntName(String acntName) {
this.acntName = acntName;
}
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getAccntType() {
return this.accntType;
}
public void setAccntType(String accntType) {
this.accntType = accntType;
}
public Long getAcntId() {
return this.acntId;
}
public void setAcntId(Long acntId) {
this.acntId = acntId;
}
public Byte getByteField() {
return this.byteField;
}
public void setByteField(Byte byteField) {
this.byteField = byteField;
}
}
public interface AccountRepository extends MongoRepository<Account, String>{
public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);
public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);
public Account findByAcntName(String acntName);
public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);
}
package com.user.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "SampleUser")
public class User {
#Id
private String acntName;
#Indexed
private Date startDate;
#Indexed
private String accntType;
private Long acntId;
private Byte byteField;
public String getAcntName() {
return this.acntName;
}
public void setAcntName(String acntName) {
this.acntName = acntName;
}
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getAccntType() {
return this.accntType;
}
public void setAccntType(String accntType) {
this.accntType = accntType;
}
public Long getAcntId() {
return this.acntId;
}
public void setAcntId(Long acntId) {
this.acntId = acntId;
}
public Byte getByteField() {
return this.byteField;
}
public void setByteField(Byte byteField) {
this.byteField = byteField;
}
}
package com.user.repo;
import java.util.Date;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import com.account.entity.Account;
import com.user.entity.User;
public interface UserRepository extends MongoRepository<User, String>{
public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);
public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);
public Account findByAcntName(String acntName);
public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);
}
JavaConfig Files:
MongoConfig.class
package com.Config;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
#Configuration
#EnableMongoRepositories(basePackages = {"xxx.xxx.xxx"},mongoTemplateRef="primaryTemplate")
public class MongoConfig {
#Resource
Environment environment;
public #Bean(name = "primarydb")
MongoDbFactory mongoDbFactory() throws Exception {
MongoCredential credential = MongoCredential.createCredential(
(environment.getProperty("xxx.datastore.mongo.server.userName")),
environment.getProperty("xxx.datastore.mongo.server.database"),
environment.getProperty("xxx.datastore.mongo.server.password").toCharArray());
List<MongoCredential> mongoCredentials = new ArrayList<>();
mongoCredentials.add(credential);
MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(
Integer.parseInt(environment.getProperty("xxx.datastore.mongo.server.connectionsPerHost")))
.build();
List<ServerAddress> serverAddress = new ArrayList<>();
prepareServerAddress(serverAddress);
MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentials, options);
return new SimpleMongoDbFactory(mongoClient,
environment.getProperty("xxx.datastore.mongo.server.database"));
}
private void prepareServerAddress(List<ServerAddress> serverAddress) throws UnknownHostException {
String serverAddressList[] = environment.getProperty("XDM.datastore.mongo.server.address")
.split("\\s*,\\s*");
for (String svrAddress : serverAddressList) {
String address[] = svrAddress.split("\\s*:\\s*");
String host = Objects.nonNull(address[0]) ? address[0] : "127.0.0.1";
Integer port = Objects.nonNull(address[1]) ? Integer.valueOf(address[1]) : 27017;
serverAddress.add(new ServerAddress(host, port));
}
}
#Primary
public #Bean(name = "primaryTemplate")
MongoTemplate mongoTemplate(#Qualifier(value = "primarydb") MongoDbFactory factory) throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(factory);
return mongoTemplate;
}
}

Why #Autowired not working all the time?

Im trying to setup mongodb with SpringBoot and im not using context.xml but trying to configure with configuration class and annotations.
package com.configuration;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
#Configuration
#EnableMongoRepositories(basePackages = "com.mongo")
public class MongoConfig extends AbstractMongoConfiguration {
#Override
protected String getDatabaseName() {
return "mongodbname";
}
#Override
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1", 27017);
}
#Override
protected String getMappingBasePackage() {
return "com.mongo";
}
}
My repository looks like this :
package com.mongo.repositories;
import com.mongo.documents.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository("sequenceRepository")
public class SequenceRepository{
#Autowired
private MongoTemplate mongoTemplate;
public Sequence findOne(Query query){
return this.mongoTemplate.findOne(query,Sequence.class);
}
public List<Sequence> find(Query query){
return this.mongoTemplate.find(query,Sequence.class);
}
public void save(Sequence object){
this.mongoTemplate.save(object);
}
public void delete(Sequence object){
this.mongoTemplate.remove(object);
}
}
If i use like this in the rest controller it is working properly :
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequenceRepository.save(sequence);
return "index";
}
}
But if i want to use the SequenceRepository inside the Sequence document like this :
package com.mongo.documents;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
public class Sequence {
#Autowired
private SequenceRepository sequenceRepository;
#Id
private String id;
private String className;
private int actual;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getActual() {
return actual;
}
public void setActual(int actual) {
this.actual = actual;
}
public void save(){
this.sequenceRepository.save(this);
}
public void delete(){
this.sequenceRepository.delete(this);
}
}
After that I change the code in the controller to :
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequence.save();
return "index";
}
}
I got a nullPointerExceeption at this point in the save() method.
There is no need to inject your Repository inside your Domain object Sequence as it is really bad design to have a dependency between the domain objects and your repository classes.
To follow some good practices, your Service classes or if you don't need a Service layer, your Controller classes should inject your Repository beans.
In your last code snippet your are already doing it, but the .save() method should be called on your repository SequenceRepository and not on the domain object Sequence.
An example could be the following:
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
// now let's save it
sequenceRepository.save(sequence);
return "index";
}
}
You can only autowire Spring-managed components into other Spring-managed components. Your sequence object is not a component (not annotated with #Component, #Service, #Repository or #Controller etc.)
I suggest you follow rieckpil's advice.

Why I receive error when use Cover As in Eclipse and not with Junit?

I use Eclipse with eCobertura
I have a little project with a Controller (SpringMVC).
I created a test (JUnit).
When I run the test from JUnit (in Eclipse IDE) all is right but when I run the command (from menu) I receive an error
My controller :
package ec.europa.eu.nwi.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* #author LEBRUJA
*/
#Controller
public class AvailibilityController {
/**
* #param request
* #return mav
*/
#RequestMapping(value = "/available")
public final ModelAndView available(final HttpServletRequest request) {
final ModelAndView mav = new ModelAndView("available", "sample",
new String("availability on 0.0.1"));
return mav;
}
}
My Test :
package ec.europa.eu.nwi.web.controller.test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import ec.europa.eu.nwi.web.controller.AvailibilityController;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring-servlet.xml"})
public final class AvalibilityControllerTest {
private transient MockHttpServletRequest request;
private transient MockHttpServletResponse response;
#Autowired
private RequestMappingHandlerAdapter handlerAdapter;
#Autowired
private RequestMappingHandlerMapping handlerMapping;
private static final Logger LOGGER = LoggerFactory.getLogger(AvalibilityControllerTest.class);
#Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
#After
public void tearDown() throws Exception {
LOGGER.debug("TearDown");
}
#Test
public void testAvailable() {
LOGGER.debug("Start testAvailable1");
LOGGER.debug("Test only availibility of the apps");
final AvailibilityController avc = new AvailibilityController();
final Object mav = avc.available(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(mav instanceof ModelAndView);
ModelAndViewAssert.assertAndReturnModelAttributeOfType((ModelAndView)mav, "sample", String.class);
ModelAndViewAssert.assertModelAttributeAvailable((ModelAndView)mav, "sample");
ModelAndViewAssert.assertModelAttributeValue((ModelAndView)mav, "sample", "availability on 0.0.1");
ModelAndViewAssert.assertViewName((ModelAndView)mav, "available");
final BindingResult result = mock(BindingResult.class);
when(result.hasErrors()).thenReturn(true);
LOGGER.debug("End testAvailable1");
}
#Test
public void testAvailable1() throws Exception {
LOGGER.debug("Start testAvailable");
LOGGER.debug("Test only availibility of the apps");
request.setMethod("GET");
request.setRequestURI("/available.html");
Object handler = handlerMapping.getHandler(request).getHandler();
LOGGER.debug("Get the Model and View");
ModelAndView modelAndView = handlerAdapter.handle(request, response,handler);
Assert.assertEquals("availability on 0.0.1", modelAndView.getModel().get("sample"));
Assert.assertTrue(modelAndView.getModel().containsKey("sample"));
LOGGER.debug("End testAvailable");
}
}
If I run with JUnit (Run As Junit), all is right but when I run Cover As .. JUnit I receive the error.
The error :
I filtered the class (from exclude configuration in Eclipse Coverage Configuration).
If I removed the filter, the junit code is marked in red
I don't understand the error.
Thanks a lot
I had
<aop:aspectj-autoproxy proxy-target-class="true" />
In my applicationContext.xml and it is running fine now
Thanks a lot