private DistrictModel dummyDataDistrict() { final DistrictModel districtModel = new DistrictModel(); // modelService.create(DistrictModel.class); // new DistrictModel(); districtModel.setCode("TEST-DISTRICT-CODE"); districtModel.setName("TEST-DISTRICT-NAME-EN", Locale.ENGLISH); districtModel.setName("TEST-DISTRICT-NAME-ZH", Locale.CHINESE); final CityModel cityModel = new CityModel(); // modelService.create(CityModel.class); //new CityModel(); cityModel.setCode("TEST-CITY-CODE"); cityModel.setName("TEST-CITY-NAME-EN", Locale.ENGLISH); cityModel.setName("TEST-CITY-NAME-ZH", Locale.CHINESE); districtModel.setCity(cityModel); return districtModel; }
/** * The aim of this test is to test that: * * <p>1) The facade's method getStadiums makes a call to the CityService's method getStadiums * * <p>2) The facade then correctly wraps StadiumModels that are returned to it from the * CityService's getStadiums into Data Transfer Objects of type CityData. */ @Test public void testGetCitiesByRegionCode() { /** * We instantiate an object that we would like to be returned to StadiumFacade when the mocked * out CityService's method getStadiums is called. This will be a list of two StadiumModels. */ final List<CityModel> cityModels = dummyDataCityList(); final CityModel cityModel = dummyDataCity(); // We tell Mockito we expect CityService's method getStadiums to be called, // and that when it is, cityModels should be returned when(cityService.getCitiesByRegionCode("TEST-REGION-ISOCODE")).thenReturn(cityModels); doAnswer( new Answer<CityData>() { @Override public CityData answer(final InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); if (args[1] instanceof CityData) { final CityData rd = (CityData) args[1]; rd.setName( "TEST-CITY-NAME-EN"); // assuming now emulating the value for Locale.ENGLISH rd.setCode("TEST-CITY-CODE"); } return null; } }) .when(this.cityPopulator) .populate( org.mockito.Matchers.any(CityModel.class), org.mockito.Matchers.any(CityData.class)); /** * We now make the call to StadiumFacade's getStadiums. If within this method a call is made to * CityService's getStadiums, Mockito will return the cityModels instance to it. Mockito will * also remember that the call was made. */ final List<CityData> dto = cityFacade.getCitiesByRegionCode("TEST-REGION-ISOCODE"); // We now check that dto is a DTO version of cityModels.. assertNotNull(dto); assertEquals(dto.size(), cityModels.size()); assertEquals(dto.get(0).getName(), cityModel.getName(Locale.ENGLISH)); assertEquals(dto.get(0).getCode(), cityModel.getCode()); }
@Test public void testGetCityForCode() { /** * We instantiate an object that we would like to be returned to StadiumFacade when the mocked * out CityService's method getStadiums is called. This will be a list of two StadiumModels. */ final CityModel cityModel = dummyDataCity(); // We tell Mockito we expect CityService's method getStadiumForCode to be // called, and that when it is, cityModel should be returned when(cityService.getCityForCode("TEST-CITY-CODE")).thenReturn(cityModel); doAnswer( new Answer<CityData>() { @Override public CityData answer(final InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); if (args[1] instanceof CityData) { final CityData rd = (CityData) args[1]; rd.setName( "TEST-CITY-NAME-EN"); // assuming now emulating the value for Locale.ENGLISH rd.setCode("TEST-CITY-CODE"); } return null; } }) .when(this.cityPopulator) .populate( org.mockito.Matchers.any(CityModel.class), org.mockito.Matchers.any(CityData.class)); /** * We now make the call to StadiumFacade's getStadium. If within this method a call is made to * CityService's getStadium, Mockito will return the cityModel instance to it. Mockito will also * remember that the call was made. */ final CityData cityData = cityFacade.getCityForCode("TEST-CITY-CODE"); // Check that cityData is a correct DTO representation of the cityModel ServiceModel assertEquals(cityData.getName(), cityModel.getName(Locale.ENGLISH)); assertEquals(cityData.getCode(), cityModel.getCode()); }
// Convenience method for returning a Stadium private CityModel dummyDataCity() { final CityModel cityModel = new CityModel(); cityModel.setActive(Boolean.TRUE); cityModel.setCode("TEST-CITY-CODE"); cityModel.setName( "TEST-CITY-NAME-EN", Locale .ENGLISH); // if just new CityModel(), then here: [mjava.lang.IllegalStateException: // there is no LocaleProvider for (detached) model // de.hybris.platform.servicelayer.model.ItemModelContextImpl@6b6955aa cityModel.setName("TEST-CITY-NAME-ZH", Locale.CHINESE); final RegionModel regionModel = new RegionModel(); regionModel.setIsocode("TEST-REGION-ISOCODE"); regionModel.setActive(Boolean.TRUE); regionModel.setName("TEST-REGION-NAME-EN", Locale.ENGLISH); regionModel.setName("TEST-REGION-NAME-ZH", Locale.CHINESE); cityModel.setRegion(regionModel); return cityModel; }
@Override public Collection<ZoneDeliveryModeValueModel> findDeliveryValues( final DeliveryModeModel mode, final DistrictModel district) { final StringBuilder strBdr = new StringBuilder(); strBdr .append("SELECT {dmv.") .append(ZoneDeliveryModeValueModel.PK) .append("} ") .append("FROM {") .append(ZoneDeliveryModeValueModel._TYPECODE) .append(" AS dmv}, ") .append("{") .append(ZoneModel._TYPECODE) .append(" AS z}, ") .append("{") .append(ZoneDeliveryModeModel._TYPECODE) .append(" AS zdm}, ") .append("{") .append(CurrencyModel._TYPECODE) .append(" AS cur}, ") .append("{") .append(DistrictModel._TYPECODE) .append(" AS d} ") .append("WHERE {dmv.") .append(ZoneDeliveryModeValueModel.CURRENCY) .append("} = {cur.") .append(CurrencyModel.PK) .append("} ") .append("AND {dmv.") .append(ZoneDeliveryModeValueModel.DELIVERYMODE) .append("} = {zdm.") .append(ZoneDeliveryModeModel.PK) .append("} ") .append("AND {dmv.") .append(ZoneDeliveryModeValueModel.ZONE) .append("} = {z.") .append(ZoneModel.PK) .append("} ") .append("AND {z.") .append(ZoneModel.DISTRICT) .append("} = {d.") .append(DistrictModel.PK) .append("} ") .append("AND {cur.") .append(CurrencyModel.ISOCODE) .append("} = 'CNY' ") .append("AND {d.") .append(DistrictModel.PK) .append("} = ?district ") .append("AND {zdm.") .append(ZoneDeliveryModeModel.PK) .append("} = ?mode ") .append("AND {z.") .append(ZoneModel.REGION) .append("} IS NULL ") .append("AND {z.") .append(ZoneModel.CITY) .append("} IS NULL"); final HashMap<String, Object> params = new HashMap<>(); params.put("mode", mode); params.put("district", district); final FlexibleSearchQuery fQuery = new FlexibleSearchQuery(strBdr.toString()); fQuery.addQueryParameters(params); fQuery.setResultClassList(Collections.singletonList(ZoneDeliveryModeValueModel.class)); final SearchResult<ZoneDeliveryModeValueModel> result = getFlexibleSearchService().search(fQuery); final Collection<ZoneDeliveryModeValueModel> vs1 = result.getResult(); if (CollectionUtils.isNotEmpty(vs1)) { return vs1; } else { final CityModel city = district.getCity(); final Collection<ZoneDeliveryModeValueModel> vs2 = findDeliveryValues(mode, city); if (CollectionUtils.isNotEmpty(vs2)) { return vs2; } else { final RegionModel region = city.getRegion(); final Collection<ZoneDeliveryModeValueModel> vs3 = findDeliveryValues(mode, region); if (CollectionUtils.isNotEmpty(vs3)) { return vs3; } else { return findDeliveryValues(mode); } } } }