### Question:
OnlineLibraryVersionDetector implements LibraryVersionDetector { public Map<String, VersionInfo> acquireVersions() { ReleaseType relType; synchronized (cacheLock) { if (!versionCache.isEmpty() && (System.currentTimeMillis() - lastAccess) < REFRESH_TIMEOUT_MILLIS) { return versionCache; } relType = cachedReleaseType; } try { var libDict = new HashMap<String, VersionInfo>(); var verData = client.postRequestForString(LIBRARY_VERSIONING_URL, "", IHttpClient.HttpDataType.JSON_DATA); var inStream = new ByteArrayInputStream(verData.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = factory.newDocumentBuilder(); Document doc = dBuilder.parse(inStream); var root = doc.getDocumentElement(); addVersionsToMap(root.getElementsByTagName("Libraries"), "Library", relType, libDict); addVersionsToMap(root.getElementsByTagName("Plugins"), "Plugin", relType, libDict); synchronized (cacheLock) { lastAccess = System.currentTimeMillis(); versionCache = libDict; } return libDict; } catch (Exception e) { logger.log(System.Logger.Level.ERROR, "Unable to get versions from main site", e); } return versionCache; } OnlineLibraryVersionDetector(IHttpClient client, ReleaseType initialReleaseType); void changeReleaseType(ReleaseType relType); ReleaseType getReleaseType(); Map<String, VersionInfo> acquireVersions(); void upgradePlugin(String name, VersionInfo requestedVersion); final static String LIBRARY_VERSIONING_URL; }### Answer:
@Test public void testReadingXmlOverMockHttpForBeta() throws IOException, InterruptedException { var mockHttp = Mockito.mock(IHttpClient.class); when(mockHttp.postRequestForString(LIBRARY_VERSIONING_URL, "", HttpDataType.JSON_DATA)).thenReturn(xmlData); var verDet = new OnlineLibraryVersionDetector(mockHttp, ReleaseType.BETA); var versions = verDet.acquireVersions(); assertEquals("10.4.1", versions.get("tcMenu/Library").toString()); assertEquals("10.4.11", versions.get("IoAbstraction/Library").toString()); assertEquals("10.2.0", versions.get("LiquidCrystalIO/Library").toString()); assertEquals("10.4.3", versions.get("core-display/Plugin").toString()); assertEquals("10.4.4", versions.get("core-remote/Plugin").toString()); }
@Test public void testReadingXmlOverMockHttp() throws IOException, InterruptedException { var mockHttp = Mockito.mock(IHttpClient.class); when(mockHttp.postRequestForString(LIBRARY_VERSIONING_URL, "", HttpDataType.JSON_DATA)).thenReturn(xmlData); var verDet = new OnlineLibraryVersionDetector(mockHttp, ReleaseType.STABLE); var versions = verDet.acquireVersions(); assertEquals("1.4.1", versions.get("tcMenu/Library").toString()); assertEquals("1.4.11", versions.get("IoAbstraction/Library").toString()); assertEquals("1.2.0", versions.get("LiquidCrystalIO/Library").toString()); assertEquals("1.4.3", versions.get("core-display/Plugin").toString()); assertEquals("1.4.4", versions.get("core-remote/Plugin").toString()); }