TravisRestService { @Transactional public Map<String, ?> createProf(ProfilingInfo profParams) { Build build = buildRepository.findByTravisBuildId(profParams.getBuildId()); Project project = projectRepository.findByBuild(build); Sample sample = sampleRepository.findByNameAndProject_Id(profParams.getSampleName(), project.getId()); if (sample == null) { sample = new Sample(); sample.setName(profParams.getSampleName()); sample.setProject(project); sample = sampleRepository.save(sample); } sample.getBuilds().add(build); sample = sampleRepository.save(sample); build.getSamples().add(sample); build = buildRepository.save(build); ProfContextDump prof = new ProfContextDump(); prof.setBuild(build); prof.setSample(sample); prof.setState(ProfContextState.CREATED); prof = profContextDumpRepository.save(prof); logger.info("Create Profiling: {}", prof.getId()); return ImmutableMap.of("profId", prof.getId()); }  @Transactional void begin(BuildInfo bi); @Transactional Map<String, ?> createProf(ProfilingInfo profParams); void saveProf(ProfilingContext profilingContext); void updateTreeNodes(List<TreeNode> treeNodes); void finish(long buildId);  }
@Test public void testCreateProf() throws Exception { ProfilingInfo profParams = new Gson().fromJson(IOUtils.toString(ClassLoader.getSystemResource("java-profiler-result/begin.json")), ProfilingInfo.class); Build build = new Build(); build.setId(3L); build.setLanguage(bi.getLang()); build.setTravisBuildId(profParams.getBuildId()); build.setNumber(bi.getBuildNum()); build.setBranch(branch); build.setNumSamples(bi.getNumSamples()); Sample sample = new Sample(); sample.setId(4L); sample.getBuilds().add(build); build.getSamples().add(sample); ProfContextDump prof = new ProfContextDump(); prof.setId(5L); prof.setBuild(build); prof.setSample(sample); prof.setState(ProfContextState.CREATED); when(buildRepository.findByTravisBuildId(eq(profParams.getBuildId()))).thenReturn(build); when(projectRepository.findByBuild(any(Build.class))).thenReturn(project); when(sampleRepository.save(any(Sample.class))).thenReturn(sample); when(buildRepository.save(any(Build.class))).thenReturn(build); when(profContextDumpRepository.save(any(ProfContextDump.class))).thenReturn(prof); Map<String, ?> result = travisRestService.createProf(profParams); assertEquals(result.get("profId"), prof.getId()); verify(buildRepository, times(1)).findByTravisBuildId(eq(profParams.getBuildId())); verify(projectRepository, times(1)).findByBuild(refEq(build, "id")); verify(sampleRepository, times(1)).save(refEq(sample)); verify(sampleRepository, times(1)).findByNameAndProject_Id(eq(profParams.getSampleName()), eq(profParams.getBuildId())); verify(buildRepository, times(1)).save(refEq(build, "id")); verify(profContextDumpRepository, times(1)).save(refEq(prof, "id")); }