Skip to content

Commit ee216ed

Browse files
authored
Merge pull request #2059 from sswguo/npm_folo_fix
Fix the tracking path of npm metadata
2 parents daccef8 + c50fd9b commit ee216ed

File tree

6 files changed

+108
-7
lines changed

6 files changed

+108
-7
lines changed

addons/folo/common/src/main/java/org/commonjava/indy/folo/change/FoloTrackingListener.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.net.MalformedURLException;
5252
import java.util.Map;
5353

54+
import static org.commonjava.indy.folo.ctl.FoloConstants.ORIGIN_PATH;
5455
import static org.commonjava.indy.model.core.StoreType.group;
5556

5657
@ApplicationScoped
@@ -112,15 +113,17 @@ public void onFileAccess( @Observes final FileAccessEvent event )
112113
return;
113114
}
114115

115-
logger.trace( "Tracking report: {} += {} in {} (DOWNLOAD)", trackingKey, transfer.getPath(),
116+
final String trackingPath = metadata.get( ORIGIN_PATH ) == null ? transfer.getPath() : (String)metadata.get( ORIGIN_PATH );
117+
118+
logger.trace( "Tracking report: {} += {} in {} (DOWNLOAD)", trackingKey, trackingPath,
116119
keyedLocation.getKey() );
117120

118121
//Here we need to think about npm metadata retrieving case. As almost all npm metadata retrieving is through
119122
// /$pkg from remote, but we use STORAGE_PATH in EventMetadata with /$pkg/package.json to store this metadata,
120123
//so the real path for this transfer should be /$pkg but its current path is /$pkg/package.json. We need to
121124
//think about if need to do the replacement here, especially for the originalUrl.
122125
recordManager.recordArtifact(
123-
createEntry( trackingKey, keyedLocation.getKey(), accessChannel, transfer.getPath(),
126+
createEntry( trackingKey, keyedLocation.getKey(), accessChannel, trackingPath,
124127
StoreEffect.DOWNLOAD, event.getEventMetadata() ) );
125128
}
126129
catch ( final FoloContentException | IndyWorkflowException e )

addons/folo/common/src/main/java/org/commonjava/indy/folo/ctl/FoloConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class FoloConstants
2626

2727
public static final String LEGACY = "legacy";
2828

29+
public static final String ORIGIN_PATH = "origin-path";
30+
2931
public enum TRACKING_TYPE
3032
{
3133
IN_PROGRESS("in_progress"),

addons/folo/ftests/src/main/java/org/commonjava/indy/folo/ftest/content/GroupMetadataExcludedFromTrackingReportTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
import static org.hamcrest.CoreMatchers.notNullValue;
3232
import static org.junit.Assert.assertThat;
3333

34+
// Ignore this since group metadata had been enabled in the tracking on production via track.group.content=true
3435
public class GroupMetadataExcludedFromTrackingReportTest
3536
extends AbstractFoloContentManagementTest
3637
{
3738

38-
@Test
39+
//@Test
3940
public void run()
4041
throws Exception
4142
{
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.folo.ftest.content;
17+
18+
import org.commonjava.indy.folo.client.IndyFoloAdminClientModule;
19+
import org.commonjava.indy.folo.client.IndyFoloContentClientModule;
20+
import org.commonjava.indy.folo.dto.TrackedContentDTO;
21+
import org.commonjava.indy.folo.dto.TrackedContentEntryDTO;
22+
import org.commonjava.indy.model.core.StoreKey;
23+
import org.junit.Test;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.util.Set;
27+
28+
import static org.commonjava.indy.model.core.StoreType.remote;
29+
import static org.commonjava.indy.model.core.StoreType.group;
30+
import static org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_PKG_KEY;
31+
import static org.hamcrest.CoreMatchers.equalTo;
32+
import static org.hamcrest.CoreMatchers.notNullValue;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertThat;
35+
36+
/**
37+
* <b>GIVEN:</b>
38+
* <ul>
39+
* <li>Repository(remote or group) for npm and path</li>
40+
* </ul>
41+
*
42+
* <br/>
43+
* <b>WHEN:</b>
44+
* <ul>
45+
* <li>Access path through folo track</li>
46+
* </ul>
47+
*
48+
* <br/>
49+
* <b>THEN:</b>
50+
* <ul>
51+
* <li>The path can be tracked correctly</li>
52+
* </ul>
53+
*/
54+
public class VerifyTrackedEntriesForNPMTest
55+
extends AbstractNPMFoloContentManagementTest
56+
{
57+
58+
@Test
59+
public void verifyTrackedEntryForStore() throws Exception
60+
{
61+
62+
final String packageContent =
63+
"{\"name\": \"jquery\",\n" + "\"description\": \"JavaScript library for DOM operations\",\n" + "\"license\": \"MIT\"}";
64+
65+
final String packagePath = "jquery";
66+
67+
final String trackingId = newName();
68+
69+
npmjsServer.expect( npmjsServer.formatUrl( packagePath ), 200, new ByteArrayInputStream( packageContent.getBytes() ) );
70+
71+
IndyFoloContentClientModule folo = client.module( IndyFoloContentClientModule.class );
72+
73+
final StoreKey storeKey = new StoreKey( NPM_PKG_KEY, group, PUBLIC );
74+
75+
folo.get( trackingId, storeKey, packagePath );
76+
77+
IndyFoloAdminClientModule adminModule = client.module( IndyFoloAdminClientModule.class );
78+
boolean success = adminModule.sealTrackingRecord( trackingId );
79+
assertThat( success, equalTo( true ) );
80+
81+
// check report
82+
final TrackedContentDTO report = adminModule.getTrackingReport( trackingId );
83+
assertThat( report, notNullValue() );
84+
85+
final Set<TrackedContentEntryDTO> downloads = report.getDownloads();
86+
assertThat( downloads, notNullValue() );
87+
assertThat( downloads.size(), equalTo( 1 ) );
88+
89+
downloads.stream().forEach( trackedContentEntryDTO -> {
90+
assertEquals("/jquery", trackedContentEntryDTO.getPath());
91+
});
92+
93+
}
94+
95+
}

addons/folo/jaxrs/src/main/java/org/commonjava/indy/folo/bind/jaxrs/FoloNPMContentAccessResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@
4848
import java.nio.file.Paths;
4949

5050
import static org.commonjava.indy.IndyContentConstants.CHECK_CACHE_ONLY;
51+
import static org.commonjava.indy.folo.ctl.FoloConstants.*;
5152
import static org.commonjava.indy.util.RequestContextHelper.CONTENT_TRACKING_ID;
52-
import static org.commonjava.indy.folo.ctl.FoloConstants.ACCESS_CHANNEL;
53-
import static org.commonjava.indy.folo.ctl.FoloConstants.TRACKING_KEY;
5453
import static org.commonjava.indy.pkg.PackageTypeConstants.PKG_TYPE_NPM;
5554
import static org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_PKG_KEY;
5655
import static org.commonjava.maven.galley.spi.cache.CacheProvider.STORE_HTTP_HEADERS;
@@ -199,7 +198,8 @@ public Response doGet( @ApiParam( "User-assigned tracking session key" ) @PathPa
199198
final String baseUri = getBasePath( uriInfo, id );
200199

201200
EventMetadata metadata = new EventMetadata().set( TRACKING_KEY, tk )
202-
.set( ACCESS_CHANNEL, AccessChannel.NATIVE );
201+
.set( ACCESS_CHANNEL, AccessChannel.NATIVE )
202+
.set( ORIGIN_PATH, packageName );
203203

204204
RequestContextHelper.setContext( CONTENT_TRACKING_ID, id );
205205

ftests/common/src/main/java/org/commonjava/indy/ftest/core/AbstractIndyFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ protected void initBaseTestConfig( CoreServerFixture fixture )
305305
+ "store.storage=infinispan\n"
306306
+ "schedule.storage=infinispan");
307307

308-
writeConfigFile( "conf.d/folo.conf", "[folo]\nfolo.cassandra=true"+ "\nfolo.cassandra.keyspace=folo");
308+
writeConfigFile( "conf.d/folo.conf", "[folo]\nfolo.cassandra=true"+ "\nfolo.cassandra.keyspace=folo" + "\ntrack.group.content=True");
309309

310310
if ( isSchedulerEnabled() )
311311
{

0 commit comments

Comments
 (0)