ImportExportService { public String exportRepository(List<String> ids, String format){ StringBuilder result = new StringBuilder("{"); ObjectMapper mapper = new ObjectMapper(); List<Service> services = serviceRepository.findByIdIn(ids); try{ String jsonArray = mapper.writeValueAsString(services); result.append("\"services\":").append(jsonArray).append(", "); } catch (Exception e){ log.error("Exception while serializing services for export", e); } List<Resource> resources = resourceRepository.findByServiceIdIn(ids); try{ String jsonArray = mapper.writeValueAsString(resources); result.append("\"resources\":").append(jsonArray).append(", "); } catch (Exception e){ log.error("Exception while serializing resources for export", e); } List<String> operationIds = new ArrayList<>(); for (Service service : services){ for (Operation operation : service.getOperations()){ operationIds.add(IdBuilder.buildOperationId(service, operation)); } } List<Request> requests = requestRepository.findByOperationIdIn(operationIds); List<Response> responses = responseRepository.findByOperationIdIn(operationIds); try{ String jsonArray = mapper.writeValueAsString(requests); result.append("\"requests\":").append(jsonArray).append(", "); jsonArray = mapper.writeValueAsString(responses); result.append("\"responses\":").append(jsonArray); } catch (Exception e){ log.error("Exception while serializing messages for export", e); } return result.append("}").toString(); }  boolean importRepository(String json); String exportRepository(List<String> ids, String format);  }
@Test public void testExportRepository(){ String result = service.exportRepository(ids, "json"); JSONObject jsonObj = null; try { jsonObj = new JSONObject(result); } catch (JSONException e) { fail("No exception should be thrown when parsing Json"); } try{ JSONArray services = jsonObj.getJSONArray("services"); assertEquals(3, services.length()); for (int i=0 ; i<services.length(); i++){ JSONObject service = services.getJSONObject(i); String name = service.getString("name"); assertTrue("HelloWorld".equals(name) || "MyService-hello".equals(name)); } } catch (JSONException e) { fail("Exception while getting services array"); } try{ JSONArray resources = jsonObj.getJSONArray("resources"); assertEquals(1, resources.length()); JSONObject resource = resources.getJSONObject(0); assertEquals("Resource 1", resource.getString("name")); assertEquals("<wsdl></wsdl>", resource.getString("content")); } catch (JSONException e) { fail("Exception while getting resources array"); } }