I'm trying to solve a problem: When user click on delete a record and instead of delete it, this record will change status to 'false'.
I suppose to create stored procedures in MySQL like "getAllUser", "changeStatus" but I'm confusing what I have to do.
This is structure of my project:
-configuration
JpaConfiguration
-controller
RestApiController
-model
User
-Repository
UserRepository
-Service
UserService
UserServiceImpl
Then I expect the index page only get records that have status is 'true'.
Procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllUsers`()
BEGIN
SELECT * FROM APP_USER where status = 0;
END
RestApiController
#RestController
#RequestMapping("/api")
public class RestApiController {
#Autowired
UserService userService;
#RequestMapping(value = "/user/", method = RequestMethod.GET)
public List<User> getAllUsers(Boolean status){
Boolean statusWhere = org.apache.commons.lang3.BooleanUtils.isNotTrue(false);
List<User> users = userServiceImpl.getAllUsers(statusWhere);
if (users == null){
return new ArrayList<>();
} else {
return users;
}
}
// Change status
#RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?>deleteUserById(#PathVariable("id") long id){
logger.info("Delete User with id {}", id);
User currentUser = userService.findById(id);
currentUser.setStatus(true);
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}
model/User
#Entity
#NamedStoredProcedureQueries({
#NamedStoredProcedureQuery(
name = "getAllUsers",
procedureName = "getAllUsers",
resultClasses = {User.class},
parameters = {
#StoredProcedureParameter(
mode = ParameterMode.IN,
name = "status",
type = Boolean.class)
}
)
})
service/UserServiceImpl
#Component
#Service("userService")
#Transactional
public class UserServiceImpl implements UserService {
#Autowired
private EntityManager entityManager;
#SuppressWarnings("unchecked")
public List<User> getAllUsers(Boolean status){
StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("getAllUsers");
storedProcedureQuery.setParameter("status",status);
storedProcedureQuery.execute();
return storedProcedureQuery.getResultList();
}
public boolean isUserExist(User user) {
return findByName(user.getName()) != null;
}
}
Related
I'm trying to compare two String values, which on the console are identical, but the returned boolean is always false.
I'm talking about the login() method. I am using PostgreSQL.
This is my Service file:
#Service
public class UserService {
private UserRepository userRepository;
#Autowired
public UserService(UserRepository userRepository) {
this.userRepository=userRepository;
}
public List<Useraccount> getUsers() {
List<Useraccount> userList = new ArrayList<>();
userRepository.findAll().forEach(userList::add);
return userList;
}
public boolean login(String username, String password) {
Useraccount user = userRepository.findByUsername(username).orElseThrow(()-> new IllegalStateException("User with Username "+username+" not found"));
System.out.println(user.getUsername()+user.getPassword()+"out");
System.out.println(username+password+"in");
return (user.getUsername()==username);
}
public String userOutput(String username) {
Useraccount user = userRepository.findByUsername(username).orElseThrow(()-> new IllegalStateException("User with Username "+username+" not found"));
return user.getUsername();
}
}
This is my Repository file:
#Repository
public interface UserRepository extends CrudRepository<Useraccount, Long>{
Optional<Useraccount> findByUsername(String username);
}
This is my Controller file:
#RestController
#RequestMapping("/api/v1/user")
#CrossOrigin
public class UserController {
private UserService userService;
#Autowired
public UserController(UserService userService) {
this.userService=userService;
}
#GetMapping
private List<Useraccount> getUsers(){
return userService.getUsers();
}
#GetMapping("/login")
public boolean login(#RequestParam(required = true) String username, #RequestParam(required =
true) String password) {
return userService.login(username, password);
}
#GetMapping(path="{username}")
public String userOutput(#PathVariable("username") String username) {
return userService.userOutput(username);
}
}
This is my Console output:
Hibernate:
select
useraccoun0_.id as id1_1_,
useraccoun0_.password as password2_1_,
useraccoun0_.username as username3_1_
from
useraccount useraccoun0_
where
useraccoun0_.username=?
DeonisosPasswordout
DeonisosPasswordin
As you can see the in and out is identical, but the boolean always returns false for some reason.
Please use equals method comparison on strings if you re trying to compare the content. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects. So, your login method should return below for accurate results.
return (user.getUsername().equals(username);
I need to use #Service from another Spring project. I will need a REST call to invoke it but how am I suppose to do it?
This is the service:
#Service
public class LocationsService implements ILocationsService {
private final Logger logger = LoggerFactory.getLogger("LocationsService");
private final ILocationRepository locationRepository;
private final IEvseRepository evseRepository;
private final IConnectorRepository connectorRepository;
#PersistenceContext
private EntityManager entityManager;
#Autowired
public LocationsService(ILocationRepository locationRepository, IEvseRepository evseRepository, IConnectorRepository connectorRepository, EntityManager entityManager) {
this.locationRepository = locationRepository;
this.evseRepository = evseRepository;
this.connectorRepository = connectorRepository;
this.entityManager = entityManager;
}
public Location getLocation(String countryCode, String partyId, String id) {
return locationRepository.findByCountryCodeAndPartyIdAndId(countryCode, partyId, id);
}
public Location deleteLocation(String countryCode, String partyId, String id) {
Location location = locationRepository.findByCountryCodeAndPartyIdAndId(countryCode, partyId, id);
if (location == null) {
logger.info("Location does not exist.");
return null;
}
locationRepository.deleteById(location.getLocId());
return location;
}
I need to call the service in this controller. The controller is in a different project:
#RestController
#RequestMapping(value = "/locations", produces = MediaType.APPLICATION_JSON_VALUE)
#Api(tags = "Locations management")
public class LocationController {
#Autowired
private LocationsService locationsService;
#RequestMapping(method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE)
#ApiOperation(value = "Get Locations", notes = "Get locations", nickname = "getLocations",
authorizations = #Authorization(value = "Bearer"))
public ResponseEntity<List<LocationDto>> getLocations() {
List<LocationDto> locations = new ArrayList<>();
return new ResponseEntity<>(locations, HttpStatus.OK);
}
}
I searched for solutions but found nothing helpful and will appreciate any help.
Thank you!
I just have started with Spring Boot and I am practicing in base to this example:
http://www.devglan.com/spring-boot/spring-boot-angular-example
The physical deleted is working ok, but I am trying to do a logical deleted and I do not know how to do it.
This is my classes:
UserController
#DeleteMapping(path ={"logical/{id}"})
public User deleteLogical(#PathVariable("id") int id, #RequestBody User user) {
return userService.deleteLogical(id, user);
}
UserService
User deleteLogical(int id, User user);
UserServiceImpl
#Override
// #Query("update user set remove = false where id = ?1")
public User deleteLogical(int id, User user) {
// user.setRemove(false);
return repository.save(user);
}
UserRepository
User save(User user);
This is the SQL statement I want to execute:
UPDATE user SET remove = false WHERE id =?;
How I could do it? Thanks,
I think you dont need to pass User object. Id will be enough.
So your code will be changed
#DeleteMapping(path ={"logical/{id}"})
public User deleteLogical(#PathVariable("id") int id) {
return userService.deleteLogical(id, user);
}
UserService
User deleteLogical(int id);
UserServiceImpl
#Override
public User deleteLogical(int id) {
User user = repository.findById(id);
user.setRemove(true);
return repository.save(user);
}
That's all.
You try to implement "soft delete" so you can try this approach:
User entity:
#Entity
User {
#Id
#GeneratedValue
private Integer id;
//...
private boolean removed; // user is removed if this property is true
//...
}
User service:
public interface UserService {
Optional<Integer> softDelete(int id);
}
#Service
public UserServiceImpl implements UserService (
// Injecting UserRepo
// First variant - read the user, set it removed, then updated it.
#Transactional
Optional<Integer> softDelete1(int id) {
// Using method findById from Spring Boot 2+, for SB1.5+ - use method findOne
return userRepo.findById(id).map(user -> {
user.setRemoved(true);
userRepo.save(user);
return 1;
});
}
// Second variant - update the user directly
#Transactional
Optional<Integer> softDelete2(int id) {
return userRepo.softDelete(id);
}
}
User controller:
#RestController
#RequestMapping("/users")
public class UserController {
// Injecting UserService
#DeleteMapping("/{id}")
public ResponseEntity<?> softDelete(#PathVariable("id") int id) {
return userService.softDelete1(id) // you can use 1 or 2 variants
.map(i -> ResponseEntity.noContent().build()) // on success
.orElse(ResponseEntity.notFound().build()); // if user not found
}
}
User repo:
public interface UserRepo extends JpaRepository<User, Integer>() {
#Modifying(clearAutomatically = true)
#Query("update User u set u.removed = true where u.id = ?1")
int remove(int id);
default Optional<Integer> softDelete(int id) {
return (remove(id) > 0) ? Optional.of(1) : Optional.empty();
}
}
UPDATED
Or, I think, you can just try to simply override the deleteById method of CrudRepository (not tested - please give a feedback):
#Override
#Modifying(clearAutomatically = true)
#Query("update User u set u.removed = true where u.id = ?1")
void deleteById(Integer id);
then use it in your service.
I'm trying to test some services with Mockito but I have problems when the main class that I test and where I inject Mocks calls to super.
I run the project with spring and these are the steps I follow to get the error.
Here is where I create the test
public class UrlShortenerTests {
private MockMvc mockMvc;
#Mock
private ShortURLRepository shortURLRepository;
#Mock
private ClickRepository clickRespository;
#InjectMocks
private UrlShortenerControllerWithLogs urlShortenerWL;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(urlShortenerWL).build();
}
#Test
public void thatShortenerCreatesARedirectIfTheURLisOK() throws Exception {
mockMvc.perform(post("/link")
.param("url", "http://www.google.com"))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.target", is("http://example.com/")));
}
}
Here is the class UrlShortenerControllerWithLogs with the method shortener, which is the one I want to test with the previous POST call
#RestController
public class UrlShortenerControllerWithLogs extends UrlShortenerController {
#Autowired
private ClickRepository clickRepository;
#Autowired
private ShortURLRepository SURLR;
public ResponseEntity<ShortURL> shortener(#RequestParam("url") String url,
#RequestParam(value = "sponsor", required = false) String sponsor,
#RequestParam(value = "brand", required = false) String brand,
HttpServletRequest request) {
ResponseEntity<ShortURL> su = super.shortener(url, sponsor, brand,
request);
return su;
}
And this is the super class
#RestController
public class UrlShortenerController {
#Autowired
protected ShortURLRepository shortURLRepository;
#Autowired
protected ClickRepository clickRepository;
#RequestMapping(value = "/link", method = RequestMethod.POST)
public ResponseEntity<ShortURL> shortener(#RequestParam("url") String url,
#RequestParam(value = "sponsor", required = false) String sponsor,
#RequestParam(value = "brand", required = false) String brand,
HttpServletRequest request) {
ShortURL su = createAndSaveIfValid(url, sponsor, brand, UUID
.randomUUID().toString(), extractIP(request));
if (su != null) {
HttpHeaders h = new HttpHeaders();
h.setLocation(su.getUri());
return new ResponseEntity<>(su, h, HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
protected ShortURL createAndSaveIfValid(String url, String sponsor,
String brand, String owner, String ip) {
UrlValidator urlValidator = new UrlValidator(new String[] { "http",
"https" });
if (urlValidator.isValid(url)) {
String id = Hashing.murmur3_32()
.hashString(url, StandardCharsets.UTF_8).toString();
ShortURL su = new ShortURL(id, url,
linkTo(
methodOn(UrlShortenerController.class).redirectTo(
id, null)).toUri(), sponsor, new Date(
System.currentTimeMillis()), owner,
HttpStatus.TEMPORARY_REDIRECT.value(), true, ip, null);
return shortURLRepository.save(su);
} else {
return null;
}
}
So, when I call to shortURLRepository.save(su) in the second method (createAndSaveIfValid), it never enters in the method save, so it returns me null instead of the object I want.
The code of the implementation of ShortURLRepository and the method save is:
#Repository
public class ShortURLRepositoryImpl implements ShortURLRepository {
private static final Logger log = LoggerFactory
.getLogger(ShortURLRepositoryImpl.class);
#Override
public ShortURL save(ShortURL su) {
try {
jdbc.update("INSERT INTO shorturl VALUES (?,?,?,?,?,?,?,?,?)",
su.getHash(), su.getTarget(), su.getSponsor(),
su.getCreated(), su.getOwner(), su.getMode(), su.getSafe(),
su.getIP(), su.getCountry());
} catch (DuplicateKeyException e) {
log.debug("When insert for key " + su.getHash(), e);
return su;
} catch (Exception e) {
log.debug("When insert", e);
return null;
}
return su;
}
I think that the problem is that the object ShortURLRepository created in the test class is not initialized on the super class (UrlShortenerController) or something similar.
Is it possible?
Can anybody help me?
The full code is in: https://github.com/alberto-648702/UrlShortener2014
The class UrlShortenerTests is in:
bangladeshGreen/src/test/java/urlshortener2014/bangladeshgreen
The class UrlShortenerControllerWithLogs is in:
bangladeshGreen/src/main/java/urlshortener2014/bangladeshgreen/web
The class UrlShortenerController is in:
common/src/main/java/urlshortener2014/common/web
The class ShortURLRepositoryImpl is in:
common/src/main/java/urlshortener2014/common/repository
This is not an error. This is the expected behaviour. #Mock creates a mock. #InjectMocks creates an instance of the class and injects the mocks that are created with the #Mock. A mock is not a real object with known values and methods. It is an object that has the same interface as the declared type but you control its behaviour. By default the mocked object methods do nothing (e.g. return null). Therefore if ShortURLRepository is mocked and injected in UrlShortenerControllerWithLogs calling save in the injected ShortURLRepository does not call the real code as you expected, it does nothing. If you want to mock the behaviour of save, add the following code in your setup:
when(shortURLRepository.save(org.mockito.Matchers.any(ShortURL.class))).
then(new Answer<ShortURL>() {
#Override
public ShortURL answer(InvocationOnMock invocation) throws Throwable {
ShortURL su = (ShortURL) invocation.getArguments()[0];
// Do something with su if needed
return su;
}
});
I am new to Junit.Please help me to test Spring hibernate Controller with ContentType is application/json
Below is my Controller
#Controller
#RequestMapping(value="/users")
public class UserServiceImpl implements UserService{
private static Logger logger = Logger.getLogger(UserService.class);
private UserDao userDao;
#Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
#RequestMapping(method = RequestMethod.POST,headers = "content-type=application/json")
#ResponseBody
public long addUser(#RequestBody UserForm user) {
logger.info("Creating new user {}"+ user);
return userDao.create(user);
}
#RequestMapping(value = "/{userId}", method = RequestMethod.GET)
#ResponseBody
public User findUser(#PathVariable(value = "userId") String userId) {
logger.info("Reading user with id {}"+ userId);
User user = userDao.find(userId);
Validate.isTrue(user != null, "Unable to find user with id: " + userId);
return user;
}
#RequestMapping(value = "/{userId}", method = RequestMethod.PUT,headers = "content-type=application/json")
#ResponseStatus(value = HttpStatus.NO_CONTENT)
public void updateUser(#PathVariable(value = "userId") String userId, #RequestBody UserForm user) {
logger.info("Updating user with id {} with {}"+ userId +"->"+ user);
Validate.isTrue(userId.equals(user.getUserId()), "userId doesn't match URL userId: " + user.getUserId());
userDao.update(user);
}
#RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
#ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deleteUser(#PathVariable(value = "userId") String userId) {
logger.info("Deleting user with id {}"+ userId);
userDao.delete(userId);
}
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public List<User> list() {
logger.info("Listing users");
return new ArrayList<User>(userDao.getUsers());
}
}
Can any one Send me the Junit Test case for Any one of the CRUD operations.
Thanks in Advance
Srikanth
If you just want to test your controller, then I would say that mock the DAO. You don't have to care about content types and such because Spring takes care of them. You are interested what the controller method is returning. If you want to test your DAO that User actually is saved to database, that's another story.
But just for testing that controller does what it is supposed to, something like this for example. Example uses EasyMock. I haven't compiled this example so it might have typos.
import static org.easymock.EasyMock.createNiceMock;
public class ControllerTest {
private UserServiceImpl userService;
private UserDao userDaoMock;
#Before
public void setup() {
userDaoMock = createNiceMock(UserDao.class);
userService = new UserServiceImpl();
userSerivce.setUserDao(userDaoMock);
}
#Test
public void testAddUser() {
UserForm userForm = new UserForm();
long expectedResult = 5L;
expect(userDaoMock.create(userForm)).andReturn(expectedResult);
replay(userDaoMock);
long actualResult = userService.addUser(userForm);
verify(userDaoMock);
assertEquals(expectedResult, actualResult);
}
}