HYBRIS DEMO

Enable annotations

 xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http:/ /www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema /context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
>

DAO

package concerttours.daos;
import java.util.List;
import concerttours.model.BandModel;< br />
/**
* An interface for the Band DAO including various operations for retrieving persisted Band model objects
*/
public interface BandDAO
{
List findBands();

List findBandsByCode(String code );
}

DAOImpl

package concerttours.daos.impl;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery ;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import concerttours.daos.BandDAO;
import concerttours.model.BandModel;

@Component(value = "bandDAO")
public class DefaultBandDAO implements BandDAO
{

@Autowired
private FlexibleSearchService flexibleSearchService;

@Override
public List findBands ()
{
// Query
final String queryString = //
"SELECT {p:" + BandModel.PK + "} "//
+ " FROM {" + BandModel._TYPECODE + "AS p} ";
final FlexibleSearchQuery query = new FlexibleSearchQuery(quer yString);
return flexibleSearchService. search(query).getResult();
}

@Override
public List findBandsByCode(final String code)
{
// Query with parameters
final String queryString = //
"SELECT {p:" + BandModel.PK + "}" //
+ "FROM {" + BandModel._TYPECODE + "AS p} "//
+ "WHERE" + "{p:" + BandModel.CODE + "}=?code ";
final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString);
query.addQueryParameter("code", code);
return flexibleSearchService. search(query).getResult();
}
}
Here used @Component(value = “bandDAO”) to realize

service

package concerttours.service;
import java.util.List;
import concerttours .model.BandModel;

p ublic interface BandService
{
List getBands();

BandModel getBandForCode(String code);
}

serviceImpl

package concerttours.service.impl;
import de.hybris.platform.servicelayer.exceptions.AmbiguousIdentifierException;
import de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException ;
import java.util.List;
import org.springframework.beans.factory.annotation.Required;
import concerttours.daos.BandDAO;
import concerttours.model.BandModel ;
import concerttours.service.BandService;

public class DefaultBandService implements BandService
{
@Resource
private ModelService modelService;

private BandDAO bandDAO;

@Override
public List getBands()
{
return bandDAO.findBands();
}< br />
@Test
public void Test()
{
//insert
final XXX xxx = modelService.create(XXX.class);
xxx.sexxx(aaa);
modelService.save(xxx);
//Batch 1
UserModel user = userService.getUserForUID(testUid);
final UserGroupModel group = modelService.create(UserGroupModel.class);
group.setUid("testGroup2");
group.setMaxBruteForceLoginAttempts(Integer.valueOf (3));
user.setGroups((Set) ImmutableSet.builder().addAll(user.getGroups()).add(group).build());
modelService.saveAll(user , group);
}

@Override
public BandModel getBandForCode(final String code) throws AmbiguousIdentifierException, UnknownIdentifierException
{
final List result = bandDAO.findBandsByCode(code);
if (result.isEmpty())
{
throw new UnknownIdentifierException("Band with code'" + code + "'not found!");
}
else if (result.size( )> 1)
{
throw new AmbiguousIdentifierException("Band code'" + code + "'is not unique, "+ result.size() +" bands found!");
}
return result.get(0);
}
@Required
public void setBandDAO(final BandDAO bandDAO)
{
this.bandDAO = bandDAO ;
}
}
here, service is configured in xml, Dao is injected into the service, so Dao can be directly injected, get, set instead of @Autowired



Integration Test

package concerttours.daos.impl;
import static org.junit.Assert.assertTrue;
import de.hybris.bootstrap.annotations.IntegrationTest;
import de.hybris.platform.servicelayer.ServicelayerTransactionalTest;
import de.hybris.platform.servicelayer.model.ModelService;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import concerttours.daos.BandDAO;
import concerttours.model.BandModel;


@IntegrationTest
public class DefaultBandDAOIntegrationTest extends ServicelayerTransactionalTest
{

@Resource
private BandDAO bandDAO;

@Resource
private ModelService modelService;
< br /> private static final String BAND_CODE = "ROCK-11";

private static final String BAND_NAME = "Ladies of Rock";

private static final String BAND_HISTORY = " All female rock band formed in Munich in the late 1990s";

private static final Long ALBUMS_SOLD = Long.valueOf(1000L);
@Test
public void bandDAOTest()< br /> {

List bandsByCode = bandDAO.findBandsByCode (BAND_CODE);
assertTrue("No Band should be returned", bandsByCode.isEmpty());

List allBands = bandDAO.findBands();
final int size = allBands.size();

final BandModel bandModel = modelService.create(BandModel.class);

bandModel.setCode(BAND_CODE);
bandModel .setName(BAND_NAME);
bandModel.setHistory(BAND_HISTORY);
bandModel.setAlbumSales(ALBUMS_SOLD);
modelService.save(bandModel);
// check we now get one more band back than previously and our test band is in the list
allBands = bandDAO.findBands();
Assert.assertEquals(size + 1, allBands.size());
Assert. assertTrue("band not found", allBands.contains(bandModel));
// check we can locate our test band by its code
bandsByCode = bandDAO.findBandsByCode(BAND_CODE);
Assert .assertEquals("Did not find the Band we just saved", 1, bandsByCode.size());
Assert.assertEquals("Retrieved Band's code attribute incorrect", BAND_CODE, bandsByCode.get(0).getCode());
Assert.assertEquals("Retrieved Band's name attribute incorrect", BAND_NAME, bandsByCode.get(0).getName());
Assert.assertEquals("Retrieved Band's albumSales attribute incorrect", ALBUMS_SOLD, bandsByCode.get(0 ).getAlbumSales());
Assert.assertEquals("Retrieved Band's history attribute incorrect", BAND_HISTORY, bandsByCode.get(0).getHistory());
}
@Test
public void testFindBands_EmptyStringParam()
{
//calling findBandsByCode() with an empty String-returns no results
final List bands = bandDAO.findBandsByCode("");< br /> Assert.assertTrue("No Band should be returned", bands.isEmpty());
}

@Test(expected = IllegalArgumentException.class)
public void testfindBands_NullPa ram()
{
//calling findBandByCode with null should throw an IllegalArgumentException
bandDAO.findBandsByCode(null); //method's return value not captured
}
}

