public Scene deserialize(Reader reader) throws IOException { _jsonParser = JSON_FACTORY.createParser(reader); skipStartObject(); _jsonParser.nextToken(); validateRequiredField(SceneSerializationConstant.SCENE_TAG_NAME); _jsonParser.nextToken(); String name = _jsonParser.getValueAsString(); return new Scene(name, null, ".", createHttpExchangeList()); }
/** * Read scene from file and construct Scene object * @param name scene name * @return scene object de-serialized from file * * */ public Scene readScene(String rootPath, String name) throws IOException { File file = new File(rootPath, name); if (file.isFile()) { if (file.length() == 0) { return new Scene(name, null, rootPath, new ArrayList<>()); } BufferedReader reader = Files.newReader(file, Charset.forName(SceneSerializationConstant.FILE_CHARSET)); SceneDeserializer sceneDeserializer = new SceneDeserializer(); return sceneDeserializer.deserialize(reader); } return null; } }
static Scene getMockSceneWithoutBodyAndHeader() throws URISyntaxException { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("https://www.google.com"), null, null); RecordedHttpResponse recordedHttpResponse1 = new RecordedHttpResponse(200, null, null); RecordedHttpExchange recordedHttpExchange1 = new RecordedHttpExchange(recordedHttpRequest1, recordedHttpResponse1, new Date("2 Oct 2015 21:04:49 GMT")); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("https://www.yahoo.com"), null, null); byte[] bytes = {1, 2, 3, 4, 5}; RecordedHttpResponse recordedHttpResponse2 = new RecordedHttpResponse(200, null, null); RecordedHttpExchange recordedHttpExchange2 = new RecordedHttpExchange(recordedHttpRequest2, recordedHttpResponse2, new Date("2 Oct 2015 21:04:49 GMT")); List<RecordedHttpExchange> recordedHttpExchangeList = new ArrayList<>(); recordedHttpExchangeList.add(recordedHttpExchange1); recordedHttpExchangeList.add(recordedHttpExchange2); return new Scene("testing", null, ".", recordedHttpExchangeList); }
/** * Helper method that will be used for unit test * */ static Scene create(SceneConfiguration sceneConfiguration, SceneReader sceneReader) throws IOException { Scene sceneResult = null; Scene sceneFromLocal = sceneReader.readScene(sceneConfiguration.getSceneRoot(), sceneConfiguration.getSceneName()); if (sceneFromLocal == null) { if (sceneConfiguration.getSceneMode() == SceneMode.PLAYBACK || sceneConfiguration.getSceneMode() == SceneMode.SEQUENTIAL_PLAYBACK) { throw new IllegalStateException(String.format("No Scene is found at %s/%s", sceneConfiguration.getSceneRoot(), sceneConfiguration.getSceneName())); } else { sceneResult = new Scene(sceneConfiguration); } } else { List<RecordedHttpExchange> recordedHttpExchanges = sceneFromLocal.getRecordedHttpExchangeList(); // In sequential record mode, start with an empty scene if (sceneConfiguration.getSceneMode() == SceneMode.SEQUENTIAL_RECORD) { recordedHttpExchanges = new ArrayList<>(); } sceneResult = new Scene(sceneConfiguration.getSceneName(), sceneConfiguration.getSceneMode(), sceneConfiguration.getSceneRoot(), recordedHttpExchanges); } return sceneResult; } }
static Scene getMockSceneWithoutBody() throws URISyntaxException { Multimap<String, String> requestHeaders1 = LinkedHashMultimap.create(); requestHeaders1.put("request-header1", "value1"); requestHeaders1.put("request-header2", "value2, value3, value4"); Multimap<String, String> responseHeaders1 = LinkedHashMultimap.create(); responseHeaders1.put("response-header1", "value1"); responseHeaders1.put("response-header2", "value2, value3, value4"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("https://www.google.com"), requestHeaders1, null); RecordedHttpResponse recordedHttpResponse1 = new RecordedHttpResponse(200, responseHeaders1, null); RecordedHttpExchange recordedHttpExchange1 = new RecordedHttpExchange(recordedHttpRequest1, recordedHttpResponse1, new Date("2 Oct 2015 21:04:49 GMT")); Multimap<String, String> requestHeaders2 = LinkedHashMultimap.create(); requestHeaders2.put("request-header11", "value11"); requestHeaders2.put("request-header12", "value12, value13, value14"); Multimap<String, String> responseHeaders2 = LinkedHashMultimap.create(); responseHeaders2.put("response-header11", "value11"); responseHeaders2.put("response-header12", "value21, value31, value41"); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("https://www.yahoo.com"), requestHeaders2, null); byte[] bytes = {1, 2, 3, 4, 5}; RecordedHttpResponse recordedHttpResponse2 = new RecordedHttpResponse(200, responseHeaders2, null); RecordedHttpExchange recordedHttpExchange2 = new RecordedHttpExchange(recordedHttpRequest2, recordedHttpResponse2, new Date("2 Oct 2015 21:04:49 GMT")); List<RecordedHttpExchange> recordedHttpExchangeList = new ArrayList<>(); recordedHttpExchangeList.add(recordedHttpExchange1); recordedHttpExchangeList.add(recordedHttpExchange2); return new Scene("testing", null, ".", recordedHttpExchangeList); }
recordedHttpExchangeList.add(recordedHttpExchange1); recordedHttpExchangeList.add(recordedHttpExchange2); return new Scene("testing", null, ".", recordedHttpExchangeList);
@Test public void testCanReplayScene() { SceneConfiguration sceneConfiguration1 = new SceneConfiguration(ROOT_PATH, SceneMode.PLAYBACK, SCENE_NAME); Scene scene1 = new Scene(sceneConfiguration1); SceneAccessLayer sceneAccessLayer = new SceneAccessLayer(scene1, EasyMock.createStrictMock(SceneWriter.class), EasyMock.createStrictMock(MatchRule.class)); Assert.assertTrue(sceneAccessLayer.canPlayback()); Scene scene2 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.RECORD, SCENE_NAME)); sceneAccessLayer.setScene(scene2); Assert.assertFalse(sceneAccessLayer.canPlayback()); Scene scene3 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.SEQUENTIAL_PLAYBACK, SCENE_NAME)); sceneAccessLayer.setScene(scene3); Assert.assertTrue(sceneAccessLayer.canPlayback()); Scene scene4 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.SEQUENTIAL_RECORD, SCENE_NAME)); sceneAccessLayer.setScene(scene4); Assert.assertFalse(sceneAccessLayer.canPlayback()); }
static Scene getMockSceneWithoutHeaders() throws URISyntaxException { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("https://www.google.com"), null, new RecordedStringHttpBody("Hello world request is awesome")); RecordedHttpResponse recordedHttpResponse1 = new RecordedHttpResponse(200, null, new RecordedStringHttpBody("Hello world response is awesome")); RecordedHttpExchange recordedHttpExchange1 = new RecordedHttpExchange(recordedHttpRequest1, recordedHttpResponse1, new Date("2 Oct 2015 21:04:49 GMT")); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("https://www.yahoo.com"), null, new RecordedStringHttpBody("Hello world request is awesome")); byte[] bytes = {1, 2, 3, 4, 5}; RecordedHttpResponse recordedHttpResponse2 = new RecordedHttpResponse(200, null, new RecordedByteHttpBody(bytes)); RecordedHttpExchange recordedHttpExchange2 = new RecordedHttpExchange(recordedHttpRequest2, recordedHttpResponse2, new Date("2 Oct 2015 21:04:49 GMT")); List<RecordedHttpExchange> recordedHttpExchangeList = new ArrayList<>(); recordedHttpExchangeList.add(recordedHttpExchange1); recordedHttpExchangeList.add(recordedHttpExchange2); return new Scene("testing", null, ".", recordedHttpExchangeList); }