@Test public void testGetDistrictsByCityCode() { final List<DistrictModel> districtModels = dummyDataDistrictList(); final DistrictModel districtModel = dummyDataDistrict(); when(districtService.getDistrictsByCityCode("TEST-CITY-CODE")).thenReturn(districtModels); doAnswer( new Answer<DistrictData>() { @Override public DistrictData answer(final InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); if (args[1] instanceof DistrictData) { final DistrictData dd = (DistrictData) args[1]; dd.setName("TEST-DISTRICT-NAME-EN"); // assuming now emulating the value for // Locale.ENGLISH dd.setCode("TEST-DISTRICT-CODE"); } return null; } }) .when(this.districtPopulator) .populate( org.mockito.Matchers.any(DistrictModel.class), org.mockito.Matchers.any(DistrictData.class)); final List<DistrictData> dto = districtFacade.getDistrictsByCityCode("TEST-CITY-CODE"); assertNotNull(dto); assertEquals(dto.size(), districtModels.size()); assertEquals(dto.get(0).getName(), districtModel.getName(Locale.ENGLISH)); assertEquals(dto.get(0).getCode(), districtModel.getCode()); }
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; }
@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); } } } }