ant

ant all
ant yunitinit
ant integrationtests -Dtestclasses.packages = concerttours. *
View the test results in /hybris/log/junit/index.html

unit test

package concerttours.service.impl ;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import de. hybris.bootstrap.annotations.UnitTest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit. Before;
import org.junit.Test;
import concerttours.daos.BandDAO;
import concerttours.model.BandModel;

/**
* This test file tests and demonstrates the behavior of the BandService's methods getAllBand, getBand and saveBand.
*
* We already have a separate file for testing the Band DAO, and we do not want this test to implicitly test that in
* addition to the BandService. This test therefore mocks out the Band DAO leaving us to test the Service in isolation,
* whose behavior should be simply to wraps calls to the DAO: forward calls to it, and passing on the results it
* receives from it.
*/
@UnitTest
public class DefaultBandServiceUnitTest
{< br /> private DefaultBandService bandService;
private BandDAO bandDAO;
private BandModel bandModel;
/** Name of test band. */
private static final String BAND_CODE = "Ch00X" ;
/** Name of test band. */
private static final String BAND_NAME = "Singers All";
/** History of test band. */
private static final String BAND_HISTORY = "Medieval choir formed in 2001, based in Munich famous for authentic monastic chants";
@Before
public void setUp()
{
// We will be testing BandServiceImpl-an implementation of BandService
bandService = new DefaultBandService();
// So as not to implicitly also test the DAO, we will mock out the DAO using Mockito
bandDAO = mock( BandDAO.class); // and inject this mocked DAO into the BandService
bandService.setBandDAO(bandDAO);
// This instance of a BandModel will be used by the tests
bandModel = new BandModel( );
bandModel.setCode(BAND_CODE);
bandModel.setName(BAND_NAME);
bandModel.setAlbumSales(null);
bandModel.setHistory(BAND_HISTORY);
}
/**
* This test tests and demonstrates that the Service's getAllBands method calls the DAOs' getBands method and returns
* the data it receives from it.
*/< br /> @Test
public void testGetAllBands()
{
// We construct the data we would like the mocked out DAO to return when called
final List bandModels = Arrays.asList(bandModel);
//Use Mockito and compare results
when(bandDAO.findBands()).thenReturn(bandModels);
// Now we make the call to BandService 's getBands() which we expect to call the DAOs' findBands() method
final List result = bandService.getBands();
// We then verify that the results returned from the service match those returned by the mocked-out DAO
assertEquals("We should find one", 1, result.size());
assertEquals("And should equals what the mock returned", bandModel, result .get(0));
}
@Test
public void testGetBand()
{
// Tell Mockito we expect a call to the DAO's getBand() , and, if it occurs, Mockito should return BandModel instance
when(bandDAO.findBandsByCode(BAND_CODE)).thenReturn(Collections.singletonList(bandModel));
// We make the call to the Service's getBandForCode () which we expect to call the DAO's findBandsByCode()
final BandModel result = bandService.getBandForCode(BAND_CODE);
// We then verify that the result returned from the Service is the sa me as that returned from the DAO
assertEquals("Band should equals() what the mock returned", bandModel, result);
}
}

ant

ant all
ant unittests -Dtestclasses.packages = concerttours. *

Facade

Create DTO


Data object for a tour summary which has no equivalent on the type system



here through xml reverse generation DTO entity class

define facade interface

package concerttours.facades;
import concerttours.data. TourData;

public interface TourFacade
{
BandData getBand(String name);
}

Define Facade implementation class

public class DefaultBandFacade implements BandFacade
{
private BandService bandService;
@Override
public List getBands()
{
final List bandModels = bandService.getBands();
fi nal List bandFacadeData = new ArrayList<>();
for (final BandModel sm: bandModels)
{
final BandData sfd = new BandData();
sfd. setId(sm.getCode());
sfd.setName(sm.getName());
sfd.setDescription(sm.getHistory());
sfd.setAlbumsSold(sm.getAlbumSales ());
bandFacadeData.add(sfd);
}
return bandFacadeData;
}
}

injection




Leave a Comment

Your email address will not be published.