Created
December 7, 2018 02:18
-
-
Save jdcasey/d50ff6efc2cca6bfaa9c48e7fac01080 to your computer and use it in GitHub Desktop.
test class and DTO for sending repository changelog to Elasticsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.commonjava.indy.ftest.core.store; | |
import com.github.difflib.DiffUtils; | |
import com.github.difflib.UnifiedDiffUtils; | |
import com.github.difflib.algorithm.DiffAlgorithmListener; | |
import com.github.difflib.patch.Patch; | |
import org.apache.commons.lang.StringUtils; | |
import org.commonjava.indy.model.core.RemoteRepository; | |
import org.commonjava.indy.model.core.StoreKey; | |
import org.commonjava.indy.model.core.io.IndyObjectMapper; | |
import org.junit.Test; | |
import javax.enterprise.inject.spi.CDI; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import static org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor.MAVEN_PKG_KEY; | |
import static org.hamcrest.CoreMatchers.equalTo; | |
import static org.junit.Assert.assertThat; | |
public class AddRemoteRepoThenModifyAndGenerateDiffEventTest | |
extends AbstractStoreManagementTest | |
{ | |
@Test | |
public void addAndModifyRemoteRepositoryThenRetrieveIt() | |
throws Exception | |
{ | |
final RemoteRepository rr = new RemoteRepository( MAVEN_PKG_KEY, newName(), "http://www.foo.com" ); | |
RemoteRepository rrOrig = rr.copyOf(); | |
client.stores() | |
.create( rr, name.getMethodName(), RemoteRepository.class ); | |
rr.setUrl( "https://www.foo.com/" ); | |
assertThat( rr.getUrl(), equalTo( "https://www.foo.com/" ) ); | |
final boolean updated = client.stores() | |
.update( rr, name.getMethodName() ); | |
assertThat( updated, equalTo( true ) ); | |
final RemoteRepository result = client.stores() | |
.load( rr.getKey(), RemoteRepository.class ); | |
IndyObjectMapper om = CDI.current().select( IndyObjectMapper.class ).get(); | |
String jsonOrig = om.writeValueAsString( rrOrig ); | |
String jsonResult = om.writeValueAsString( result ); | |
final DiffAlgorithmListener listener = null; | |
Patch<String> diff = DiffUtils.diff( jsonOrig, jsonResult, listener ); | |
String filename = "remote-" + rrOrig.getName() + ".json"; | |
List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff( "a/" + filename, "b/" + filename, | |
Arrays.asList( jsonOrig.split( "\\\n" ) ), diff, | |
0 ); | |
String diffTxt = StringUtils.join( udiff, "\n" ); | |
DiffEventDTO dto = | |
new DiffEventDTO( rrOrig.getKey(), new Date(), "2", "this is a test changelog", "me", diffTxt ); | |
System.out.println( "\n\n\n\n" + om.writeValueAsString( dto ) + "\n\n\n\n" ); | |
} | |
private static final class DiffEventDTO | |
{ | |
private StoreKey storeKey; | |
private Date timestamp; | |
private String version; | |
private String changelog; | |
private String author; | |
private String diff; | |
public DiffEventDTO( final StoreKey storeKey, final Date timestamp, final String version, final String changelog, | |
final String author, final String diff ) | |
{ | |
this.storeKey = storeKey; | |
this.timestamp = timestamp; | |
this.version = version; | |
this.changelog = changelog; | |
this.author = author; | |
this.diff = diff; | |
} | |
public StoreKey getStoreKey() | |
{ | |
return storeKey; | |
} | |
public void setStoreKey( final StoreKey storeKey ) | |
{ | |
this.storeKey = storeKey; | |
} | |
public Date getTimestamp() | |
{ | |
return timestamp; | |
} | |
public void setTimestamp( final Date timestamp ) | |
{ | |
this.timestamp = timestamp; | |
} | |
public String getVersion() | |
{ | |
return version; | |
} | |
public void setVersion( final String version ) | |
{ | |
this.version = version; | |
} | |
public String getChangelog() | |
{ | |
return changelog; | |
} | |
public void setChangelog( final String changelog ) | |
{ | |
this.changelog = changelog; | |
} | |
public String getAuthor() | |
{ | |
return author; | |
} | |
public void setAuthor( final String author ) | |
{ | |
this.author = author; | |
} | |
public String getDiff() | |
{ | |
return diff; | |
} | |
public void setDiff( final String diff ) | |
{ | |
this.diff = diff; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment